From: Mark Wielaard Date: Tue, 14 Oct 2025 16:21:54 +0000 (+0200) Subject: libelf: elf[32|64]_offscn shouldn't return a result for empty sections X-Git-Tag: elfutils-0.194~8 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4e2c5051cf3d0540e7c0612a618271cb8ae7bfdc;p=thirdparty%2Felfutils.git libelf: elf[32|64]_offscn shouldn't return a result for empty sections 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 --- diff --git a/libelf/elf32_offscn.c b/libelf/elf32_offscn.c index 9e757c84..1a9a3b0a 100644 --- a/libelf/elf32_offscn.c +++ b/libelf/elf32_offscn.c @@ -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;