Package: libbinutils
Version: 2.41.50.20231206-1

Valgrind reports a leak when using bfd_find_nearest_line():

==2681185== 145,802 bytes in 1 blocks are definitely lost in loss record 39
of 39
==2681185==    at 0x4840808: malloc (in
/usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2681185==    by 0x4FCA936: bfd_malloc (libbfd.c:290)
==2681185==    by 0x5023665: read_section (dwarf2.c:737)
==2681185==    by 0x5024E54: read_debug_rnglists (dwarf2.c:3181)
==2681185==    by 0x5024E54: read_rnglists (dwarf2.c:3716)
==2681185==    by 0x5026D42: read_rangelist (dwarf2.c:3791)
==2681185==    by 0x5026D42: parse_comp_unit (dwarf2.c:4570)
==2681185==    by 0x5026D42: stash_comp_unit (dwarf2.c:5589)
==2681185==    by 0x502AB2A: _bfd_dwarf2_find_nearest_line_with_alt
(dwarf2.c:5969)
==2681185==    by 0x4FFDC56: _bfd_elf_find_nearest_line_with_alt
(elf.c:10013)
==2681185==    by 0x4FFDDC0: _bfd_elf_find_nearest_line (elf.c:9990)

The program does call bfd_close(), but that single allocation is still
leaked afterwards.

I've traced this to dwarf_rnglists_buffer in dwarf2_debug_file never being
freed. The following patch fixes it:

--- a/bfd/dwarf2.c 2023-11-25 04:59:00.000000000 -0300
+++ b/bfd/dwarf2.c 2023-12-10 21:05:02.084673848 -0300
@@ -6123,6 +6123,7 @@
       free (file->dwarf_line_str_buffer);
       free (file->dwarf_str_buffer);
       free (file->dwarf_ranges_buffer);
+      free (file->dwarf_rnglists_buffer);
       free (file->dwarf_line_buffer);
       free (file->dwarf_abbrev_buffer);
       free (file->dwarf_info_buffer);

I don't provide a test case, but it's very easy to see that the pointer
does get allocated but never gets freed anywhere, unlike its neighbours
(e.g. dwarf_ranges_buffer is allocated and freed in exactly the same way).
I've also tested the patch and the error goes away with it.
  • Bug#1057962: bfd_find_nearest_line() leaks memory due to... Konstantin Isakov

Reply via email to