Commit 4e58e355 authored by Mark OLESEN's avatar Mark OLESEN Committed by Andrew Heather
Browse files

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

parent 83454524
......@@ -350,12 +350,16 @@ Foam::string Foam::getEnv(const std::string& envName)
{
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)
{
env.resize(len+1);
::GetEnvironmentVariable(envName.c_str(), &(env[0]), len+1);
env.resize(len);
// 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);
return fileName::validate(env);
......@@ -442,16 +446,18 @@ Foam::fileName Foam::home(const std::string& userName)
Foam::fileName Foam::cwd()
{
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)
{
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);
return fileName::validate(path);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment