]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
More robust error handling in windows_getenv. refs #1674
authorDaira Hopwood <daira@jacaranda.org>
Tue, 3 Feb 2015 23:47:31 +0000 (23:47 +0000)
committerDaira Hopwood <daira@jacaranda.org>
Tue, 3 Feb 2015 23:47:53 +0000 (23:47 +0000)
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
src/allmydata/util/fileutil.py

index 45b74063831ba445d3604c887178bf96b659164b..7bb25e8e7538d18949793d98d020326cfda1811b 100644 (file)
@@ -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