Fix: flt.lttng-utils.debug-info: memory leak
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Mon, 6 May 2019 20:22:03 +0000 (16:22 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 7 May 2019 16:20:38 +0000 (12:20 -0400)
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 <francis.deslauriers@efficios.com>
Change-Id: I5cc4049075e4ca1d64d98e17662f935e6562bb6e
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1265
Reviewed-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
plugins/lttng-utils/debug-info/bin-info.c

index 5393cdbaefd54bf5fbab5cabc3bbeccb85c519e8..eeac6b4b58bf98a35d892a93be1e7809bd9a0565 100644 (file)
@@ -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;
 }
 
This page took 0.025766 seconds and 4 git commands to generate.