Skip to contentSkip to conversation

dwarf_loader: Fix memleak in dwarf_cus__create_cu

PATCH In review Viktor Malik 2 participants 3 messages Sep 2, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dwarf_loader: Fix memleak in dwarf_cus__create_cu

Coverity reports a possible memleak in dwarf_cus__create_cu due to cu not being freed for the case when it was allocated but cu__set_common failed. Fix the leak.

---
 dwarf_loader.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
dwarf_loader.c +3 -1
index 04f5637..ad36e3c 100644
@@ -3378,8 +3378,10 @@ static struct dwarf_cu *dwarf_cus__create_cu(struct dwarf_cus *dcus, Dwarf_Die *
 	 */
 	const char *name = attr_string(cu_die, DW_AT_name, dcus->conf);
 	struct cu *cu = cu__new(name ?: "", pointer_size, dcus->build_id, dcus->build_id_len, dcus->filename, dcus->conf->use_obstack);
-	if (cu == NULL || cu__set_common(cu, dcus->conf, dcus->mod, dcus->elf) != 0)
+	if (cu == NULL || cu__set_common(cu, dcus->conf, dcus->mod, dcus->elf) != 0) {
+		cu__delete(cu);
 		return NULL;
+	}
 
 	struct dwarf_cu *dcu = dwarf_cu__new(cu);
 
Signature · 1 line
2.46.0
#1 lore raw
Quoted 15 lines · Viktor Malik

Coverity reports a possible memleak in dwarf_cus__create_cu due to cu not being freed for the case when it was allocated but cu__set_common failed. Fix the leak.

---
 dwarf_loader.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
dwarf_loader.c +0 -0
index 04f5637..ad36e3c 100644
@@ -3378,8 +3378,10 @@ static struct dwarf_cu *dwarf_cus__create_cu(struct dwarf_cus *dcus, Dwarf_Die *
 	 */

dwarf_loader.c static struct dwarf_cu *dwarf_cus__create_cu(struct dwarf_cus *dcus, Dwarf_Die * · line 3387

Quoted 10 lines
dwarf_loader.c +3 -1
 	const char *name = attr_string(cu_die, DW_AT_name, dcus->conf);
 	struct cu *cu = cu__new(name ?: "", pointer_size, dcus->build_id, dcus->build_id_len, dcus->filename, dcus->conf->use_obstack);
-	if (cu == NULL || cu__set_common(cu, dcus->conf, dcus->mod, dcus->elf) != 0)
+	if (cu == NULL || cu__set_common(cu, dcus->conf, dcus->mod, dcus->elf) != 0) {
+		cu__delete(cu);
 		return NULL;
+	}
 
 	struct dwarf_cu *dcu = dwarf_cu__new(cu);
 

Good catch! Looks like there's another few instances of cu leaks like this later on in __cus__load_debug_types():

	if (*cup == NULL) {
                        struct cu *cu;

                        cu = cu__new("", pointer_size, build_id,
                                     build_id_len, filename,
conf->use_obstack);
                        if (cu == NULL ||
                            cu__set_common(cu, conf, mod, elf) != 0) {
need cu__delete() here ->
                                return DWARF_CB_ABORT;
                        }

                        if (dwarf_cu__init(dcup, cu) != 0)
need cu__delete() here ->
                                return DWARF_CB_ABORT;

Could you respin to fix them too? Thanks!

#2 lore raw
Quoted 48 lines · Alan Maguire
Quoted 15 lines · Viktor Malik

Coverity reports a possible memleak in dwarf_cus__create_cu due to cu not being freed for the case when it was allocated but cu__set_common failed. Fix the leak.

---
 dwarf_loader.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
dwarf_loader.c +0 -0
index 04f5637..ad36e3c 100644
@@ -3378,8 +3378,10 @@ static struct dwarf_cu *dwarf_cus__create_cu(struct dwarf_cus *dcus, Dwarf_Die *
 	 */

dwarf_loader.c static struct dwarf_cu *dwarf_cus__create_cu(struct dwarf_cus *dcus, Dwarf_Die * · line 3387

Quoted 10 lines
dwarf_loader.c +3 -1
 	const char *name = attr_string(cu_die, DW_AT_name, dcus->conf);
 	struct cu *cu = cu__new(name ?: "", pointer_size, dcus->build_id, dcus->build_id_len, dcus->filename, dcus->conf->use_obstack);
-	if (cu == NULL || cu__set_common(cu, dcus->conf, dcus->mod, dcus->elf) != 0)
+	if (cu == NULL || cu__set_common(cu, dcus->conf, dcus->mod, dcus->elf) != 0) {
+		cu__delete(cu);
 		return NULL;
+	}
 
 	struct dwarf_cu *dcu = dwarf_cu__new(cu);
 

Good catch! Looks like there's another few instances of cu leaks like this later on in __cus__load_debug_types():

	if (*cup == NULL) {
                        struct cu *cu;

                        cu = cu__new("", pointer_size, build_id,
                                     build_id_len, filename,
conf->use_obstack);
                        if (cu == NULL ||
                            cu__set_common(cu, conf, mod, elf) != 0) {
need cu__delete() here ->
                                return DWARF_CB_ABORT;
                        }

                        if (dwarf_cu__init(dcup, cu) != 0)
need cu__delete() here ->
                                return DWARF_CB_ABORT;
Quoted 2 lines

Could you respin to fix them too? Thanks!

Sure, I'll check those and send v2 tomorrow.

Viktor