Module: Mesa
Branch: main
Commit: bed69133cda5bb6e29beacd61a665ef653d4d1f9
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=bed69133cda5bb6e29beacd61a665ef653d4d1f9

Author: Jesse Natalie <jenat...@microsoft.com>
Date:   Mon Dec 18 10:04:46 2023 -0800

util: Re-implement getenv for Windows

On Windows, the C runtime maintains an environment variable cache for
getenv. But apps and drivers are free to statically link the C runtime,
so you might get different environment variable caches between components.
Specifically, a test trying to putenv to update the environment won't have
its update reflected by the driver if the CRT is statically linked, unless
we go to the Win32 API directly.

Reviewed-by: Sil Vilerino <sivil...@microsoft.com>
Reviewed-by: Yonggang Luo <luoyongg...@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/26744>

---

 src/util/os_misc.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/src/util/os_misc.c b/src/util/os_misc.c
index 02286121542..7261577488f 100644
--- a/src/util/os_misc.c
+++ b/src/util/os_misc.c
@@ -175,6 +175,22 @@ os_get_android_option(const char *name)
 }
 #endif
 
+#if DETECT_OS_WINDOWS
+
+/* getenv doesn't necessarily reflect changes to the environment
+ * that have been made during the process lifetime, if either the
+ * setter uses a different CRT (e.g. due to static linking) or the
+ * setter used the Win32 API directly. */
+const char *
+os_get_option(const char *name)
+{
+   static thread_local char value[_MAX_ENV];
+   DWORD size = GetEnvironmentVariableA(name, value, _MAX_ENV);
+   return (size > 0 && size < _MAX_ENV) ? value : NULL;
+}
+
+#else
+
 const char *
 os_get_option(const char *name)
 {
@@ -187,6 +203,8 @@ os_get_option(const char *name)
    return opt;
 }
 
+#endif
+
 static struct hash_table *options_tbl;
 static bool options_tbl_exited = false;
 static simple_mtx_t options_tbl_mtx = SIMPLE_MTX_INITIALIZER;

Reply via email to