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 Dec 14, 2020
1 parent 6c95660 commit 9d0d599
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 8 additions & 4 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -578,10 +578,14 @@ 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 TEMPDIR is set tempname will ignore `parent` as an argument (#38873)
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 9d0d599

Please sign in to comment.