]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
tidy elf attr error handling
authorAlan Modra <amodra@gmail.com>
Tue, 10 Feb 2026 00:16:47 +0000 (10:46 +1030)
committerAlan Modra <amodra@gmail.com>
Tue, 10 Feb 2026 00:16:47 +0000 (10:46 +1030)
Add a missing error check, and make another error check a little more
stringent.  If it were ever possible for oav2_parse_attr to return
zero, the loop would not terminate.

* elf-attrs.c (oav2_parse_subsection): Check read_ntbs return
for errors.  Tidy loop reading attrs, and error on <= 0.

bfd/elf-attrs.c

index c4f81ba5f6ba569dc173b7391a79971ae2a20069..bb542d80ab7da0588dca681e34bf42921ac7d9d0 100644 (file)
@@ -2801,6 +2801,8 @@ oav2_parse_subsection (bfd *abfd,
        subsection_name.  Either the string has to be freed in case of errors,
        or its ownership must be transferred.  */
     int read = read_ntbs (abfd, cursor, subsection_name_end, &subsection_name);
+    if (read <= 0)
+      goto error;
     total_read += read;
     cursor += read;
   }
@@ -2848,25 +2850,22 @@ oav2_parse_subsection (bfd *abfd,
     (subsection_name, scope, comprehension_raw, value_encoding);
 
   /* A subsection can be empty, so 'cursor' can be equal to 'end' here.  */
-  bool err = false;
-  while (!err && cursor < end)
+  while (cursor < end)
     {
       obj_attr_v2_t *attr;
       ssize_t read = oav2_parse_attr (abfd, cursor, end, value_encoding, &attr);
+      if (read <= 0)
+       {
+         _bfd_elf_obj_attr_subsection_v2_free (*subsec);
+         *subsec = NULL;
+         return -1;
+       }
       if (attr != NULL)
        LINKED_LIST_APPEND (obj_attr_v2_t) (*subsec, attr);
       total_read += read;
-      err |= (read < 0);
       cursor += read;
     }
 
-  if (err)
-    {
-      _bfd_elf_obj_attr_subsection_v2_free (*subsec);
-      *subsec = NULL;
-      return -1;
-    }
-
   BFD_ASSERT (cursor == end);
   return total_read;