From d756ef1765ff1abf4d4905cae142effa058e175b Mon Sep 17 00:00:00 2001 From: Daira Hopwood Date: Tue, 3 Feb 2015 23:47:31 +0000 Subject: [PATCH] More robust error handling in windows_getenv. refs #1674 Signed-off-by: Daira Hopwood --- src/allmydata/util/fileutil.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/allmydata/util/fileutil.py b/src/allmydata/util/fileutil.py index 45b74063..7bb25e8e 100644 --- a/src/allmydata/util/fileutil.py +++ b/src/allmydata/util/fileutil.py @@ -408,17 +408,23 @@ def windows_getenv(name): raise AssertionError("name must be Unicode") n = GetEnvironmentVariableW(name, None, 0) - if n <= 0: + if n == 0: err = GetLastError() - raise OSError("Windows error %d attempting to read environment variable %r" + raise OSError("Windows error %d attempting to read size of environment variable %r" % (err, name)) + elif n < 0: + raise OSError("Unexpected result %d from GetEnvironmentVariableW attempting to read size of environment variable %r" + % (n, name)) buf = create_unicode_buffer(u'\0'*n) retval = GetEnvironmentVariableW(name, buf, n) - if retval <= 0: + if retval == 0: err = GetLastError() raise OSError("Windows error %d attempting to read environment variable %r" % (err, name)) + elif retval != n-1: + raise OSError("Unexpected result %d from GetEnvironmentVariableW attempting to read environment variable %r" + % (n, name)) return buf.value -- 2.45.2