Skip to content

Commit c5f8a40

Browse files
committed
Fix getUserName behavior
1 parent 7a49064 commit c5f8a40

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/libutil/users.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@ namespace nix {
1313
std::string getUserName(uid_t uid)
1414
{
1515
auto pw = getpwuid(uid);
16-
std::string name = pw ? pw->pw_name : getEnv("USER").value_or("");
17-
if (name.empty())
16+
if (!pw)
1817
throw Error("cannot figure out user name");
19-
return name;
18+
return pw->pw_name;
2019
}
2120

2221
std::string getUserName()
2322
{
24-
return getUserName(getuid());
23+
uid_t uid = getuid();
24+
if (getpwuid(uid))
25+
return getUserName(uid);
26+
else {
27+
if (auto name = getEnv("USER"))
28+
return *name;
29+
else
30+
throw Error("cannot figure our user name");
31+
}
2532
}
2633

2734
std::string getGroupName(gid_t gid)

0 commit comments

Comments
 (0)