From: Daira Hopwood <daira@jacaranda.org>
Date: Tue, 3 Feb 2015 23:47:31 +0000 (+0000)
Subject: More robust error handling in windows_getenv. refs #1674
X-Git-Tag: allmydata-tahoe-1.10.1a1~77^2~2
X-Git-Url: https://git.rkrishnan.org/%5B/%5D%20/uri/simplejson/...?a=commitdiff_plain;h=d756ef1765ff1abf4d4905cae142effa058e175b;p=tahoe-lafs%2Ftahoe-lafs.git

More robust error handling in windows_getenv. refs #1674

Signed-off-by: Daira Hopwood <daira@jacaranda.org>
---

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