From 9d0d599dbedb90810285e7a082a2c64b2fb00573 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Mon, 14 Dec 2020 16:12:14 -0500 Subject: [PATCH] Unset TMPDIR for tempname --- base/file.jl | 12 ++++++++---- test/file.jl | 8 ++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/base/file.jl b/base/file.jl index d76e81f62364e8..37cbbc01a6ba0a 100644 --- a/base/file.jl +++ b/base/file.jl @@ -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 diff --git a/test/file.jl b/test/file.jl index 2a2de0d7932159..50859742445b7b 100644 --- a/test/file.jl +++ b/test/file.jl @@ -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