The open source OpenXR runtime

u/debug: Do not save pointer returned from getenv

+44 -17
+32 -15
src/xrt/auxiliary/util/u_debug.c
··· 18 18 19 19 #include <ctype.h> 20 20 #include <stdio.h> 21 - #include <assert.h> 22 21 #include <stdlib.h> 23 22 #include <string.h> 24 23 ··· 33 32 */ 34 33 35 34 static const char * 36 - os_getenv(const char *name) 35 + get_option_raw(char *chars, size_t char_count, const char *name) 37 36 { 38 - return getenv(name); 37 + const char *raw = getenv(name); 38 + 39 + if (raw == NULL) { 40 + return NULL; 41 + } else { 42 + snprintf(chars, char_count, "%s", raw); 43 + return chars; 44 + } 39 45 } 40 46 41 47 static const char * ··· 211 217 */ 212 218 213 219 const char * 214 - debug_get_option(const char *name, const char *_default) 220 + debug_get_option(char *chars, size_t char_count, const char *name, const char *_default) 215 221 { 216 - const char *raw = os_getenv(name); 217 - const char *ret; 222 + const char *raw = get_option_raw(chars, char_count, name); 223 + const char *ret = raw; 218 224 219 - if (raw == NULL) { 220 - ret = _default; 221 - } else { 222 - ret = raw; 225 + if (ret == NULL) { 226 + if (_default != NULL) { 227 + snprintf(chars, char_count, "%s", _default); 228 + ret = chars; // Return a value. 229 + } 223 230 } 224 231 225 232 if (debug_get_bool_option_print()) { ··· 232 239 bool 233 240 debug_get_bool_option(const char *name, bool _default) 234 241 { 235 - const char *raw = os_getenv(name); 242 + char chars[DEBUG_CHAR_STORAGE_SIZE]; 243 + const char *raw = get_option_raw(chars, ARRAY_SIZE(chars), name); 244 + 236 245 bool ret = raw == NULL ? _default : debug_string_to_bool(raw); 237 246 238 247 if (debug_get_bool_option_print()) { ··· 245 254 enum debug_tristate_option 246 255 debug_get_tristate_option(const char *name) 247 256 { 248 - const char *raw = os_getenv(name); 257 + char chars[DEBUG_CHAR_STORAGE_SIZE]; 258 + const char *raw = get_option_raw(chars, ARRAY_SIZE(chars), name); 259 + 249 260 enum debug_tristate_option ret = debug_string_to_tristate(raw); 250 261 251 262 if (debug_get_bool_option_print()) { ··· 259 270 long 260 271 debug_get_num_option(const char *name, long _default) 261 272 { 262 - const char *raw = os_getenv(name); 273 + char chars[DEBUG_CHAR_STORAGE_SIZE]; 274 + const char *raw = get_option_raw(chars, ARRAY_SIZE(chars), name); 275 + 263 276 long ret = debug_string_to_num(raw, _default); 264 277 265 278 if (debug_get_bool_option_print()) { ··· 272 285 float 273 286 debug_get_float_option(const char *name, float _default) 274 287 { 275 - const char *raw = os_getenv(name); 288 + char chars[DEBUG_CHAR_STORAGE_SIZE]; 289 + const char *raw = get_option_raw(chars, ARRAY_SIZE(chars), name); 290 + 276 291 float ret = debug_string_to_float(raw, _default); 277 292 278 293 if (debug_get_bool_option_print()) { ··· 285 300 enum u_logging_level 286 301 debug_get_log_option(const char *name, enum u_logging_level _default) 287 302 { 288 - const char *raw = os_getenv(name); 303 + char chars[DEBUG_CHAR_STORAGE_SIZE]; 304 + const char *raw = get_option_raw(chars, ARRAY_SIZE(chars), name); 305 + 289 306 enum u_logging_level ret = debug_string_to_log_level(raw, _default); 290 307 291 308 if (debug_get_bool_option_print()) {
+12 -2
src/xrt/auxiliary/util/u_debug.h
··· 19 19 extern "C" { 20 20 #endif 21 21 22 + 23 + /* 24 + * 25 + * Definitions. 26 + * 27 + */ 28 + 29 + #define DEBUG_CHAR_STORAGE_SIZE (1024) 30 + 22 31 enum debug_tristate_option 23 32 { 24 33 DEBUG_TRISTATE_OFF, ··· 56 65 */ 57 66 58 67 const char * 59 - debug_get_option(const char *name, const char *_default); 68 + debug_get_option(char *chars, size_t char_count, const char *name, const char *_default); 60 69 61 70 bool 62 71 debug_get_bool_option(const char *name, bool _default); ··· 86 95 #define DEBUG_GET_ONCE_OPTION(suffix, name, _default) \ 87 96 static const char *debug_get_option_##suffix(void) \ 88 97 { \ 98 + static char storage[DEBUG_CHAR_STORAGE_SIZE]; \ 89 99 static bool gotten = false; \ 90 100 static const char *stored; \ 91 101 if (!gotten) { \ 92 102 gotten = true; \ 93 - stored = debug_get_option(name, _default); \ 103 + stored = debug_get_option(storage, ARRAY_SIZE(storage), name, _default); \ 94 104 } \ 95 105 return stored; \ 96 106 }