util: Add function os_get_option_dup and os_get_option_secure_dup for latter use

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
Reviewed-by: Yiwei Zhang <zzyiwei@chromium.org>
Acked-by: Antonio Ospite <antonio.ospite@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/38128>
This commit is contained in:
Yonggang Luo 2025-10-29 15:54:13 +08:00 committed by Marge Bot
parent de36fed555
commit eeb54aa92d
2 changed files with 40 additions and 0 deletions

View file

@ -239,12 +239,32 @@ os_get_option(const char *name)
return os_get_option_internal(name, false);
}
char *
os_get_option_dup(const char *name)
{
const char *opt = os_get_option_internal(name, false);
if (opt) {
return strdup(opt);
}
return NULL;
}
const char *
os_get_option_secure(const char *name)
{
return os_get_option_internal(name, true);
}
char *
os_get_option_secure_dup(const char *name)
{
const char *opt = os_get_option_internal(name, true);
if (opt) {
return strdup(opt);
}
return NULL;
}
static struct hash_table *options_tbl;
static bool options_tbl_exited = false;
static simple_mtx_t options_tbl_mtx = SIMPLE_MTX_INITIALIZER;

View file

@ -90,6 +90,16 @@ os_log_message(const char *message);
const char *
os_get_option(const char *name);
/*
* Equivalent to os_get_option except the return value need to be `free()`
* os_get_option is not safe when the returned value is not immediately used.
* E.g.
* 1. when multiple consecutive calls to os_get_option are performed before using the returned values
* 2. when the value returned by os_get_option is assigned to a struct member
*/
char *
os_get_option_dup(const char *name);
/*
* Get an option. Should return NULL if specified option is not set.
* Same as `os_get_option()` but uses `secure_getenv()` instead of `getenv()`
@ -97,6 +107,16 @@ os_get_option(const char *name);
const char *
os_get_option_secure(const char *name);
/*
* Equivalent to os_get_option_secure except the return value need to be `free()`
* os_get_option_secure is not safe when the returned value is not immediately used.
* E.g.
* 1. when multiple consecutive calls to os_get_option_secure are performed before using the returned values
* 2. when the value returned by os_get_option_secure is assigned to a struct member
*/
char *
os_get_option_secure_dup(const char *name);
/*
* Get an option. Should return NULL if specified option is not set.
* It's will save the option into hash table for the first time, and