diff --git a/src/util/os_misc.c b/src/util/os_misc.c index 86b62774f10..e7ebfcba147 100644 --- a/src/util/os_misc.c +++ b/src/util/os_misc.c @@ -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; diff --git a/src/util/os_misc.h b/src/util/os_misc.h index 2a5c19659a9..4d2615ab242 100644 --- a/src/util/os_misc.h +++ b/src/util/os_misc.h @@ -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