[PATCH] binutils: avoid leaking section header pointers

Alhuda Khan alhuda@research.digiscrypt.com
Thu Nov 19 11:47:11 GMT 2026


From: Alhuda Khan <alhuda@research.digiscrypt.com>

readelf leaks memory when dumping a thin archive of ELF objects.
ElfXX(_get_section_headers) allocates filedata->orig_section_headers on
every non-probe call, so when it runs more than once for the same
Filedata the earlier allocation is overwritten and lost.

"readelf -h" on a thin archive reproduces it. In an -fsanitize=address
build, LeakSanitizer reports a 72-byte leak from the xcalloc in that
function. Allocate orig_section_headers only when it is still NULL.

This also clears the "readelf -h bintest.thin" failure in the testsuite:
with an ASan build, check-binutils goes from 1 unexpected failure to 0
(354 -> 355 passes).

Signed-off-by: Alhuda Khan <alhuda@research.digiscrypt.com>
---
 binutils/readelf-nn.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/binutils/readelf-nn.c b/binutils/readelf-nn.c
index 071fd80cc46..39d1cae70a8 100644
--- a/binutils/readelf-nn.c
+++ b/binutils/readelf-nn.c
@@ -135,9 +135,9 @@ ElfXX(_get_section_headers) (Filedata * filedata, bool probe)
       return false;
     }
 
-  if (!probe)
+  if (!probe && filedata->orig_section_headers == NULL)
     filedata->orig_section_headers = xcalloc2 (num,
-					       sizeof (Elf_Internal_Shdr *));
+	sizeof (Elf_Internal_Shdr *));
 
   orig_internal = filedata->orig_section_headers;
   for (i = 0, internal = filedata->section_headers;

thanks 
Alhuda Khan


More information about the Binutils mailing list