PR26412 UBSAN: objcopy.c:3026 null pointer fwrite
authorAlan Modra <amodra@gmail.com>
Wed, 26 Aug 2020 00:41:38 +0000 (10:11 +0930)
committerAlan Modra <amodra@gmail.com>
Wed, 26 Aug 2020 13:53:43 +0000 (23:23 +0930)
PR 26412
* objcopy.c (copy_object): Don't fwrite NULL contents.

binutils/ChangeLog
binutils/objcopy.c

index 352e506544710532c72c3fdbbcb300e30c2c20b8..bf56cc1a59fb5e2387ac78072c19d26d2b2e1be9 100644 (file)
@@ -1,3 +1,8 @@
+2020-08-26  Alan Modra  <amodra@gmail.com>
+
+       PR 26412
+       * objcopy.c (copy_object): Don't fwrite NULL contents.
+
 2020-08-26  Katayama Hirofumi  <katayama.hirofumi.mz@gmail.com>
 
        PR 26340
index c5af179b17e0690372cf40141ead226c2127e5f2..1cbcd17a7ec63dcec0236c91f1ba89d4b63d3c16 100644 (file)
@@ -3011,7 +3011,8 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
            }
 
          bfd_size_type size = bfd_section_size (osec);
-         /* Note - we allow the dumping of zero-sized sections.  */
+         /* Note - we allow the dumping of zero-sized sections,
+            creating an empty file.  */
 
          f = fopen (pdump->filename, FOPEN_WB);
          if (f == NULL)
@@ -3023,7 +3024,7 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
 
          if (bfd_malloc_and_get_section (ibfd, osec, &contents))
            {
-             if (fwrite (contents, 1, size, f) != size)
+             if (size != 0 && fwrite (contents, 1, size, f) != size)
                {
                  non_fatal (_("error writing section contents to %s (error: %s)"),
                             pdump->filename,