PR 868
authorNick Clifton <nickc@redhat.com>
Wed, 20 Feb 2008 17:42:36 +0000 (17:42 +0000)
committerNick Clifton <nickc@redhat.com>
Wed, 20 Feb 2008 17:42:36 +0000 (17:42 +0000)
   * 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.

17 files changed:
bfd/ChangeLog
bfd/bfdio.c
bfd/bfdwin.c
bfd/dwarf2.c
bfd/elf-strtab.c
bfd/elf32-arm.c
bfd/elf32-ppc.c
bfd/elf32-xtensa.c
bfd/elflink.c
bfd/libbfd-in.h
bfd/libbfd.c
bfd/libbfd.h
bfd/mach-o.c
bfd/stabs.c
bfd/vms-misc.c
bfd/vms-tir.c
bfd/vms.c

index 9a4564828801210ca707c556e01f815dcc6d9ffc..a9d7988393c5cd07b2178c6f2d3cbd48b2339ba4 100644 (file)
@@ -1,3 +1,27 @@
+2008-02-20  Nick Clifton  <nickc@redhat.com>
+
+       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.
+
 2008-02-20  Diogo de Carvalho Kraemer  <diogo@kraemer.eng.br>
            Nick Clifton  <nickc@redhat.com>
 
index 080cd947f81834a0a7e79c104d2ea235305566aa..d8aa1de99926761fa015bab24c49798563cec37b 100644 (file)
@@ -1,7 +1,7 @@
 /* Low-level I/O routines for BFDs.
 
    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
+   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
    Free Software Foundation, Inc.
 
    Written by Cygnus Support.
@@ -174,8 +174,8 @@ bfd_bwrite (const void *ptr, bfd_size_type size, bfd *abfd)
          newsize = (bim->size + 127) & ~(bfd_size_type) 127;
          if (newsize > oldsize)
            {
-             bim->buffer = bfd_realloc (bim->buffer, newsize);
-             if (bim->buffer == 0)
+             bim->buffer = bfd_realloc_or_free (bim->buffer, newsize);
+             if (bim->buffer == NULL)
                {
                  bim->size = 0;
                  return 0;
@@ -298,8 +298,8 @@ bfd_seek (bfd *abfd, file_ptr position, int direction)
              newsize = (bim->size + 127) & ~(bfd_size_type) 127;
              if (newsize > oldsize)
                {
-                 bim->buffer = bfd_realloc (bim->buffer, newsize);
-                 if (bim->buffer == 0)
+                 bim->buffer = bfd_realloc_or_free (bim->buffer, newsize);
+                 if (bim->buffer == NULL)
                    {
                      bim->size = 0;
                      return -1;
index a44265efa752e1f572c9644b4ef93ff1281218ac..dfe89cb45c3cd90ef9421cd7238b040a01a04e18 100644 (file)
@@ -1,5 +1,5 @@
 /* Support for memory-mapped windows into a BFD.
-   Copyright 1995, 1996, 2001, 2002, 2003, 2005, 2007
+   Copyright 1995, 1996, 2001, 2002, 2003, 2005, 2007, 2008
    Free Software Foundation, Inc.
    Written by Cygnus Support.
 
@@ -224,16 +224,16 @@ bfd_get_file_window (bfd *abfd,
   if (debug_windows)
     fprintf (stderr, "\n\t%s(%6ld)",
             i->data ? "realloc" : " malloc", (long) size_to_alloc);
-  i->data = bfd_realloc (i->data, size_to_alloc);
+  i->data = bfd_realloc_or_free (i->data, size_to_alloc);
   if (debug_windows)
     fprintf (stderr, "\t-> %p\n", i->data);
-  i->refcount = 1;
   if (i->data == NULL)
     {
       if (size_to_alloc == 0)
        return TRUE;
       return FALSE;
     }
+  i->refcount = 1;
   if (bfd_seek (abfd, offset, SEEK_SET) != 0)
     return FALSE;
   i->size = bfd_bread (i->data, size, abfd);
index 7ad218e6a0f802d94814e2b25b51f8ef7c905645..830b747108a599c4a72073da978f378b02def2c2 100644 (file)
@@ -1255,6 +1255,7 @@ decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
          if (tmp == NULL)
            {
              free (table->dirs);
+             free (table);
              return NULL;
            }
          table->dirs = tmp;
index 018c42cbc1f7ab8fad6f121050935b57aa021248..f5013d15e4a725d8f5b8c2969afda602efb6194f 100644 (file)
@@ -1,5 +1,5 @@
 /* ELF strtab with GC and suffix merging support.
-   Copyright 2001, 2002, 2003, 2005, 2006, 2007
+   Copyright 2001, 2002, 2003, 2005, 2006, 2007, 2008
    Free Software Foundation, Inc.
    Written by Jakub Jelinek <jakub@redhat.com>.
 
@@ -166,7 +166,7 @@ _bfd_elf_strtab_add (struct elf_strtab_hash *tab,
        {
          bfd_size_type amt = sizeof (struct elf_strtab_hash_entry *);
          tab->alloced *= 2;
-         tab->array = bfd_realloc (tab->array, tab->alloced * amt);
+         tab->array = bfd_realloc_or_free (tab->array, tab->alloced * amt);
          if (tab->array == NULL)
            return (bfd_size_type) -1;
        }
index 7218ba8c3c9814439b89d56b4733a39d1cd98b80..6f66a7150dff32e7e481dfca56dd60494e841385 100644 (file)
@@ -2948,12 +2948,15 @@ elf32_arm_section_map_add (asection *sec, char type, bfd_vma vma)
   if (sec_data->mapcount > sec_data->mapsize)
     {
       sec_data->mapsize *= 2;
-      sec_data->map = bfd_realloc (sec_data->map, sec_data->mapsize
-                                    * sizeof (elf32_arm_section_map));
+      sec_data->map = bfd_realloc_or_free (sec_data->map, sec_data->mapsize
+                                          * sizeof (elf32_arm_section_map));
+    }
+
+  if (sec_data->map)
+    {
+      sec_data->map[newidx].vma = vma;
+      sec_data->map[newidx].type = type;
     }
-  
-  sec_data->map[newidx].vma = vma;
-  sec_data->map[newidx].type = type;
 }
 
 
index a301638eac51e6755bd22d6576f704d3c1a87695..0e215932de1f6e4f6abbda1fae630e1c068a7091 100644 (file)
@@ -5540,7 +5540,7 @@ ppc_elf_relax_section (bfd *abfd,
        }
       while (fixups);
 
-      contents = bfd_realloc (contents, trampoff);
+      contents = bfd_realloc_or_free (contents, trampoff);
       if (contents == NULL)
        goto error_return;
 
index 05ee9dfcf385c8f1f2ef0a1097fb887d0a3bd55f..ec6d4636e2a471362ebe3c483fad867d11e39252 100644 (file)
@@ -1,5 +1,5 @@
 /* Xtensa-specific support for 32-bit ELF.
-   Copyright 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
+   Copyright 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
    This file is part of BFD, the Binary File Descriptor library.
 
@@ -1758,12 +1758,15 @@ vsprint_msg (const char *origmsg, const char *fmt, int arglen, ...)
   len = orig_len + strlen (fmt) + arglen + 20;
   if (len > alloc_size)
     {
-      message = (char *) bfd_realloc (message, len);
+      message = (char *) bfd_realloc_or_free (message, len);
       alloc_size = len;
     }
-  if (!is_append)
-    memcpy (message, origmsg, orig_len);
-  vsprintf (message + orig_len, fmt, ap);
+  if (message != NULL)
+    {
+      if (!is_append)
+       memcpy (message, origmsg, orig_len);
+      vsprintf (message + orig_len, fmt, ap);
+    }
   VA_CLOSE (ap);
   return message;
 }
index a00634be52b9c5fd97e0ee352f37cbc9d316e462..ab19be255a6518f66a96dedb75224d7522b2b685 100644 (file)
@@ -8180,9 +8180,10 @@ elf_link_output_sym (struct elf_final_link_info *finfo,
          bfd_size_type amt;
 
          amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
-         finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
+         destshndx = bfd_realloc (destshndx, amt * 2);
          if (destshndx == NULL)
            return FALSE;
+         finfo->symshndxbuf = destshndx;
          memset ((char *) destshndx + amt, 0, amt);
          finfo->shndxbuf_size *= 2;
        }
index ec11bd5d6b03f59468db356a02d1d293933e6e3e..f57d450a4fb8c07ca9a340c30f586e009fa47fff 100644 (file)
@@ -99,6 +99,8 @@ extern void *bfd_malloc
   (bfd_size_type);
 extern void *bfd_realloc
   (void *, bfd_size_type);
+extern void *bfd_realloc_or_free
+  (void *, bfd_size_type);
 extern void *bfd_zmalloc
   (bfd_size_type);
 extern void *bfd_malloc2
index bf49a2e1db14671145790ef88433016f394f28df..a225397c3e780fd18b39c940d19f78edd3fad542 100644 (file)
@@ -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 *
index cb6544a009a85e12e5cd32e59e4e9d45a7dd1032..cfc364f2435fc9bca9a538387363572fac11ea3a 100644 (file)
@@ -104,6 +104,8 @@ extern void *bfd_malloc
   (bfd_size_type);
 extern void *bfd_realloc
   (void *, bfd_size_type);
+extern void *bfd_realloc_or_free
+  (void *, bfd_size_type);
 extern void *bfd_zmalloc
   (bfd_size_type);
 extern void *bfd_malloc2
index 166dd6551ec995f260e03c00ac3dd8b8695f6431..bde89b04034cc78110a70aa277b2c126f9e3c053 100644 (file)
@@ -1,5 +1,5 @@
 /* Mach-O support for BFD.
-   Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
+   Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
    Free Software Foundation, Inc.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -1923,13 +1923,18 @@ bfd_mach_o_core_fetch_environment (bfd *abfd,
              if (size > (end - start))
                size = (end - start);
 
-             buf = bfd_realloc (buf, size);
-
+             buf = bfd_realloc_or_free (buf, size);
+             if (buf == NULL)
+               return -1;
+             
              bfd_seek (abfd, end - size, SEEK_SET);
              nread = bfd_bread (buf, size, abfd);
 
              if (nread != size)
-               return -1;
+               {
+                 free (buf);
+                 return -1;
+               }
 
              for (offset = 4; offset <= size; offset += 4)
                {
@@ -1952,6 +1957,7 @@ bfd_mach_o_core_fetch_environment (bfd *abfd,
                      *rlen = top - bottom;
 
                      memcpy (*rbuf, buf + size - *rlen, *rlen);
+                     free (buf);
                      return 0;
                    }
                }
@@ -1961,6 +1967,8 @@ bfd_mach_o_core_fetch_environment (bfd *abfd,
 
              size *= 2;
            }
+
+         free (buf);
        }
     }
 
index a9fe409f1ecb30fb2d8973b501ab2bfe6304c869..72e312cc5201f2874cf16fa40647bc253d08a485 100644 (file)
@@ -337,7 +337,7 @@ _bfd_link_section_stabs (bfd *abfd,
                      if (num_chars >= buf_len)
                        {
                          buf_len += 32 * 1024;
-                         symb = bfd_realloc (symb, buf_len);
+                         symb = bfd_realloc_or_free (symb, buf_len);
                          if (symb == NULL)
                            goto error_return;
                          symb_rover = symb + num_chars;
@@ -393,7 +393,7 @@ _bfd_link_section_stabs (bfd *abfd,
                goto error_return;
              t->sum_chars = sum_chars;
              t->num_chars = num_chars;
-             t->symb = bfd_realloc (symb, num_chars); /* Trim data down.  */
+             t->symb = symb = bfd_realloc_or_free (symb, num_chars); /* Trim data down.  */
              t->next = incl_entry->totals;
              incl_entry->totals = t;
            }
index 98fbb0e299719c3217506f42cd10e1f549e718d5..87f7ce8088b9cd2cd0f5fa9139beaa8342c86557 100644 (file)
@@ -1,7 +1,7 @@
 /* vms-misc.c -- Miscellaneous functions for VAX (openVMS/VAX) and
    EVAX (openVMS/Alpha) files.
    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
-   2007 Free Software Foundation, Inc.
+   2007, 2008  Free Software Foundation, Inc.
 
    Written by Klaus K"ampf (kkaempf@rmi.de)
 
@@ -349,7 +349,7 @@ _bfd_vms_get_record (bfd * abfd)
 
       if (PRIV (rec_length) > PRIV (buf_size))
        {
-         PRIV (vms_buf) = bfd_realloc (vms_buf,
+         PRIV (vms_buf) = bfd_realloc_or_free (vms_buf,
                                        (bfd_size_type) PRIV (rec_length));
          vms_buf = PRIV (vms_buf);
          if (vms_buf == 0)
index de20d4f53628c59db1a2b5ac93cba7c7ae35cac3..e6ec6ebcc37610f3d32241517168072e6d1d1a2f 100644 (file)
@@ -1,6 +1,6 @@
 /* vms-tir.c -- BFD back-end for VAX (openVMS/VAX) and
    EVAX (openVMS/Alpha) files.
-   Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2007
+   Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2007, 2008
    Free Software Foundation, Inc.
 
    TIR record handling functions
@@ -51,8 +51,8 @@ check_section (bfd * abfd, int size)
   if (offset + size > PRIV (image_section)->size)
     {
       PRIV (image_section)->contents
-       = bfd_realloc (PRIV (image_section)->contents, offset + size);
-      if (PRIV (image_section)->contents == 0)
+       = bfd_realloc_or_free (PRIV (image_section)->contents, offset + size);
+      if (PRIV (image_section)->contents == NULL)
        {
          (*_bfd_error_handler) (_("No Mem !"));
          return -1;
@@ -833,8 +833,8 @@ alloc_section (bfd * abfd, unsigned int idx)
 
   amt = idx + 1;
   amt *= sizeof (asection *);
-  PRIV (sections) = bfd_realloc (PRIV (sections), amt);
-  if (PRIV (sections) == 0)
+  PRIV (sections) = bfd_realloc_or_free (PRIV (sections), amt);
+  if (PRIV (sections) == NULL)
     return -1;
 
   while (PRIV (section_count) <= idx)
index bb5ab118c723130bf2550ebfd6da80babad94f49..1944c1ee06736cc6c032ebc81b2d16e5cb417616 100644 (file)
--- a/bfd/vms.c
+++ b/bfd/vms.c
@@ -1,7 +1,7 @@
 /* vms.c -- BFD back-end for VAX (openVMS/VAX) and
    EVAX (openVMS/Alpha) files.
    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
-   2006, 2007 Free Software Foundation, Inc.
+   2006, 2007, 2008 Free Software Foundation, Inc.
 
    Written by Klaus K"ampf (kkaempf@rmi.de)
 
@@ -476,7 +476,7 @@ vms_new_section_hook (bfd * abfd, asection *section)
     {
       bfd_size_type amt = section_count;
       amt *= sizeof (asection *);
-      PRIV (sections) = bfd_realloc (PRIV (sections), amt);
+      PRIV (sections) = bfd_realloc_or_free (PRIV (sections), amt);
       if (PRIV (sections) == NULL)
        return FALSE;
       PRIV (section_count) = section_count;
This page took 0.071523 seconds and 4 git commands to generate.