]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commit
bfd: fix memory leak when assigning the merge result of OAv2 string attributes
authorMatthieu Longo <matthieu.longo@arm.com>
Mon, 2 Feb 2026 14:40:22 +0000 (14:40 +0000)
committerMatthieu Longo <matthieu.longo@arm.com>
Fri, 6 Feb 2026 10:41:54 +0000 (10:41 +0000)
commit2eeb2c99bb0fff2ff0afab5959a4c49c87035aab
treeae53a86aa83aa8e8dfab8efb024eaeb7004effb6
parentbf0a637f4d5f73b5242241bcec5a447a7531d5d2
bfd: fix memory leak when assigning the merge result of OAv2 string attributes

oav2_attr_merge() merges input OAv2 attributes and returns a merge result,
which is then assigned to the previous value held by REF.

In the current implementation of handle_optional_subsection_merge(), when
merging string attributes, the existing value is overwritten without first
being freed. This results in a memory leak. This issue was detected by
LeakSanitizer (see the relevant stack trace below).

This patch fixes the memory leak by wrapping the assignment of the merge
result to REF inside a helper function. For string attributes, the helper
frees the previous value before performing the assignment. This approach
also centralizes the logic and makes easier to correctly free and assign
more complex structures in the future, if needed.

==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 12 byte(s) in 2 object(s) allocated from:
    #2 xmemdup ../../libiberty/xmemdup.c:37
    #3 read_ntbs ../../bfd/elf-attrs.c:2676
    #4 oav2_parse_attr ../../bfd/elf-attrs.c:2699
    #5 oav2_parse_subsection ../../bfd/elf-attrs.c:2845
    #6 oav2_parse_section ../../bfd/elf-attrs.c:2883
    #7 _bfd_elf_parse_attributes ../../bfd/elf-attrs.c:2947
bfd/elf-attrs.c