From: Mathieu Desnoyers Date: Mon, 26 Sep 2022 17:36:38 +0000 (-0400) Subject: Fix: Handle empty string in lttng_event_field_value_string_create_with_size X-Git-Url: https://git.efficios.com/?a=commitdiff_plain;h=8ce733e56bbab1ca5507b868e8093f4cb5cc87b7;p=deliverable%2Flttng-tools.git Fix: Handle empty string in lttng_event_field_value_string_create_with_size When using the event notification capture API, empty strings are represented by a NULL pointer with a size=0 in the msgpack object. The NULL pointer is unexpected, which triggers an assertion in lttng_event_field_value_string_create_with_size. Fix this by duplicating an empty string ("") when a size=0 is encountered. This ensures that users of the API don't end up with an unexpected NULL pointer. Indeed, the sample program notif-app.c in the LTTng website documentation does not expect a NULL pointer. Signed-off-by: Mathieu Desnoyers Change-Id: I7c3a839dbbeeb95a1b3bf6ddc3205a2f6b4538e3 --- diff --git a/src/common/event-field-value.c b/src/common/event-field-value.c index b57c7c53e..15ff36f4b 100644 --- a/src/common/event-field-value.c +++ b/src/common/event-field-value.c @@ -190,8 +190,16 @@ struct lttng_event_field_value *lttng_event_field_value_string_create_with_size( goto error; } - assert(val); - field_val->val = strndup(val, size); + if (size) { + assert(val); + field_val->val = strndup(val, size); + } else { + /* + * User code do not expect a NULL string pointer. + * Populate with an empty string when length is 0. + */ + field_val->val = strdup(""); + } if (!field_val->val) { goto error; }