From: Alan Modra <amodra@gmail.com>
To: binutils@sourceware.org
Subject: Fix mmo memory leaks
Date: Fri, 10 Feb 2023 20:35:23 +1030 [thread overview]
Message-ID: <Y+YW49ew8TrVHIVE@squeak.grove.modra.org> (raw)
The main one here is the section buffer, which can be quite large.
By using alloc rather than malloc we can leave tidying memory to the
generic bfd code when the bfd is closed. bfd_check_format also
releases memory when object_p fails, so while it wouldn't be wrong
to bfd_release at bad_format_free in mmo_object_p, it's a little extra
code and work for no gain.
* mmo.c (mmo_object_p): bfd_alloc rather than bfd_malloc
lop_stab_symbol. Don't free/release on error.
(mmo_get_spec_section): bfd_zalloc rather than bfd_zmalloc
section buffer.
(mmo_scan): Free fname on another error path.
diff --git a/bfd/mmo.c b/bfd/mmo.c
index 6e218345243..bbdaad31e6e 100644
--- a/bfd/mmo.c
+++ b/bfd/mmo.c
@@ -526,7 +526,7 @@ mmo_object_p (bfd *abfd)
important as all of the symbol information can only be 256k. */
abfd->tdata.mmo_data->max_symbol_length = (b[2] * 256 + b[3]) * 4;
abfd->tdata.mmo_data->lop_stab_symbol
- = bfd_malloc (abfd->tdata.mmo_data->max_symbol_length + 1);
+ = bfd_alloc (abfd, abfd->tdata.mmo_data->max_symbol_length + 1);
if (abfd->tdata.mmo_data->lop_stab_symbol == NULL)
{
@@ -539,7 +539,7 @@ mmo_object_p (bfd *abfd)
/* Read in everything. */
if (! mmo_scan (abfd))
- goto bad_format_free;
+ goto bad_format;
if (abfd->symcount > 0)
abfd->flags |= HAS_SYMS;
@@ -548,12 +548,10 @@ mmo_object_p (bfd *abfd)
arches (not recommended due to its small-size limitations). Look at
the ELF format for how to make it target-generic. */
if (! bfd_default_set_arch_mach (abfd, bfd_arch_mmix, 0))
- goto bad_format_free;
+ goto bad_format;
return _bfd_no_cleanup;
- bad_format_free:
- free (abfd->tdata.mmo_data->lop_stab_symbol);
bad_format:
bfd_set_error (bfd_error_wrong_format);
bad_final:
@@ -1128,8 +1126,8 @@ mmo_get_spec_section (bfd *abfd, int spec_data_number)
/* We allocate a buffer here for the advertised size, with head room for
tetrabyte alignment. */
- loc = bfd_zmalloc (section_length + 3
- + sizeof (struct mmo_data_list_struct));
+ loc = bfd_zalloc (abfd, (section_length + 3
+ + sizeof (struct mmo_data_list_struct)));
if (loc == NULL)
goto format_error;
@@ -1888,6 +1886,7 @@ mmo_scan (bfd *abfd)
" was already entered as `%s'\n"),
abfd, y, fname, file_names[y]);
bfd_set_error (bfd_error_bad_value);
+ free (fname);
goto error_return;
}
--
Alan Modra
Australia Development Lab, IBM
reply other threads:[~2023-02-10 10:05 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Y+YW49ew8TrVHIVE@squeak.grove.modra.org \
--to=amodra@gmail.com \
--cc=binutils@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).