Hi Mark, On Thu, Nov 19, 2026 at 9:16 PM Mark Wielaard <[email protected]> wrote: > > In elf_newdata we return &result->data.d which turns a Elf_Data_list * > into an Elf_Data *. Which should work because data is the first member > of Elf_Data_list and d is the first member of Elf_Data_Scn. But > unfortunately dereferencing NULL is technically UB. Just directly > casting result to Elf_Data * is simpler and defined. Just add some > static asserts so proof this works. > > * libelf/elf_newdata.c (elf_newdata): Cast result to Elf_Data *. > > Signed-off-by: Mark Wielaard <[email protected]>
LGTM. Aaron > --- > libelf/elf_newdata.c | 12 +++++++++--- > 1 file changed, 9 insertions(+), 3 deletions(-) > > diff --git a/libelf/elf_newdata.c b/libelf/elf_newdata.c > index 7eadb9f05b2f..70a4122d635e 100644 > --- a/libelf/elf_newdata.c > +++ b/libelf/elf_newdata.c > @@ -133,7 +133,13 @@ elf_newdata (Elf_Scn *scn) > out: > rwlock_unlock (scn->elf->lock); > > - /* Please note that the following is thread safe and is also defined > - for RESULT == NULL since it still return NULL. */ > - return &result->data.d; > + /* We would like to just return &result->data.d; Which should work > + because data is the first member of Elf_Data_list and d is the > + first member of Elf_Data_Scn. But dereferencing NULL is undefined > + behavior. Just cast result directly to Elf_Data * (which is > + defined). And do some static asserts so this breaks if someone > + ever changes the struct definitions. */ > + eu_static_assert (offsetof (Elf_Data_List, data) == 0); > + eu_static_assert (offsetof (Elf_Data_Scn, d) == 0); > + return (Elf_Data *) result; > } > -- > 2.56.1 >
