* [PATCH] bfd: Add bfd_xmemdup to fix memory leaks in gas
@ 2024-04-09 23:03 H.J. Lu
0 siblings, 0 replies; only message in thread
From: H.J. Lu @ 2024-04-09 23:03 UTC (permalink / raw)
To: binutils
Add bfd_xmemdup to fix a memory leak in allocate_filename_to_slot and
obj_elf_section.
bfd/
* libbfd.c (bfd_xmemdup): New.
* bfd-in2.h: Regenerated.
gas/
* as.h (bfd_xmemdup0): New.
* dwarf2dbg.c (allocate_filename_to_slot): Replace xmemdup0
with bfd_xmemdup0.
* config/obj-elf.c (obj_elf_section): Likewise.
---
bfd/bfd-in2.h | 3 +++
bfd/libbfd.c | 25 +++++++++++++++++++++++++
gas/as.h | 6 ++++++
gas/config/obj-elf.c | 3 ++-
gas/dwarf2dbg.c | 4 +++-
5 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
index 807a423dd75..c249c7ac13a 100644
--- a/bfd/bfd-in2.h
+++ b/bfd/bfd-in2.h
@@ -176,6 +176,9 @@ void *bfd_xalloc (bfd *abfd, bfd_size_type wanted);
void *bfd_xzalloc (bfd *abfd, bfd_size_type wanted);
+void * bfd_xmemdup (bfd *abfd, const void *input,
+ size_t copy_size, size_t alloc_size);
+
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 b92fa86d4ac..5bf90f655c9 100644
--- a/bfd/libbfd.c
+++ b/bfd/libbfd.c
@@ -527,6 +527,31 @@ bfd_xzalloc (bfd *abfd, bfd_size_type wanted)
return res;
}
+/*
+FUNCTION
+ bfd_xmemdup
+
+SYNOPSIS
+ void * bfd_xmemdup (bfd *abfd, const void *input,
+ size_t copy_size, size_t alloc_size);
+
+DESCRIPTION
+ Allocate a block of @var{alloc_size} bytes of memory attached
+ to <<abfd>>, copy @var{copy_size} bytes from @var{input} and
+ return the allocated memory block. Call _bfd_error_handler if
+ memory allocation failed.
+*/
+
+void *
+bfd_xmemdup (bfd *abfd, const void *input, size_t copy_size,
+ size_t alloc_size)
+{
+ void *output = bfd_xalloc (abfd, alloc_size);
+ if (alloc_size > copy_size)
+ memset ((char *) output + copy_size, 0, alloc_size - copy_size);
+ return (void *) memcpy (output, input, copy_size);
+}
+
/*
FUNCTION
bfd_xalloc_strdup
diff --git a/gas/as.h b/gas/as.h
index 69d7ae2cd17..c168a09ec7e 100644
--- a/gas/as.h
+++ b/gas/as.h
@@ -539,6 +539,12 @@ xmemdup0 (const char *in, size_t len)
return xmemdup (in, len, len + 1);
}
+static inline char *
+bfd_xmemdup0 (bfd *abfd, const char *in, size_t len)
+{
+ return bfd_xmemdup (abfd, in, len, len + 1);
+}
+
struct expressionS;
struct fix;
typedef struct symbol symbolS;
diff --git a/gas/config/obj-elf.c b/gas/config/obj-elf.c
index 177d63d59b4..4f6e3a488de 100644
--- a/gas/config/obj-elf.c
+++ b/gas/config/obj-elf.c
@@ -1409,7 +1409,8 @@ obj_elf_section (int push)
(void) restore_line_pointer (c);
length = input_line_pointer - beg;
if (length)
- match.linked_to_symbol_name = xmemdup0 (beg, length);
+ match.linked_to_symbol_name
+ = bfd_xmemdup0 (stdoutput, beg, length);
}
}
else if ((attr & SHF_LINK_ORDER) != 0 && inherit
diff --git a/gas/dwarf2dbg.c b/gas/dwarf2dbg.c
index 0871c3c10c9..de7249da64b 100644
--- a/gas/dwarf2dbg.c
+++ b/gas/dwarf2dbg.c
@@ -882,7 +882,9 @@ allocate_filename_to_slot (const char *dirname,
dirs = XCNEWVEC (char *, dirs_allocated);
}
- dirs[files[num].dir] = xmemdup0 (dirname, strlen (dirname));
+ dirs[files[num].dir] = bfd_xmemdup0 (stdoutput,
+ dirname,
+ strlen (dirname));
}
return true;
--
2.44.0
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2024-04-09 23:03 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-09 23:03 [PATCH] bfd: Add bfd_xmemdup to fix memory leaks in gas H.J. Lu
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).