Skip to content

Commit 6aee4e0

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

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

base/file.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,16 @@ 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 TMPDIR is set libc's `tempnam` will ignore `parent` as an argument (#38873)
582+
# So we unset the environment variable for the call to libc, after checking
583+
# in `tempdir` for the default argument.
584+
s = withenv("TMPDIR" => nothing) do
585+
p = ccall(:tempnam, Cstring, (Cstring, Cstring), parent, temp_prefix)
586+
systemerror(:tempnam, p == C_NULL)
587+
s = unsafe_string(p)
588+
Libc.free(p)
589+
return s
590+
end
585591
cleanup && temp_cleanup_later(s)
586592
return s
587593
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)