Skip to contentSkip to conversation

libtraceevent: Fix leaks and NULL check in process_int_dynamic_array()

PATCH New Ian Rogers 1 participant 1 message Sep 28

lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH 1/1 libtraceevent: Fix leaks and NULL check in process_int_dynamic_array()

process_int_dynamic_array() reads the name of the array's field into token, but only frees it on the "out" error path, so each __print_dynamic_array() parsed leaks the name. The kernel's syscall events use it for user space buffers, such as write's buf. The early return, when the first argument isn't a name, leaks the token too, and unlike the other error path it doesn't set *tok to NULL.

Also check the result of alloc_arg() rather than arg, which can't be NULL, so that an allocation failure is reported rather than dereferenced.

Found with LeakSanitizer running perf trace.

Fixes 3f9655198727 ("libtraceevent: Add support for __print_dynamic_array() macro")
Assisted-by Antigravity:gemini-3.1-pro
---
 src/event-parse.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
src/event-parse.c +6 -2
index 4bd5c1b..14e1b91 100644
@@ -3473,8 +3473,11 @@ process_int_dynamic_array(struct tep_event *event, struct tep_print_arg *arg, ch
 	 * the index into where the array starts.
 	 */
 	type = read_token(event->tep, &token);
-	if (type != TEP_EVENT_ITEM)
+	if (type != TEP_EVENT_ITEM) {
+		free_token(token);
+		*tok = NULL;
 		return TEP_EVENT_ERROR;
+	}
 
 	/* Find the field */
 	field = tep_find_field(event, token);
@@ -3490,7 +3493,7 @@ process_int_dynamic_array(struct tep_event *event, struct tep_print_arg *arg, ch
 		goto out;
 
 	arg->int_array.field = alloc_arg();
-	if (!arg) {
+	if (!arg->int_array.field) {
 		do_warning_event(event, "%s: not enough memory!", __func__);
 		goto out;
 	}
@@ -3499,6 +3502,7 @@ process_int_dynamic_array(struct tep_event *event, struct tep_print_arg *arg, ch
 	arg->int_array.field->dynarray.field = field;
 	arg->int_array.field->dynarray.index = 0;
 
+	free_token(token);
 	return read_token_item(event->tep, tok);
 out:
 	free_token(token);

base-commit: 51d47b0eb2b40d4273fb461782fe9a01db0f901c

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog