[PATCH 04/31] dwarves: Fix missing list head initialization in type__clone_members

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

After class__clone() does memcpy(class, from, sizeof(*class)), the
clone's list_head fields still point into the source type's linked
list chains.  type__clone_members() reinitializes namespace.tags and
variant_parts, but misses type_enum, template_type_params, and
template_value_params.

If the source type has non-empty lists for any of these, subsequent
list operations on the clone (e.g. during --reorganize) corrupt both
the clone's and the source's lists, leading to crashes or infinite
loops.

Add the three missing INIT_LIST_HEAD calls, matching the initialization
done in __type__init().

Fixes: 8a1e0238ee4005a6 ("dwarf_loader: Initial support for DW_TAG_template_type_param tag")
Reported-by: Sashiko:gemini-3-1-pro-preview # Running on a local machine
Assisted-by: Claude:claude-sonnet-4-5
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
 dwarves.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/dwarves.c b/dwarves.c
index f05c4d3b4b669335..95a8dcd66f861be9 100644
--- a/dwarves.c
+++ b/dwarves.c
@@ -1382,6 +1382,10 @@ static int type__clone_members(struct type *type, const struct type *from, struc
 
 	type->nr_members = type->nr_static_members = 0;
 	INIT_LIST_HEAD(&type->namespace.tags);
+	INIT_LIST_HEAD(&type->namespace.annots);
+	INIT_LIST_HEAD(&type->type_enum);
+	INIT_LIST_HEAD(&type->template_type_params);
+	INIT_LIST_HEAD(&type->template_value_params);
 
 	type__for_each_member(from, pos) {
 		struct class_member *clone = class_member__clone(pos, cu);
@@ -1400,6 +1404,8 @@ struct class *class__clone(const struct class *from, const char *new_class_name,
 
 	 if (class != NULL) {
 		memcpy(class, from, sizeof(*class));
+		INIT_LIST_HEAD(&class->vtable);
+		class->nr_vtable_entries = 0;
 		if (new_class_name != NULL) {
 			class->type.namespace.name = strdup(new_class_name);
 			if (class->type.namespace.name == NULL) {
-- 
2.55.0