]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
libelf: elf[32|64]_offscn shouldn't return a result for empty sections
authorMark Wielaard <mark@klomp.org>
Tue, 14 Oct 2025 16:21:54 +0000 (18:21 +0200)
committerMark Wielaard <mark@klomp.org>
Wed, 15 Oct 2025 13:57:05 +0000 (15:57 +0200)
offscn sets the result before checking the section isn't empty. It
assumes the result will be reset for the next section that matches the
given offset. But this might not be the case, for example if this was
the last section. It will then return that section (and set elf_errno)
instead of returning NULL to indicate no non-empty section matched.

* libelf/elf32_offscn.c (offscn): Move assignment to result
after empty size check.

Signed-off-by: Mark Wielaard <mark@klomp.org>
libelf/elf32_offscn.c

index 9e757c840948549bafdb632e29d3dba83d5b2241..1a9a3b0a94d7bf4ce9dad74725e941969ff2af54 100644 (file)
@@ -73,14 +73,15 @@ elfw2(LIBELFBITS,offscn) (Elf *elf, ElfW2(LIBELFBITS,Off) offset)
       for (unsigned int i = 0; i < runp->cnt; ++i)
        if (runp->data[i].shdr.ELFW(e,LIBELFBITS)->sh_offset == offset)
          {
-           result = &runp->data[i];
-
            /* If this section is empty, the following one has the same
               sh_offset.  We presume the caller is looking for a nonempty
               section, so keep looking if this one is empty.  */
            if (runp->data[i].shdr.ELFW(e,LIBELFBITS)->sh_size != 0
                && runp->data[i].shdr.ELFW(e,LIBELFBITS)->sh_type != SHT_NOBITS)
-             goto out;
+             {
+               result = &runp->data[i];
+               goto out;
+             }
          }
 
       runp = runp->next;