From bfe42dac12bfd6aba7159ee5935828a027468555 Mon Sep 17 00:00:00 2001 From: Francis Deslauriers Date: Mon, 6 May 2019 16:22:03 -0400 Subject: [PATCH] Fix: flt.lttng-utils.debug-info: memory leak MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The gelf_getshdr() function return a pointer to the second parameter on success and NULL on error. This means that on we overwrite the `curr_section_hdr` variable and leak memory. To fix it, we simply change the variable from a heap allocated variable to a stack allocated variable this has the effect of making code simpler. Found using scan-build: line 334, column 7 Potential leak of memory pointed to by 'curr_section_hdr' Report-by: scan-build (ad8e62) Signed-off-by: Francis Deslauriers Change-Id: I5cc4049075e4ca1d64d98e17662f935e6562bb6e Reviewed-on: https://review.lttng.org/c/babeltrace/+/1265 Reviewed-by: Jérémie Galarneau --- plugins/lttng-utils/debug-info/bin-info.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/plugins/lttng-utils/debug-info/bin-info.c b/plugins/lttng-utils/debug-info/bin-info.c index 5393cdba..eeac6b4b 100644 --- a/plugins/lttng-utils/debug-info/bin-info.c +++ b/plugins/lttng-utils/debug-info/bin-info.c @@ -339,7 +339,7 @@ int is_build_id_matching(struct bin_info *bin) { int ret, is_build_id, is_matching = 0; Elf_Scn *curr_section = NULL, *next_section = NULL; - GElf_Shdr *curr_section_hdr = NULL; + GElf_Shdr curr_section_hdr; if (!bin->build_id) { goto error; @@ -354,11 +354,6 @@ int is_build_id_matching(struct bin_info *bin) } } - curr_section_hdr = g_new0(GElf_Shdr, 1); - if (!curr_section_hdr) { - goto error; - } - next_section = elf_nextscn(bin->elf_file, curr_section); if (!next_section) { goto error; @@ -371,13 +366,11 @@ int is_build_id_matching(struct bin_info *bin) curr_section = next_section; next_section = elf_nextscn(bin->elf_file, curr_section); - curr_section_hdr = gelf_getshdr(curr_section, curr_section_hdr); - - if (!curr_section_hdr) { + if (!gelf_getshdr(curr_section, &curr_section_hdr)) { goto error; } - if (curr_section_hdr->sh_type != SHT_NOTE) { + if (curr_section_hdr.sh_type != SHT_NOTE) { continue; } @@ -422,7 +415,6 @@ int is_build_id_matching(struct bin_info *bin) } } error: - g_free(curr_section_hdr); return is_matching; } -- 2.34.1