Skip to content

Commit

Permalink
Do not honor TMPDIR for anonymous temporary files when tainting
Browse files Browse the repository at this point in the history
Use a default of /tmp on Unixes when TMPDIR is unset or empty, or
when creation of a temporary file in it fails

This goes on top of commit 26e8050
  • Loading branch information
rgs committed Jun 10, 2009
1 parent 92a24ac commit 0b99e98
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
22 changes: 16 additions & 6 deletions perlio.c
Expand Up @@ -5174,20 +5174,30 @@ PerlIO_tmpfile(void)
f = PerlIO_fdopen(fd, "w+b");
#else /* WIN32 */
# if defined(HAS_MKSTEMP) && ! defined(VMS) && ! defined(OS2)
const char * const tmpdir = PerlEnv_getenv("TMPDIR");
SV * const sv = newSVpv(tmpdir ? tmpdir : "/tmp", 0);
sv_catpv(sv, "/PerlIO_XXXXXX");
int fd = -1;
char tempname[] = "/tmp/PerlIO_XXXXXX";
const char * const tmpdir = PL_tainting ? NULL : PerlEnv_getenv("TMPDIR");
SV * const sv = tmpdir && *tmpdir ? newSVpv(tmpdir, 0) : NULL;
/*
* I have no idea how portable mkstemp() is ... NI-S
*/
const int fd = mkstemp(SvPVX(sv));
if (sv) {
/* if TMPDIR is set and not empty, we try that first */
sv_catpv(sv, tempname + 4);
fd = mkstemp(SvPVX(sv));
}
if (fd < 0) {
/* else we try /tmp */
fd = mkstemp(tempname);
}
if (fd >= 0) {
f = PerlIO_fdopen(fd, "w+");
if (f)
PerlIOBase(f)->flags |= PERLIO_F_TEMP;
PerlLIO_unlink(SvPVX_const(sv));
PerlLIO_unlink(sv ? SvPVX_const(sv) : tempname);
}
SvREFCNT_dec(sv);
if (sv)
SvREFCNT_dec(sv);
# else /* !HAS_MKSTEMP, fallback to stdio tmpfile(). */
FILE * const stdio = PerlSIO_tmpfile();

Expand Down
4 changes: 2 additions & 2 deletions t/io/perlio.t
Expand Up @@ -96,7 +96,7 @@ ok(close($utffh));
if !$Config{d_mkstemp}
|| $^O eq 'VMS' || $^O eq 'MSwin32' || $^O eq 'os2';
local $ENV{TMPDIR} = $nonexistent;
ok( !open(my $x,"+<",undef), 'TMPDIR honored by magic temp file via 3 arg open with undef - fails if TMPDIR points to a non-existent dir');
ok( open(my $x,"+<",undef), 'TMPDIR honored by magic temp file via 3 arg open with undef - works if TMPDIR points to a non-existent dir');

mkdir $ENV{TMPDIR};
ok(open(my $x,"+<",undef), 'TMPDIR honored by magic temp file via 3 arg open with undef - works if TMPDIR points to an existent dir');
Expand Down Expand Up @@ -148,6 +148,6 @@ END {
1 while unlink $txt;
1 while unlink $bin;
1 while unlink $utf;
1 while rmdir $nonexistent;
rmdir $nonexistent;
}

0 comments on commit 0b99e98

Please sign in to comment.