]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
PR32698, potential null pointer dereference in tekhex.c
authorAlan Modra <amodra@gmail.com>
Sat, 15 Feb 2025 06:36:58 +0000 (17:06 +1030)
committerAlan Modra <amodra@gmail.com>
Sat, 15 Feb 2025 22:13:10 +0000 (08:43 +1030)
PR 32698
* tekhex.c (find_chunk): Remove unnecessary casts.
(insert_byte): Check and return status from find_chunk.
(move_section_contents): Likewise.
(tekhex_get_section_contents, tekhex_set_arch_mach): Return
status from move_section_contents.
(first_phase): Check and return status from first_phase.

bfd/tekhex.c

index aab0b2f5592607d15c6026edf0359234964959a1..7e4e698efd6807fdd90fb2b0e8d9170aba81d218 100644 (file)
@@ -327,9 +327,7 @@ find_chunk (bfd *abfd, bfd_vma vma, bool create)
   if (!d && create)
     {
       /* No chunk for this address, so make one up.  */
-      d = (struct data_struct *)
-         bfd_zalloc (abfd, (bfd_size_type) sizeof (struct data_struct));
-
+      d = bfd_zalloc (abfd, sizeof (struct data_struct));
       if (!d)
        return NULL;
 
@@ -340,17 +338,20 @@ find_chunk (bfd *abfd, bfd_vma vma, bool create)
   return d;
 }
 
-static void
+static bool
 insert_byte (bfd *abfd, int value, bfd_vma addr)
 {
   if (value != 0)
     {
       /* Find the chunk that this byte needs and put it in.  */
       struct data_struct *d = find_chunk (abfd, addr, true);
+      if (!d)
+       return false;
 
       d->chunk_data[addr & CHUNK_MASK] = value;
       d->chunk_init[(addr & CHUNK_MASK) / CHUNK_SPAN] = 1;
     }
+  return true;
 }
 
 /* The first pass is to find the names of all the sections, and see
@@ -374,7 +375,8 @@ first_phase (bfd *abfd, int type, char *src, char * src_end)
 
       while (*src && src < src_end - 1)
        {
-         insert_byte (abfd, HEX (src), addr);
+         if (!insert_byte (abfd, HEX (src), addr))
+           return false;
          src += 2;
          addr++;
        }
@@ -628,7 +630,7 @@ tekhex_object_p (bfd *abfd)
   return _bfd_no_cleanup;
 }
 
-static void
+static bool
 move_section_contents (bfd *abfd,
                       asection *section,
                       const void * locationp,
@@ -653,6 +655,8 @@ move_section_contents (bfd *abfd,
        {
          /* Different chunk, so move pointer. */
          d = find_chunk (abfd, chunk_number, must_write);
+         if (!d)
+           return false;
          prev_number = chunk_number;
        }
 
@@ -671,22 +675,19 @@ move_section_contents (bfd *abfd,
 
       location++;
     }
+  return true;
 }
 
 static bool
 tekhex_get_section_contents (bfd *abfd,
                             asection *section,
-                            void * locationp,
+                            void *location,
                             file_ptr offset,
                             bfd_size_type count)
 {
-  if (section->flags & (SEC_LOAD | SEC_ALLOC))
-    {
-      move_section_contents (abfd, section, locationp, offset, count, true);
-      return true;
-    }
-
-  return false;
+  if ((section->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
+    return false;
+  return move_section_contents (abfd, section, location, offset, count, true);
 }
 
 static bool
@@ -704,18 +705,13 @@ tekhex_set_arch_mach (bfd *abfd,
 static bool
 tekhex_set_section_contents (bfd *abfd,
                             sec_ptr section,
-                            const void * locationp,
+                            const void *location,
                             file_ptr offset,
-                            bfd_size_type bytes_to_do)
+                            bfd_size_type count)
 {
-  if (section->flags & (SEC_LOAD | SEC_ALLOC))
-    {
-      move_section_contents (abfd, section, locationp, offset, bytes_to_do,
-                            false);
-      return true;
-    }
-
-  return false;
+  if ((section->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
+    return false;
+  return move_section_contents (abfd, section, location, offset, count, false);
 }
 
 static void