[PATCH v1 4/7] bfd: fix memory leak when default-initializing an OAv2 attribute

Matthieu Longo matthieu.longo@arm.com
Thu Feb 5 17:39:51 GMT 2026
On 05/02/2026 16:49, Jan Beulich wrote:
> On 05.02.2026 17:02, Matthieu Longo wrote:
>> On 05/02/2026 15:39, Jan Beulich wrote:
>>> On 05.02.2026 16:18, Matthieu Longo wrote:
>>>> New version using oav2_assign_value().
>>>
>>> Just one question:
>>>
>>>> --- a/bfd/elf-attrs.c
>>>> +++ b/bfd/elf-attrs.c
>>>> @@ -1083,33 +1083,30 @@ oav2_attr_overwrite_with_default (const struct bfd_link_info *info,
>>>>     {
>>>>       const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
>>>>
>>>> +  union obj_attr_value_v2 default_value;
>>>> +  memset (&default_value, 0, sizeof (default_value));
>>>
>>> Why not via an initializer?
>>
>> I did some research because I remember that last time I had to initialize a union with zero, there was an issue somewhere.
>> And I found that:
>>
>>     {0} initializer in C or C++ for unions no longer guarantees clearing of the
>>     whole union (except for static storage duration initialization), it just
>>     initializes the first union member to zero. If initialization of the whole
>>     union including padding bits is desirable, use {} (valid in C23 or C++) or use
>>     -fzero-init-padding-bits=unions option to restore the old GCC behavior.
>>
>>     Source: https://gcc.gnu.org/gcc-15/changes.html
>>
>> We don't want to make C23 a requirement. And at the same time, the code must not be broken if someone wants to use C23.
>> So neither {0} nor {} would work here in my understanding.
> 
> Correct. But a dedicated initializer should work, aiui, for those being specified
> to work like static storage duration initializers.
> 
> Jan


I am not sure I understood you well.
Are you saying that I could use the following to initialize the union ?
union obj_attr_value_v2 default_value = { .int = 0, };
But this initializes only one member. The string address was only partially initialized to NULL.
union obj_attr_value_v2 default_value = { .int = 0, .string = NULL }; does not work.
And union obj_attr_value_v2 default_value = { .string = NULL }; works but it assumes that the biggest element in the union will always be 'const char *'.

How does this work if I need both members of the union being zeroed ?

Matthieu


More information about the Binutils mailing list