From de36fed5552ea464b047ac598fbb28faaea14555 Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Wed, 29 Oct 2025 15:09:25 +0800 Subject: [PATCH] util: Add new function os_get_option_internal to improve os_get_option* This is for take care of GetEnvironmentVariableA/getenv/secure_getenv/os_get_android_option consistently across windows/posix/android And also secure_getenv should not directly used in mesa, remove it from u_debug.h os_get_option_secure is using getenv for windows before, that also pull the drawback of getenv, so convert it also using GetEnvironmentVariableA on windows instead. This is done the same as commit bed69133cda5bb6e29beacd61a665ef653d4d1f9 for os_get_option_secure Signed-off-by: Yonggang Luo Reviewed-by: Yiwei Zhang Acked-by: Antonio Ospite Part-of: --- src/util/os_misc.c | 37 +++++++++++++++++++++++-------------- src/util/u_debug.h | 7 ------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/util/os_misc.c b/src/util/os_misc.c index 473f6193d6c..86b62774f10 100644 --- a/src/util/os_misc.c +++ b/src/util/os_misc.c @@ -200,20 +200,29 @@ os_get_android_option(const char *name) * 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 const char * +os_get_option_internal(const char *name, UNUSED bool use_secure_getenv) { static thread_local char value[_MAX_ENV]; DWORD size = GetEnvironmentVariableA(name, value, _MAX_ENV); return (size > 0 && size < _MAX_ENV) ? value : NULL; } -#else +#else /* !DETECT_OS_WINDOWS */ -const char * -os_get_option(const char *name) +static const char * +os_get_option_internal(const char *name, bool use_secure_getenv) { - const char *opt = getenv(name); + const char *opt; + if (use_secure_getenv) { +#ifdef HAVE_SECURE_GETENV + opt = secure_getenv(name); +#else + opt = getenv(name); +#endif + } else { + opt = getenv(name); + } #if DETECT_OS_ANDROID if (!opt) { opt = os_get_android_option(name); @@ -222,18 +231,18 @@ os_get_option(const char *name) return opt; } -#endif +#endif /* DETECT_OS_WINDOWS */ + +const char * +os_get_option(const char *name) +{ + return os_get_option_internal(name, false); +} const char * os_get_option_secure(const char *name) { - const char *opt = secure_getenv(name); -#if DETECT_OS_ANDROID - if (!opt) { - opt = os_get_android_option(name); - } -#endif - return opt; + return os_get_option_internal(name, true); } static struct hash_table *options_tbl; diff --git a/src/util/u_debug.h b/src/util/u_debug.h index 50b45f51a5f..11eea0a8c1d 100644 --- a/src/util/u_debug.h +++ b/src/util/u_debug.h @@ -409,13 +409,6 @@ __normal_user(void) #endif } -#ifndef HAVE_SECURE_GETENV -static inline char *secure_getenv(const char *name) -{ - return getenv(name); -} -#endif - #define DEBUG_GET_ONCE_BOOL_OPTION(sufix, name, dfault) \ static bool \ debug_get_option_ ## sufix (void) \