Skip to content

Commit

Permalink
Unset TMPDIR for tempname
Browse files Browse the repository at this point in the history
  • Loading branch information
vchuravy committed Jan 8, 2021
1 parent 5c6e21e commit 4a1df33
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 10 additions & 4 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -581,10 +581,16 @@ else # !windows
# Obtain a temporary filename.
function tempname(parent::AbstractString=tempdir(); cleanup::Bool=true)
isdir(parent) || throw(ArgumentError("$(repr(parent)) is not a directory"))
p = ccall(:tempnam, Cstring, (Cstring, Cstring), parent, temp_prefix)
systemerror(:tempnam, p == C_NULL)
s = unsafe_string(p)
Libc.free(p)
# If TMPDIR is set libc's `tempnam` will ignore `parent` as an argument (#38873)
# So we unset the environment variable for the call to libc, after checking
# in `tempdir` for the default argument.
s = withenv("TMPDIR" => nothing) do
p = ccall(:tempnam, Cstring, (Cstring, Cstring), parent, temp_prefix)
systemerror(:tempnam, p == C_NULL)
s = unsafe_string(p)
Libc.free(p)
return s
end
cleanup && temp_cleanup_later(s)
return s
end
Expand Down
8 changes: 8 additions & 0 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ end
t = tempname(d)
@test dirname(t) == d
end
# 38873: check that `TMPDIR` being set does not
# override the parent argument to `tempname`.
mktempdir() do d
whithenv("TMPDIR"=>tmpdir()) do
t = tempname(d)
@test dirname(t) == d
end
end
@test_throws ArgumentError tempname(randstring())
end

Expand Down

0 comments on commit 4a1df33

Please sign in to comment.