summaryrefslogtreecommitdiffstats
path: root/libelf/elf_begin.c
diff options
context:
space:
mode:
authorMark Wielaard <[email protected]>2021-12-19 15:52:32 +0100
committerMark Wielaard <[email protected]>2021-12-19 15:52:32 +0100
commitaab1cc4c0313cd0a0ad9b7ecf794301126daab15 (patch)
tree9d61955e4112b812cece40a882c7676d92e8d668 /libelf/elf_begin.c
parent163d1e9582efa8248057b088ad9c28fc8d24512e (diff)
libelf: Only set shdr state when there is at least one shdr
The elf shdr state only needs to be set when scncnt is at least one. Otherwise e_shoff can be bogus. Also use unsigned arithmetic for checking e_shoff alignment. Signed-off-by: Mark Wielaard <[email protected]>
Diffstat (limited to 'libelf/elf_begin.c')
-rw-r--r--libelf/elf_begin.c16
1 files changed, 10 insertions, 6 deletions
diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c
index bd3399de..0c9a988d 100644
--- a/libelf/elf_begin.c
+++ b/libelf/elf_begin.c
@@ -383,7 +383,7 @@ file_read_elf (int fildes, void *map_address, unsigned char *e_ident,
if (map_address != NULL && e_ident[EI_DATA] == MY_ELFDATA
&& cmd != ELF_C_READ_MMAP /* We need a copy to be able to write. */
&& (ALLOW_UNALIGNED
- || (((uintptr_t) ((char *) ehdr + e_shoff)
+ || ((((uintptr_t) ehdr + e_shoff)
& (__alignof__ (Elf32_Shdr) - 1)) == 0)))
{
if (unlikely (scncnt > 0 && e_shoff >= maxsize)
@@ -395,8 +395,10 @@ file_read_elf (int fildes, void *map_address, unsigned char *e_ident,
__libelf_seterrno (ELF_E_INVALID_ELF);
return NULL;
}
- elf->state.elf32.shdr
- = (Elf32_Shdr *) ((char *) ehdr + e_shoff);
+
+ if (scncnt > 0)
+ elf->state.elf32.shdr
+ = (Elf32_Shdr *) ((char *) ehdr + e_shoff);
for (size_t cnt = 0; cnt < scncnt; ++cnt)
{
@@ -485,15 +487,17 @@ file_read_elf (int fildes, void *map_address, unsigned char *e_ident,
if (map_address != NULL && e_ident[EI_DATA] == MY_ELFDATA
&& cmd != ELF_C_READ_MMAP /* We need a copy to be able to write. */
&& (ALLOW_UNALIGNED
- || (((uintptr_t) ((char *) ehdr + e_shoff)
+ || ((((uintptr_t) ehdr + e_shoff)
& (__alignof__ (Elf64_Shdr) - 1)) == 0)))
{
if (unlikely (scncnt > 0 && e_shoff >= maxsize)
|| unlikely (maxsize - e_shoff
< scncnt * sizeof (Elf64_Shdr)))
goto free_and_out;
- elf->state.elf64.shdr
- = (Elf64_Shdr *) ((char *) ehdr + e_shoff);
+
+ if (scncnt > 0)
+ elf->state.elf64.shdr
+ = (Elf64_Shdr *) ((char *) ehdr + e_shoff);
for (size_t cnt = 0; cnt < scncnt; ++cnt)
{