Skip to content

Commit fc54bf6

Browse files
committed
trace2: remove use of xstrdup()
In the previous change, we removed a use of xsprintf() that caused a recursive die() loop when failing to allocate memory. The trace2 library is too low-level to be calling die(), especially because of these recursive loops that can occur during the die handler. For full defense in depth, we remove the xstrdup() calls from trace2/tr2_sysenv.c. First, in tr2_sysenv_cb(), we need to handle a failed assignment of the value with a negative return to halt the config parsing loop. Second, in tr2_sysenv_get(), the method will return NULL when strdup() returns NULL. This return is indistinguishable from the environment variable having no value. That means that all callers know how to handle a NULL response, but no behavior change will occur between the case of no environment being set and detecting an environment variable exists but we fail to duplicate it. This seems an appropriate trade-off, as an allocation failure at this level will likely lead to failure in another system, but at least the trace2 API will not cause the process to fail early. Signed-off-by: Derrick Stolee <stolee@gmail.com>
1 parent 832b4c6 commit fc54bf6

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

trace2/tr2_sysenv.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ static int tr2_sysenv_cb(const char *key, const char *value,
7373
if (!value)
7474
return config_error_nonbool(key);
7575
free(tr2_sysenv_settings[k].value);
76-
tr2_sysenv_settings[k].value = xstrdup(value);
76+
tr2_sysenv_settings[k].value = strdup(value);
77+
if (!tr2_sysenv_settings[k].value)
78+
return -1;
7779
return 0;
7880
}
7981
}
@@ -109,7 +111,7 @@ const char *tr2_sysenv_get(enum tr2_sysenv_variable var)
109111
const char *v = getenv(tr2_sysenv_settings[var].env_var_name);
110112
if (v && *v) {
111113
free(tr2_sysenv_settings[var].value);
112-
tr2_sysenv_settings[var].value = xstrdup(v);
114+
tr2_sysenv_settings[var].value = strdup(v);
113115
}
114116
tr2_sysenv_settings[var].getenv_called = 1;
115117
}

0 commit comments

Comments
 (0)