Skip to content
Snippets Groups Projects
Commit 2827a57b authored by Mark OLESEN's avatar Mark OLESEN
Browse files

BUG: off-by-one in Windows env and cwd sizing (#1238)

parent 03583567
No related branches found
No related tags found
No related merge requests found
...@@ -350,12 +350,16 @@ Foam::string Foam::getEnv(const std::string& envName) ...@@ -350,12 +350,16 @@ Foam::string Foam::getEnv(const std::string& envName)
{ {
std::string env; std::string env;
const auto len = ::GetEnvironmentVariable(envName.c_str(), nullptr, 0); auto len = ::GetEnvironmentVariable(envName.c_str(), nullptr, 0);
// len [return] = size with trailing nul char, or zero on failure
if (len) if (len)
{ {
env.resize(len+1); env.resize(len);
::GetEnvironmentVariable(envName.c_str(), &(env[0]), len+1);
// len [in] = size with trailing nul char
// len [return] = size without trailing nul char
len = ::GetEnvironmentVariable(envName.c_str(), &(env[0]), len);
env.resize(len); env.resize(len);
return fileName::validate(env); return fileName::validate(env);
...@@ -442,16 +446,18 @@ Foam::fileName Foam::home(const std::string& userName) ...@@ -442,16 +446,18 @@ Foam::fileName Foam::home(const std::string& userName)
Foam::fileName Foam::cwd() Foam::fileName Foam::cwd()
{ {
string path; string path;
const DWORD len = ::GetCurrentDirectory(0, nullptr); auto len = ::GetCurrentDirectory(0, nullptr);
// len [return] = size with trailing nul char, or zero on failure
if (len) if (len)
{ {
path.resize(len+1); path.resize(len);
::GetCurrentDirectory(len+1, &(path[0])); // len [in] = size with trailing nul char
// len [return] = size without trailing nul char
len = ::GetCurrentDirectory(len, &(path[0]));
path.resize(len); path.resize(len);
return fileName::validate(path); return fileName::validate(path);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment