Skip to content

Commit

Permalink
Fixed handling of getenv results
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx committed Nov 17, 2023
1 parent 0b8468f commit fead7e7
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/tests/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,13 @@ TEST_F(rnp_tests, test_cli_dump)

TEST_F(rnp_tests, test_cli_logname)
{
// !!!!!
// getenv function is not required to be thread-safe.
// Another call to getenv, as well as a call to the POSIX functions setenv(), unsetenv(), and putenv()
// may invalidate the pointer returned by a previous call or modify the string obtained from a previous call.
char * logname = getenv("LOGNAME");
std::string safe_logname(logname ? logname : "");

char * user = getenv("USER");
std::string testname(user ? user : "user");
testname.append("-test-user");
Expand All @@ -851,7 +857,7 @@ TEST_F(rnp_tests, test_cli_logname)
}

if (logname) {
setenv("LOGNAME", logname, 1);
setenv("LOGNAME", safe_logname.c_str(), 1);
} else {
unsetenv("LOGNAME");
}
Expand Down

0 comments on commit fead7e7

Please sign in to comment.