[PATCH 09/31] dwarf_loader: Fix annotation failure leaks in variable and typedef creation

Arnaldo Carvalho de Melo <[email protected]> Wed, 29 Jul 2026 16:07:09 -0300
Newsgroups org.kernel.vger.dwarves,org.kernel.vger.bpf
Message-ID <[email protected]>
From: Arnaldo Carvalho de Melo <[email protected]>

die__create_new_variable() and die__create_new_typedef() leak their
allocated objects when add_child_llvm_annotations() fails: the
allocation succeeds but the error path returns NULL without freeing it.

Split the compound NULL-or-annotation-failure check in
die__create_new_variable() into separate checks, and add explicit
cleanup (tag__delete / type__delete) before returning NULL on
annotation failure in both functions.

Fixes: aa8c494e65a77fa5 ("dwarf_loader: Parse DWARF tag DW_TAG_LLVM_annotation")
Assisted-by: Claude:claude-sonnet-4-5
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
 dwarf_loader.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/dwarf_loader.c b/dwarf_loader.c
index b87519fa73405ff7..ddf7cdcfd467beb4 100644
--- a/dwarf_loader.c
+++ b/dwarf_loader.c
@@ -2078,8 +2078,10 @@ static struct tag *die__create_new_typedef(Dwarf_Die *die, struct cu *cu, struct
 	if (tdef == NULL)
 		return NULL;
 
-	if (add_child_llvm_annotations(die, -1, conf, &tdef->namespace.annots))
+	if (add_child_llvm_annotations(die, -1, conf, &tdef->namespace.annots)) {
+		type__delete(tdef, cu);
 		return NULL;
+	}
 
 	return &tdef->namespace.tag;
 }
@@ -2190,8 +2192,13 @@ static struct tag *die__create_new_variable(Dwarf_Die *die, struct cu *cu, struc
 {
 	struct variable *var = variable__new(die, cu, conf, top_level);
 
-	if (var == NULL || add_child_llvm_annotations(die, -1, conf, &var->annots))
+	if (var == NULL)
+		return NULL;
+
+	if (add_child_llvm_annotations(die, -1, conf, &var->annots)) {
+		tag__delete(&var->ip.tag, cu);
 		return NULL;
+	}
 
 	return &var->ip.tag;
 }
-- 
2.55.0