From: Mathieu Desnoyers Date: Tue, 20 Mar 2012 17:15:01 +0000 (-0400) Subject: Fix kernel_list_events memleaks X-Git-Tag: v2.0.0-rc4~13^2~1 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=3b870559a62e8f74a8793a6a23b9445346fe9fdf;p=lttng-tools.git Fix kernel_list_events memleaks - The fscanf %m allocation was leaking the "event" (needs to be freed explicitly). - The error handling of realloc should free the previous pointer. Acked-by: David Goulet Signed-off-by: Mathieu Desnoyers --- diff --git a/src/bin/lttng-sessiond/kernel.c b/src/bin/lttng-sessiond/kernel.c index b1ff2ec48..1aad5263e 100644 --- a/src/bin/lttng-sessiond/kernel.c +++ b/src/bin/lttng-sessiond/kernel.c @@ -582,24 +582,35 @@ ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events) */ nbmem = KERNEL_EVENT_INIT_LIST_SIZE; elist = zmalloc(sizeof(struct lttng_event) * nbmem); + if (elist == NULL) { + PERROR("alloc list events"); + count = -ENOMEM; + goto end; + } while ((size = fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) { if (count >= nbmem) { + struct lttng_event *new_elist; + DBG("Reallocating event list from %zu to %zu bytes", nbmem, nbmem * 2); /* Double the size */ nbmem <<= 1; - elist = realloc(elist, nbmem * sizeof(struct lttng_event)); - if (elist == NULL) { + new_elist = realloc(elist, nbmem * sizeof(struct lttng_event)); + if (new_elist == NULL) { PERROR("realloc list events"); + free(event); + free(elist); count = -ENOMEM; goto end; } + elist = new_elist; } strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN); elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; elist[count].enabled = -1; count++; + free(event); } *events = elist;