From 68aa8a8d0b3f219551a004ea9a637768e6a38755 Mon Sep 17 00:00:00 2001 From: Quentin JEROME Date: Fri, 24 Feb 2023 21:23:10 +0100 Subject: [PATCH] Fixed bug: - duplicated members under some conditions (it was not causing any functional issue) Signed-off-by: Quentin JEROME --- aya-obj/src/btf/helpers.rs | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/aya-obj/src/btf/helpers.rs b/aya-obj/src/btf/helpers.rs index 17df2f5b..fd289a2f 100644 --- a/aya-obj/src/btf/helpers.rs +++ b/aya-obj/src/btf/helpers.rs @@ -78,13 +78,20 @@ impl StructHelper { match member_type { BtfType::Struct(_) => { - // we push the structure name - self.members.push(MemberHelper::new( - p.clone(), - member_offset, - member_type.clone(), - None, - )); + // in case of structure containing an unamed union and again + // a struct the algorithm creates duplicates members so + // we need to check if we aleady have the member before insertion. + // this is not optimal, it would be better in a hashmap + if !self.contains_member(p.to_string()) { + // we push the structure name + self.members.push(MemberHelper::new( + p.clone(), + member_offset, + member_type.clone(), + None, + )); + } + // we process all members of the structure self.resolve_members_rec(btf, member_type, &p, member_offset)?; } @@ -121,9 +128,6 @@ impl StructHelper { // members of unions all have the same offset if !matches!(t.kind(), BtfKind::Union) { - // a zero member_size is an issue as two members - // will have the same offsets - debug_assert_ne!(member_size, 0); member_offset += member_size; } } @@ -155,6 +159,11 @@ impl StructHelper { self.members.iter().find(|m| m.is_path(path)) } + #[inline] + pub(crate) fn contains_member>(&self, path: P) -> bool { + self.get_member(path).is_some() + } + /// Returns the size of the [`StructHelper`]. This might not be the real size of /// the structure as it does not take alignement into account. In order to have /// the size of aligned structure use [`size_of_aligned`] method.