public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] bfd: Add bfd_xalloc, bfd_xzalloc and bfd_xalloc_strdup
@ 2024-04-09 23:01 H.J. Lu
  2024-04-10 23:44 ` Alan Modra
  0 siblings, 1 reply; 2+ messages in thread
From: H.J. Lu @ 2024-04-09 23:01 UTC (permalink / raw)
  To: binutils

Remove bfd_xalloc in binutils.  Add bfd_xalloc, bfd_xzalloc and
bfd_xalloc_strdup to bfd to fix memory leaks in read.c, write.c and
obj-elf.c.

bfd/

	* libbfd.c (bfd_xalloc): New.
	(bfd_xzalloc): Likewise.
	(bfd_xalloc_strdup): Likewise.
	* bfd-in2.h: Regenerated.

binutils/

	* bucomm.c (bfd_xalloc): Removed.
	* bucomm.h (bfd_xalloc): Likewise.

gas/

	* read.c (s_reloc): Replace XNEW with bfd_xalloc.
	* write.c (write_relocs): Replace XCNEWVEC with bfd_xzalloc.
	* config/obj-elf.c (obj_elf_find_and_add_versioned_name): Replace
	xmalloc with bfd_xalloc.  Replace xstrdup with bfd_xalloc_strdup.
---
 bfd/bfd-in2.h        |  6 ++++
 bfd/libbfd.c         | 68 ++++++++++++++++++++++++++++++++++++++++++++
 binutils/bucomm.c    | 13 ---------
 binutils/bucomm.h    |  2 --
 gas/config/obj-elf.c |  4 +--
 gas/read.c           |  2 +-
 gas/write.c          |  5 ++--
 7 files changed, 80 insertions(+), 20 deletions(-)

diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
index d107a22f27e..807a423dd75 100644
--- a/bfd/bfd-in2.h
+++ b/bfd/bfd-in2.h
@@ -172,6 +172,12 @@ void *bfd_alloc (bfd *abfd, bfd_size_type wanted);
 
 void *bfd_zalloc (bfd *abfd, bfd_size_type wanted);
 
+void *bfd_xalloc (bfd *abfd, bfd_size_type wanted);
+
+void *bfd_xzalloc (bfd *abfd, bfd_size_type wanted);
+
+char *bfd_xalloc_strdup (bfd *abfd, const char *str);
+
 void bfd_release (bfd *, void *);
 
 
diff --git a/bfd/libbfd.c b/bfd/libbfd.c
index 2f5ddcaf3b8..b92fa86d4ac 100644
--- a/bfd/libbfd.c
+++ b/bfd/libbfd.c
@@ -481,6 +481,74 @@ bfd_zalloc (bfd *abfd, bfd_size_type size)
   return res;
 }
 
+/*
+FUNCTION
+	bfd_xalloc
+
+SYNOPSIS
+	void *bfd_xalloc (bfd *abfd, bfd_size_type wanted);
+
+DESCRIPTION
+	Allocate a block of @var{wanted} bytes of memory attached to
+	<<abfd>> and return a pointer to it.  Call _bfd_error_handler
+	if memory allocation failed.
+*/
+
+void *
+bfd_xalloc (bfd *abfd, bfd_size_type wanted)
+{
+  void *buf = bfd_alloc (abfd, wanted);
+  if (buf == NULL)
+    _bfd_error_handler
+      /* xgettext:c-format */
+      (_("error: %pB: out of memory allocating %#" PRIx64 " bytes"),
+       abfd, (uint64_t) wanted);
+  return buf;
+}
+
+/*
+FUNCTION
+	bfd_xzalloc
+
+SYNOPSIS
+	void *bfd_xzalloc (bfd *abfd, bfd_size_type wanted);
+
+DESCRIPTION
+	Allocate a block of @var{wanted} bytes of memory attached to
+	<<abfd>>, zero the memory block and return a pointer to it.
+	Call _bfd_error_handler if memory allocation failed.
+*/
+
+void *
+bfd_xzalloc (bfd *abfd, bfd_size_type wanted)
+{
+  void *res = bfd_xalloc (abfd, wanted);
+  memset (res, 0, (size_t) wanted);
+  return res;
+}
+
+/*
+FUNCTION
+	bfd_xalloc_strdup
+
+SYNOPSIS
+	char *bfd_xalloc_strdup (bfd *abfd, const char *str);
+
+DESCRIPTION
+	Allocate a block of memory attached to <<abfd>> and return a
+	copy of @var{str}.  Call _bfd_error_handler if memory allocation
+	failed.
+*/
+
+char *
+bfd_xalloc_strdup (bfd *abfd, const char *str)
+{
+  size_t len = strlen (str) + 1;
+  char *buf = bfd_xalloc (abfd, len);
+  memcpy (buf, str, len);
+  return buf;
+}
+
 /*
 FUNCTION
 	bfd_release
diff --git a/binutils/bucomm.c b/binutils/bucomm.c
index d51d1349f12..5e5b410c688 100644
--- a/binutils/bucomm.c
+++ b/binutils/bucomm.c
@@ -142,19 +142,6 @@ non_fatal (const char *format, ...)
   va_end (args);
 }
 
-/* Like xmalloc except that ABFD's objalloc memory is returned.
-   Use objalloc_free_block to free this memory and all more recently
-   allocated, or more usually, leave it to bfd_close to free.  */
-
-void *
-bfd_xalloc (bfd *abfd, size_t size)
-{
-  void *ret = bfd_alloc (abfd, size);
-  if (ret == NULL)
-    bfd_fatal (NULL);
-  return ret;
-}
-
 /* Set the default BFD target based on the configured target.  Doing
    this permits the binutils to be configured for a particular target,
    and linked against a shared BFD library which was configured for a
diff --git a/binutils/bucomm.h b/binutils/bucomm.h
index f45838ef592..8d9e4b15f31 100644
--- a/binutils/bucomm.h
+++ b/binutils/bucomm.h
@@ -39,8 +39,6 @@ void fatal (const char *, ...) ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
 
 void non_fatal (const char *, ...) ATTRIBUTE_PRINTF_1;
 
-void *bfd_xalloc (bfd *, size_t);
-
 void set_default_bfd_target (void);
 
 void list_matching_formats (char **);
diff --git a/gas/config/obj-elf.c b/gas/config/obj-elf.c
index e28ba0a62d5..177d63d59b4 100644
--- a/gas/config/obj-elf.c
+++ b/gas/config/obj-elf.c
@@ -1851,8 +1851,8 @@ obj_elf_find_and_add_versioned_name (const char *version_name,
 
   /* Add this versioned name to the head of the list,  */
   versioned_name = (struct elf_versioned_name_list *)
-    xmalloc (sizeof (*versioned_name));
-  versioned_name->name = xstrdup (version_name);
+    bfd_xalloc (stdoutput, sizeof (*versioned_name));
+  versioned_name->name = bfd_xalloc_strdup (stdoutput, version_name);
   versioned_name->next = sy_obj->versioned_name;
   sy_obj->versioned_name = versioned_name;
 
diff --git a/gas/read.c b/gas/read.c
index 8026a6cdb65..547365eb6cb 100644
--- a/gas/read.c
+++ b/gas/read.c
@@ -4244,7 +4244,7 @@ s_reloc (int ignore ATTRIBUTE_UNUSED)
     { "64", BFD_RELOC_64 }
   };
 
-  reloc = XNEW (struct reloc_list);
+  reloc = bfd_xalloc (stdoutput, sizeof (struct reloc_list));
 
   if (flag_mri)
     stop = mri_comment_field (&stopc);
diff --git a/gas/write.c b/gas/write.c
index 18cf18fc830..1a3a84ec4f6 100644
--- a/gas/write.c
+++ b/gas/write.c
@@ -1244,7 +1244,7 @@ write_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
   segment_info_type *seginfo = seg_info (sec);
   unsigned int n;
   struct reloc_list *my_reloc_list, **rp, *r;
-  arelent **relocs;
+  arelent **relocs = NULL;
   fixS *fixp;
   fragS *last_frag;
 
@@ -1279,7 +1279,8 @@ write_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
 	rp = &r->next;
     }
 
-  relocs = XCNEWVEC (arelent *, n);
+  if (n != 0)
+    relocs = bfd_xzalloc (stdoutput, sizeof (arelent *) * n);
 
   n = 0;
   r = my_reloc_list;
-- 
2.44.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] bfd: Add bfd_xalloc, bfd_xzalloc and bfd_xalloc_strdup
  2024-04-09 23:01 [PATCH] bfd: Add bfd_xalloc, bfd_xzalloc and bfd_xalloc_strdup H.J. Lu
@ 2024-04-10 23:44 ` Alan Modra
  0 siblings, 0 replies; 2+ messages in thread
From: Alan Modra @ 2024-04-10 23:44 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils

On Tue, Apr 09, 2024 at 04:01:44PM -0700, H.J. Lu wrote:
> Remove bfd_xalloc in binutils.  Add bfd_xalloc, bfd_xzalloc and
> bfd_xalloc_strdup to bfd to fix memory leaks in read.c, write.c and
> obj-elf.c.

NAK.  The "x" names imply a function that does not return on failure,
yet bfd_xallox does.  You even assume that yourself, eg. in the
following:

> +void *
> +bfd_xzalloc (bfd *abfd, bfd_size_type wanted)
> +{
> +  void *res = bfd_xalloc (abfd, wanted);
> +  memset (res, 0, (size_t) wanted);
> +  return res;
> +}

I don't see any point in adding these functions to libbfd.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-04-10 23:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-09 23:01 [PATCH] bfd: Add bfd_xalloc, bfd_xzalloc and bfd_xalloc_strdup H.J. Lu
2024-04-10 23:44 ` Alan Modra

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).