summaryrefslogtreecommitdiff
path: root/bfd/libbfd.c
diff options
context:
space:
mode:
authorNick Clifton <nickc@redhat.com>2008-02-20 17:42:36 +0000
committerNick Clifton <nickc@redhat.com>2008-02-20 17:42:36 +0000
commitb3507c293a3ef2f9932e7be5ee5a726ce41abc97 (patch)
tree6ecaf79110054c19efe4d2cdad097bd39db0c0c6 /bfd/libbfd.c
parent8c5e25b1fcfd570112405e00762404deff4822f4 (diff)
downloadgdb-b3507c293a3ef2f9932e7be5ee5a726ce41abc97.tar.gz
PR 868
* libbfd.c (bfd_realloc_or_free): New function. Performs like bfd_realloc, but if the (re)allocation fails, the pointer is freed. * libbfd-in.h: Prototype. * libbfd.h: Regenerate. * bfdio.c (bfd_bwrite): Use the new function. (bfd_seek): Likewise. * bfdwin.c:(bfd_get_file_window): Likewise. * elf-strtab.c (_bfd_elf_strtab_add): Likewise. * elf32-ppc.c (ppc_elf_relax_section): Likewise. * elf32-xtensa.c (vsprintf_msg): Likewise. * mach-o.c (bfd_mach_o_core_fetch_environment): Likewise. * stabs.c (_bfd_link_seciton_stabs): Likewise. * vms-misc.c (_bfd_vms_get_record): Likewise. * vms-tir.c (check_section): Likewise. * vms.c (vms_new_section_hook): Likewise. * elf32-arm.c (elf32_arm_section_map_add): Check that the allocation of sec_data->map succeeded before using it. * elflink.c (elf_link_output_sym): Do not overwrite finfo-> symshndxbuf until it is known that the reallocation succeeded.
Diffstat (limited to 'bfd/libbfd.c')
-rw-r--r--bfd/libbfd.c30
1 files changed, 29 insertions, 1 deletions
diff --git a/bfd/libbfd.c b/bfd/libbfd.c
index bf49a2e1db1..a225397c3e7 100644
--- a/bfd/libbfd.c
+++ b/bfd/libbfd.c
@@ -1,6 +1,6 @@
/* Assorted BFD support routines, only used internally.
Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
- 2000, 2001, 2002, 2003, 2004, 2005, 2007
+ 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
Free Software Foundation, Inc.
Written by Cygnus Support.
@@ -265,6 +265,34 @@ bfd_realloc2 (void *ptr, bfd_size_type nmemb, bfd_size_type size)
return ret;
}
+/* Reallocate memory using realloc.
+ If this fails the pointer is freed before returning. */
+
+void *
+bfd_realloc_or_free (void *ptr, bfd_size_type size)
+{
+ size_t amount = (size_t) size;
+ void *ret;
+
+ if (size != amount)
+ ret = NULL;
+ else if (ptr == NULL)
+ ret = malloc (amount);
+ else
+ ret = realloc (ptr, amount);
+
+ if (ret == NULL)
+ {
+ if (amount > 0)
+ bfd_set_error (bfd_error_no_memory);
+
+ if (ptr != NULL)
+ free (ptr);
+ }
+
+ return ret;
+}
+
/* Allocate memory using malloc and clear it. */
void *