[PATCH 19/31] dwarves: Fix variable shadowing in __cus__find_struct_by_name()

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

The inner loop declared 'struct tag *tag' which shadowed the outer
'struct tag *tag = NULL' at function scope.  When the inner lookup
succeeded and we broke out of the loop, the function returned the
outer (still NULL) tag instead of the found one.

This broke any tool that loads CUs without a steal callback and then
calls cus__find_struct_by_name() — notably ctracer.  Tools like pahole
and pfunct were unaffected because they use a steal callback to process
each CU as it arrives, calling per-CU cu__find_struct_by_name() which
has no shadowing issue.

Before: ctracer loads 3291 CUs, cus__find_struct_by_name() returns NULL
After:  ctracer loads 3291 CUs, cus__find_struct_by_name() finds the struct

Fixes: fb99cad539e58638 ("dwarf_loader: Parallel DWARF loading")
Assisted-by: Claude:claude-sonnet-4-5
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
 dwarves.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/dwarves.c b/dwarves.c
index 5c9d83a6dee897c2..94bab301d598695d 100644
--- a/dwarves.c
+++ b/dwarves.c
@@ -1049,7 +1049,8 @@ static struct tag *__cus__find_struct_by_name(struct cus *cus, struct cu **cu, c
 	cus__lock(cus);
 
 	list_for_each_entry(pos, &cus->cus, node) {
-		struct tag *tag = __cu__find_struct_by_name(pos, name, include_decls, unions, id);
+		/* Don't shadow the outer 'tag' — we need to return it */
+		tag = __cu__find_struct_by_name(pos, name, include_decls, unions, id);
 		if (tag != NULL) {
 			if (cu != NULL)
 				*cu = pos;
-- 
2.55.0