public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: binutils@sourceware.org
Subject: PR31162, Memory Leak in ldwrite.c
Date: Mon, 18 Dec 2023 22:45:09 +1030	[thread overview]
Message-ID: <ZYA3zY4ZcFOk9MWT@squeak.grove.modra.org> (raw)

This isn't a particularly worrying memory leak, but fix it anyway.

	PR 31162
	* ldwrite.c (build_link_order): Use bfd_alloc rather than xmalloc.
	Add %E to error messages.

diff --git a/ld/ldwrite.c b/ld/ldwrite.c
index 0e9d7d0a3df..9375644cbd1 100644
--- a/ld/ldwrite.c
+++ b/ld/ldwrite.c
@@ -57,11 +57,14 @@ build_link_order (lang_statement_union_type *statement)
 
 	link_order = bfd_new_link_order (link_info.output_bfd, output_section);
 	if (link_order == NULL)
-	  einfo (_("%F%P: bfd_new_link_order failed\n"));
+	  einfo (_("%F%P: bfd_new_link_order failed: %E\n"));
 
 	link_order->type = bfd_data_link_order;
 	link_order->offset = statement->data_statement.output_offset;
-	link_order->u.data.contents = (bfd_byte *) xmalloc (QUAD_SIZE);
+	link_order->u.data.contents = bfd_alloc (link_info.output_bfd,
+						 QUAD_SIZE);
+	if (link_order->u.data.contents == NULL)
+	  einfo (_("%F%P: bfd_new_link_order failed: %E\n"));
 
 	value = statement->data_statement.value;
 
@@ -167,13 +170,15 @@ build_link_order (lang_statement_union_type *statement)
 
 	link_order = bfd_new_link_order (link_info.output_bfd, output_section);
 	if (link_order == NULL)
-	  einfo (_("%F%P: bfd_new_link_order failed\n"));
+	  einfo (_("%F%P: bfd_new_link_order failed: %E\n"));
 
 	link_order->offset = rs->output_offset;
 	link_order->size = bfd_get_reloc_size (rs->howto);
 
 	link_order->u.reloc.p = (struct bfd_link_order_reloc *)
-	  xmalloc (sizeof (struct bfd_link_order_reloc));
+	  bfd_alloc (link_info.output_bfd, sizeof (struct bfd_link_order_reloc));
+	if (link_order->u.reloc.p == NULL)
+	  einfo (_("%F%P: bfd_new_link_order failed: %E\n"));
 
 	link_order->u.reloc.p->reloc = rs->reloc;
 	link_order->u.reloc.p->addend = rs->addend_value;
@@ -219,7 +224,7 @@ build_link_order (lang_statement_union_type *statement)
 	    link_order = bfd_new_link_order (link_info.output_bfd,
 					     output_section);
 	    if (link_order == NULL)
-	      einfo (_("%F%P: bfd_new_link_order failed\n"));
+	      einfo (_("%F%P: bfd_new_link_order failed: %E\n"));
 
 	    if ((i->flags & SEC_NEVER_LOAD) != 0
 		&& (i->flags & SEC_DEBUGGING) == 0)
@@ -260,7 +265,7 @@ build_link_order (lang_statement_union_type *statement)
 	link_order = bfd_new_link_order (link_info.output_bfd,
 					 output_section);
 	if (link_order == NULL)
-	  einfo (_("%F%P: bfd_new_link_order failed\n"));
+	  einfo (_("%F%P: bfd_new_link_order failed: %E\n"));
 	link_order->type = bfd_data_link_order;
 	link_order->size = statement->padding_statement.size;
 	link_order->offset = statement->padding_statement.output_offset;

-- 
Alan Modra
Australia Development Lab, IBM

                 reply	other threads:[~2023-12-18 12:15 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=ZYA3zY4ZcFOk9MWT@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).