objcopy write_debugging_info memory leaks

The old stabs code didn't bother too much about freeing memory.
This patch corrects that and avoids some dubious copying of strings.

	* objcopy.c (write_debugging_info): Free both strings and
	syms on failure to create sections.
	* wrstabs.c: Delete unnecessary forward declarations and casts
	throughout file.
	(stab_write_symbol_and_free): New function.  Use it
	throughout, simplifying return paths.
	(stab_push_string): Don't strdup string.  Use it thoughout
	for malloced strings.
	(stab_push_string_dup): New function.  Use it throughout for
	strings in auto buffers.
	(write_stabs_in_sections_debugging_info): Free malloced memory.
	(stab_enum_type): Increase buffer sizing for worst case.
	(stab_range_type, stab_array_type): Reduce buffer size.
	(stab_set_type): Likewise.
	(stab_method_type): Free args on error return.  Correct
	buffer size.
	(stab_struct_field): Fix memory leaks.
	(stab_class_static_member, stab_class_baseclass): Likewise.
	(stab_start_class_type): Likewise.  Correct buffer size.
	(stab_class_start_method): Correct buffer size.
	(stab_class_method_var): Free memory on error return.
	(stab_start_function): Fix "rettype" memory leak.
This commit is contained in:
Alan Modra
2023-04-05 16:44:35 +09:30
parent b5bfe9351b
commit 41e6ffcecb
2 changed files with 167 additions and 217 deletions

View File

@@ -4667,6 +4667,7 @@ write_debugging_info (bfd *obfd, void *dhandle,
flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
ret = true;
if (stabsec == NULL
|| stabstrsec == NULL
|| !bfd_set_section_size (stabsec, symsize)
@@ -4676,18 +4677,17 @@ write_debugging_info (bfd *obfd, void *dhandle,
{
bfd_nonfatal_message (NULL, obfd, NULL,
_("can't create debugging section"));
free (strings);
return false;
ret = false;
}
/* We can get away with setting the section contents now because
the next thing the caller is going to do is copy over the
real sections. We may someday have to split the contents
setting out of this function. */
ret = true;
if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
|| ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
stringsize))
if (ret
&& (!bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
|| !bfd_set_section_contents (obfd, stabstrsec, strings, 0,
stringsize)))
{
bfd_nonfatal_message (NULL, obfd, NULL,
_("can't set debugging section contents"));