[PATCH v1 4/7] bfd: fix memory leak when default-initializing an OAv2 attribute
Matthieu Longo
matthieu.longo@arm.com
Tue Feb 3 10:00:45 GMT 2026
To merge an OAv2 attribute, the input values must either be present in
the subsections being merged, or a default value must be created for the
missing ones. Note that this default value is not necessarily null.
In the current implementation of oav2_attr_default(), the default value
is created by copying another attribute provided as a template. As a
result, a string attribute may be copied from the template if it is not
NULL. The copied value is overwritten with whatever default the backend
provides for that attribute.
In oav2_attr_overwrite_with_default(), when no default attribute value
is found in the backend, a string attribute is simply assigned NULL.
This ignores the possibility that the original value may be non-NULL,
and causes the previously allocated memory for the string to be leaked.
This issue was detected by the LeakSanitizer (see the relevant part of
the stack trace below).
This patch fixes the memory leak by freeing the existing value before
assigning NULL to the attribute.
==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 5 byte(s) in 1 object(s) allocated from:
...
#2 0x6252100dc7b2 in xstrdup ../../libiberty/xstrdup.c:34
#3 0x62520fecf6c0 in _bfd_elf_obj_attr_v2_copy ../../bfd/elf-attrs.c:3185
#4 0x62520fec58c0 in oav2_attr_default ../../bfd/elf-attrs.c:1109
#5 0x62520fec81f6 in handle_optional_subsection_merge ../../bfd/elf-attrs.c:1558
---
bfd/elf-attrs.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/bfd/elf-attrs.c b/bfd/elf-attrs.c
index b9547631793..5e209dfb7bb 100644
--- a/bfd/elf-attrs.c
+++ b/bfd/elf-attrs.c
@@ -1078,7 +1078,10 @@ oav2_attr_overwrite_with_default (const struct bfd_link_info *info,
if (subsec->encoding == OA_ENC_ULEB128)
attr->val.uint = 0;
else
- attr->val.string = NULL;
+ {
+ free ((char *) attr->val.string);
+ attr->val.string = NULL;
+ }
return;
}
@@ -1089,7 +1092,7 @@ oav2_attr_overwrite_with_default (const struct bfd_link_info *info,
{
if (attr->val.string != NULL)
{
- free ((void *) attr->val.string);
+ free ((char *) attr->val.string);
attr->val.string = NULL;
}
if (attr_info->default_value.string != NULL)
--
2.52.0
More information about the Binutils
mailing list