From 6f860418d556d4e5492b3da9e1a52e4b85a85f3e Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Wed, 14 Jun 2023 14:24:50 +0930 Subject: asprintf memory leaks A number of backends want to return bfd_reloc_dangerous messaqes from relocation special_function, and construct the message using asprintf. Such messages are not freed anywhere, leading to small memory leaks inside libbfd. To limit the leaks, I'd implemented a static buffer in the ppc backends that was freed before use in asprintf output. This patch extends that scheme to other backends using a shared static buffer and goes further in freeing the buffer on any bfd_close. The patch also fixes a few other cases where asprintf output was not freed after use. bfd/ * bfd.c (_input_error_msg): Make global and rename to.. (_bfd_error_buf): ..this. (bfd_asprintf): New function. (bfd_errmsg): Use bfd_asprintf. * opncls.c (bfd_close_all_done): Free _buf_error_buf. * elf32-arm.c (find_thumb_glue, find_arm_glue): Use bfd_asprintf. * elf32-nios2.c (nios2_elf32_relocate_section): Likewise. * elf32-ppc.c (ppc_elf_unhandled_reloc): Likewise. * elf64-ppc.c (ppc64_elf_unhandled_reloc): Likewise. * elfnn-riscv.c (riscv_resolve_pcrel_lo_relocs): Likewise. (riscv_elf_relocate_section): Likewise. * libbfd.h: Regenerate. gas/ * read.c (read_end): Free current_name and current_label. (do_s_func): Likewise on error path. strdup label. ld/ * pe-dll.c (make_head, make_tail, make_one), (make_singleton_name_thunk, make_import_fixup_entry), (make_runtime_pseudo_reloc), (pe_create_runtime_relocator_reference: Free oname after use. --- gas/read.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'gas/read.c') diff --git a/gas/read.c b/gas/read.c index b4b628f..0cceca8 100644 --- a/gas/read.c +++ b/gas/read.c @@ -322,6 +322,8 @@ read_end (void) stabs_end (); poend (); _obstack_free (&cond_obstack, NULL); + free (current_name); + free (current_label); } #ifndef TC_ADDRESS_BYTES @@ -6048,6 +6050,8 @@ do_s_func (int end_p, const char *default_prefix) if (debug_type == DEBUG_STABS) stabs_generate_asm_endfunc (current_name, current_label); + free (current_name); + free (current_label); current_name = current_label = NULL; } else /* ! end_p */ @@ -6084,7 +6088,7 @@ do_s_func (int end_p, const char *default_prefix) as_fatal ("%s", xstrerror (errno)); } else - label = name; + label = xstrdup (name); } } else -- cgit v1.1