Skip to content

Commit e09066f

Browse files
vtjnashKristofferC
authored andcommitted
handle lack of uv_os_get_passwd (#34255)
On some configurations, getpwuid_r might be unavailable for the current user. Work around that by checking return values and ensuring HOME is set for various tests.
1 parent 3dc498e commit e09066f

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

src/runtime_ccall.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,21 @@ std::string jl_format_filename(StringRef output_pattern)
136136
for (auto c : output_pattern) {
137137
if (special) {
138138
if (!got_pwd && (c == 'i' || c == 'd' || c == 'u')) {
139-
uv_os_get_passwd(&pwd);
140-
got_pwd = true;
139+
int r = uv_os_get_passwd(&pwd);
140+
if (r == 0)
141+
got_pwd = true;
141142
}
142143
switch (c) {
143144
case 'p':
144145
outfile << jl_getpid();
145146
break;
146147
case 'd':
147-
outfile << pwd.homedir;
148+
if (got_pwd)
149+
outfile << pwd.homedir;
148150
break;
149151
case 'i':
150-
outfile << pwd.uid;
152+
if (got_pwd)
153+
outfile << pwd.uid;
151154
break;
152155
case 'l':
153156
case 'L':
@@ -163,7 +166,8 @@ std::string jl_format_filename(StringRef output_pattern)
163166
#endif
164167
break;
165168
case 'u':
166-
outfile << pwd.username;
169+
if (got_pwd)
170+
outfile << pwd.username;
167171
break;
168172
default:
169173
outfile << c;

test/cmdlineargs.jl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ end
3434
let
3535
fn = format_filename("a%d %p %i %L %l %u z")
3636
hd = withenv("HOME" => nothing) do
37-
homedir()
37+
# get the homedir, as reported by uv_os_get_passwd, as used by jl_format_filename
38+
try
39+
homedir()
40+
catch ex
41+
(ex isa Base.IOError && ex.code == Base.UV_ENOENT) || rethrow(ex)
42+
""
43+
end
3844
end
3945
@test startswith(fn, "a$hd ")
4046
@test endswith(fn, " z")
@@ -48,13 +54,17 @@ let exename = `$(Base.julia_cmd()) --startup-file=no`
4854
# tests for handling of ENV errors
4955
let v = writereadpipeline("println(\"REPL: \", @which(less), @isdefined(InteractiveUtils))",
5056
setenv(`$exename -i -E 'empty!(LOAD_PATH); @isdefined InteractiveUtils'`,
51-
"JULIA_LOAD_PATH"=>"", "JULIA_DEPOT_PATH"=>""))
57+
"JULIA_LOAD_PATH" => "",
58+
"JULIA_DEPOT_PATH" => "",
59+
"HOME" => homedir()))
5260
@test v[1] == "false\nREPL: InteractiveUtilstrue\n"
5361
@test v[2]
5462
end
5563
let v = writereadpipeline("println(\"REPL: \", InteractiveUtils)",
5664
setenv(`$exename -i -e 'const InteractiveUtils = 3'`,
57-
"JULIA_LOAD_PATH"=>";;;:::", "JULIA_DEPOT_PATH"=>";;;:::"))
65+
"JULIA_LOAD_PATH" => ";;;:::",
66+
"JULIA_DEPOT_PATH" => ";;;:::",
67+
"HOME" => homedir()))
5868
# TODO: ideally, `@which`, etc. would still work, but Julia can't handle `using $InterativeUtils`
5969
@test v[1] == "REPL: 3\n"
6070
@test v[2]
@@ -71,13 +81,13 @@ let exename = `$(Base.julia_cmd()) --startup-file=no`
7181
end
7282
real_threads = string(ccall(:jl_cpu_threads, Int32, ()))
7383
for nc in ("0", "-2", "x", "2x", " ", "")
74-
v = readchomperrors(setenv(`$exename -i -E 'Sys.CPU_THREADS'`, "JULIA_CPU_THREADS" => nc))
84+
v = readchomperrors(setenv(`$exename -i -E 'Sys.CPU_THREADS'`, "JULIA_CPU_THREADS" => nc, "HOME" => homedir()))
7585
@test v[1]
7686
@test v[2] == real_threads
7787
@test v[3] == "WARNING: couldn't parse `JULIA_CPU_THREADS` environment variable. Defaulting Sys.CPU_THREADS to $real_threads."
7888
end
7989
for nc in ("1", " 1 ", " +1 ", " 0x1 ")
80-
v = readchomperrors(setenv(`$exename -i -E 'Sys.CPU_THREADS'`, "JULIA_CPU_THREADS" => nc))
90+
v = readchomperrors(setenv(`$exename -i -E 'Sys.CPU_THREADS'`, "JULIA_CPU_THREADS" => nc, "HOME" => homedir()))
8191
@test v[1]
8292
@test v[2] == "1"
8393
@test isempty(v[3])
@@ -101,7 +111,7 @@ let exename = `$(Base.julia_cmd()) --startup-file=no`
101111
if !Sys.iswindows()
102112
expanded = abspath(expanduser("~/foo"))
103113
@test occursin(expanded, readchomp(`$exename --project='~/foo' -E 'Base.active_project()'`))
104-
@test occursin(expanded, readchomp(setenv(`$exename -E 'Base.active_project()'`, "JULIA_PROJECT"=>"~/foo")))
114+
@test occursin(expanded, readchomp(setenv(`$exename -E 'Base.active_project()'`, "JULIA_PROJECT" => "~/foo", "HOME" => homedir())))
105115
end
106116

107117
# --quiet, --banner

0 commit comments

Comments
 (0)