Fix: src.ctf.lttng-live: possible memory leak on error path
[babeltrace.git] / plugins / lttng-utils / debug-info / bin-info.c
index 5393cdbaefd54bf5fbab5cabc3bbeccb85c519e8..c0546c861df8a4288f489bb089d860800fbdc7c5 100644 (file)
@@ -90,8 +90,7 @@ struct bin_info *bin_info_create(struct bt_fd_cache *fdc, const char *path,
        }
 
        if (target_prefix) {
-               bin->elf_path = g_build_path("/", target_prefix,
-                                               path, NULL);
+               bin->elf_path = g_build_filename(target_prefix, path, NULL);
        } else {
                bin->elf_path = g_strdup(path);
        }
@@ -252,8 +251,18 @@ int is_build_id_note_section(uint8_t *buf)
         * - Note type
         */
        name_sz = (uint32_t) *buf;
+
+       /*
+        * Check the note name length. The name_sz field includes the
+        * terminating null byte.
+        */
+       if (name_sz != sizeof(BUILD_ID_NOTE_NAME)) {
+               goto invalid;
+       }
+
        buf += sizeof(name_sz);
 
+       /* Ignore the note description size. */
        buf += sizeof(desc_sz);
 
        note_type = (uint32_t) *buf;
@@ -339,7 +348,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 +363,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 +375,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 +424,6 @@ int is_build_id_matching(struct bin_info *bin)
                }
        }
 error:
-       g_free(curr_section_hdr);
        return is_matching;
 }
 
@@ -600,7 +601,7 @@ int bin_info_set_dwarf_info_build_id(struct bin_info *bin)
        g_snprintf(&build_id_file[build_id_char_len], build_id_suffix_char_len,
                BUILD_ID_SUFFIX);
 
-       path = g_build_path("/", dbg_dir, BUILD_ID_SUBDIR, build_id_file, NULL);
+       path = g_build_filename(dbg_dir, BUILD_ID_SUBDIR, build_id_file, NULL);
        if (!path) {
                goto error;
        }
@@ -1434,7 +1435,9 @@ error:
  * @param cu           bt_dwarf_cu instance in which to look for the address
  * @param addr         The address for which to look for
  * @param src_loc      Out parameter, the source location (filename and
- *                     line number) for the address
+ *                     line number) for the address. Set only if the address
+ *                     is found and resolved successfully
+ *
  * @returns            0 on success, -1 on failure
  */
 static
@@ -1459,7 +1462,8 @@ int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu *cu, uint64_t addr,
 
        line = dwarf_getsrc_die(die->dwarf_die, addr);
        if (!line) {
-               goto error;
+               /* This is not an error. The caller needs to keep looking. */
+               goto end;
        }
 
        ret = dwarf_lineaddr(line, &line_addr);
@@ -1493,6 +1497,7 @@ int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu *cu, uint64_t addr,
                *src_loc = _src_loc;
        }
 
+end:
        return 0;
 
 error:
This page took 0.026116 seconds and 4 git commands to generate.