From d1892e1a33645be2d173b8d92808664fb4f6bbce Mon Sep 17 00:00:00 2001 From: Jonathan Rajotte Date: Tue, 19 Oct 2021 15:22:39 -0400 Subject: [PATCH] Fix: lttng-ctl: tracing_group memory leaks MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Observed issue ============== liblttng-ctl leaks memory if `lttng_set_tracing_group` is called at least 1 time by an API client. joraj@~/lttng/master/lttng-tools-dev [master][]$ valgrind --leak-check=full lttng --group=joraj list ==24823== Memcheck, a memory error detector ==24823== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==24823== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info ==24823== Command: lttng --group=joraj list ==24823== Error: No session daemon is available ==24823== ==24823== HEAP SUMMARY: ==24823== in use at exit: 8 bytes in 1 blocks ==24823== total heap usage: 55 allocs, 54 frees, 87,023 bytes allocated ==24823== ==24823== 8 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==24823== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) ==24823== by 0x4BA7DC7: __vasprintf_internal (vasprintf.c:71) ==24823== by 0x4C4B742: __asprintf_chk (asprintf_chk.c:34) ==24823== by 0x48687D9: asprintf (stdio2.h:181) ==24823== by 0x48687D9: lttng_set_tracing_group (lttng-ctl.c:2620) ==24823== by 0x4011B89: call_init.part.0 (dl-init.c:72) ==24823== by 0x4011C90: call_init (dl-init.c:30) ==24823== by 0x4011C90: _dl_init (dl-init.c:119) ==24823== by 0x4001139: ??? (in /usr/lib/x86_64-linux-gnu/ld-2.31.so) ==24823== by 0x2: ??? ==24823== by 0x1FFEFFFCFE: ??? ==24823== by 0x1FFEFFFD04: ??? ==24823== by 0x1FFEFFFD12: ??? ==24823== ==24823== LEAK SUMMARY: ==24823== definitely lost: 8 bytes in 1 blocks ==24823== indirectly lost: 0 bytes in 0 blocks ==24823== possibly lost: 0 bytes in 0 blocks ==24823== still reachable: 0 bytes in 0 blocks ==24823== suppressed: 0 bytes in 0 blocks ==24823== ==24823== For lists of detected and suppressed errors, rerun with: -s ==24823== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) Cause ===== The allocated pointer in the library constructor is not freed on subsequent assignation. Solution ======== Free the pointer. Signed-off-by: Jonathan Rajotte Signed-off-by: Jérémie Galarneau Change-Id: Ie1d4c45df2764a88c74d56de691783df9215633c --- src/lib/lttng-ctl/lttng-ctl.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib/lttng-ctl/lttng-ctl.c b/src/lib/lttng-ctl/lttng-ctl.c index 7ad1fe132..bb30feff6 100644 --- a/src/lib/lttng-ctl/lttng-ctl.c +++ b/src/lib/lttng-ctl/lttng-ctl.c @@ -2618,14 +2618,19 @@ end: */ int lttng_set_tracing_group(const char *name) { + char *new_group; if (name == NULL) { return -LTTNG_ERR_INVALID; } - if (asprintf(&tracing_group, "%s", name) < 0) { + if (asprintf(&new_group, "%s", name) < 0) { return -LTTNG_ERR_FATAL; } + free(tracing_group); + tracing_group = new_group; + new_group = NULL; + return 0; } -- 2.34.1