From dea152929c27a40dc2f422ad3937687cc0142f75 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Mon, 20 May 2019 17:41:27 +0100 Subject: [PATCH] BUG: off-by-one in Windows env and cwd sizing (#1238) --- src/OSspecific/MSwindows/MSwindows.C | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/OSspecific/MSwindows/MSwindows.C b/src/OSspecific/MSwindows/MSwindows.C index 30fb55792f4..4516c6a7fbe 100644 --- a/src/OSspecific/MSwindows/MSwindows.C +++ b/src/OSspecific/MSwindows/MSwindows.C @@ -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); } -- GitLab