libct/nsenter: nullify pointer on asprintf error

The contents of the pointer returned on asprintf() error are undefined
i.e., it can be anything there. We set it to NULL on error so that
free() afterwards won't get a garbage pointer.

This patch applies the above to message and stage as well to be
consistent with what we do for json.

Signed-off-by: Kailun Qin <kailun.qin@intel.com>
This commit is contained in:
Kailun Qin
2021-08-14 04:15:45 -04:00
parent bb34048f93
commit 515082102e
+6 -2
View File
@@ -146,8 +146,10 @@ static void write_log(const char *level, const char *format, ...)
va_start(args, format);
ret = vasprintf(&message, format, args);
va_end(args);
if (ret < 0)
if (ret < 0) {
message = NULL;
goto out;
}
message = escape_json_string(message);
@@ -155,8 +157,10 @@ static void write_log(const char *level, const char *format, ...)
stage = strdup("nsexec");
else
ret = asprintf(&stage, "nsexec-%d", current_stage);
if (ret < 0)
if (ret < 0) {
stage = NULL;
goto out;
}
ret = asprintf(&json, "{\"level\":\"%s\", \"msg\": \"%s[%d]: %s\"}\n", level, stage, getpid(), message);
if (ret < 0) {