[Python-checkins] cpython: Use GetEnvironmentVariableW instead of _wgetenv to silence VC warnings.

brian.curtin python-checkins at python.org
Fri Jun 22 00:13:05 CEST 2012


http://hg.python.org/cpython/rev/a71bf682235e
changeset:   77548:a71bf682235e
user:        Martin v. Löwis <martin at v.loewis.de>
date:        Thu Jun 21 16:33:09 2012 +0200
summary:
  Use GetEnvironmentVariableW instead of _wgetenv to silence VC warnings.

files:
  PC/launcher.c |  27 ++++++++++++++++++---------
  1 files changed, 18 insertions(+), 9 deletions(-)


diff --git a/PC/launcher.c b/PC/launcher.c
--- a/PC/launcher.c
+++ b/PC/launcher.c
@@ -54,22 +54,31 @@
 }
 
 /*
- * This function is here to minimise Visual Studio
- * warnings about security implications of getenv, and to
- * treat blank values as if they are absent.
+ * This function is here to simplify memory management
+ * and to treat blank values as if they are absent.
  */
 static wchar_t * get_env(wchar_t * key)
 {
-    wchar_t * result = _wgetenv(key);
+    /* This is not thread-safe, just like getenv */
+    static wchar_t buf[256];
+    DWORD result = GetEnvironmentVariableW(key, buf, 256);
 
-    if (result) {
-        result = skip_whitespace(result);
-        if (*result == L'\0')
-            result = NULL;
+    if (result > 256) {
+        /* Large environment variable. Accept some leakage */
+        wchar_t *buf2 = (wchar_t*)malloc(sizeof(wchar_t) * (result+1));
+        GetEnvironmentVariableW(key, buf2, result);
+        return buf2;
     }
-    return result;
+
+    if (result == 0)
+        /* Either some error, e.g. ERROR_ENVVAR_NOT_FOUND,
+           or an empty environment variable. */
+        return NULL;
+
+    return buf;
 }
 
+
 static void
 debug(wchar_t * format, ...)
 {

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list