Normalize PWD environment variable before using it

In commit 5aed3ee49d (cmSystemTools: Add GetLogicalWorkingDirectory,
2024-10-28, v4.0.0-rc1~528^2~6) we incorrectly trusted `PWD` to be a
normalized path so long as its realpath matches the current working
directory.

Fixes: #26870
This commit is contained in:
Brad King
2025-04-15 08:41:36 -04:00
parent 813f6c263e
commit cd4e72ca08
3 changed files with 34 additions and 8 deletions

View File

@@ -3007,10 +3007,11 @@ std::string InitLogicalWorkingDirectory()
{
std::string cwd = cmsys::SystemTools::GetCurrentWorkingDirectory();
std::string pwd;
if (cmSystemTools::GetEnv("PWD", pwd)) {
if (cmSystemTools::GetEnv("PWD", pwd) &&
cmSystemTools::FileIsFullPath(pwd)) {
std::string const pwd_real = cmSystemTools::GetRealPath(pwd);
if (pwd_real == cwd) {
cwd = std::move(pwd);
cwd = cmSystemTools::ToNormalizedPathOnDisk(std::move(pwd));
}
}
return cwd;