Skip to content

Commit 9d0d599

Browse files
committed
Unset TMPDIR for tempname
1 parent 6c95660 commit 9d0d599

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

base/file.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,14 @@ else # !windows
578578
# Obtain a temporary filename.
579579
function tempname(parent::AbstractString=tempdir(); cleanup::Bool=true)
580580
isdir(parent) || throw(ArgumentError("$(repr(parent)) is not a directory"))
581-
p = ccall(:tempnam, Cstring, (Cstring, Cstring), parent, temp_prefix)
582-
systemerror(:tempnam, p == C_NULL)
583-
s = unsafe_string(p)
584-
Libc.free(p)
581+
# If TEMPDIR is set tempname will ignore `parent` as an argument (#38873)
582+
s = withenv("TMPDIR" => nothing) do
583+
p = ccall(:tempnam, Cstring, (Cstring, Cstring), parent, temp_prefix)
584+
systemerror(:tempnam, p == C_NULL)
585+
s = unsafe_string(p)
586+
Libc.free(p)
587+
return s
588+
end
585589
cleanup && temp_cleanup_later(s)
586590
return s
587591
end

test/file.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ end
7171
t = tempname(d)
7272
@test dirname(t) == d
7373
end
74+
# 38873: check that `TMPDIR` being set does not
75+
# override the parent argument to `tempname`.
76+
mktempdir() do d
77+
whithenv("TMPDIR"=>tmpdir()) do
78+
t = tempname(d)
79+
@test dirname(t) == d
80+
end
81+
end
7482
@test_throws ArgumentError tempname(randstring())
7583
end
7684

0 commit comments

Comments
 (0)