From 25475bdf4280936af1d5117adf755b2086a4c1bb Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Fri, 7 Oct 2022 12:04:43 +0000 Subject: [PATCH] zdump: read_elf_phdrs: fix `-Wconversion` issue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `ehdr->e_phoff` has the type `Elf64_Off` and this maps to `uint64_t`, but `off_t` is signed. Let's add a check for this. Signed-off-by: Marc Hartmayer Reviewed-by: Steffen Eiden Signed-off-by: Jan Höppner --- zdump/df_elf.c | 5 ++++- zdump/zg.h | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/zdump/df_elf.c b/zdump/df_elf.c index 257ffaa9..626f35be 100644 --- a/zdump/df_elf.c +++ b/zdump/df_elf.c @@ -74,10 +74,13 @@ Elf64_Phdr *read_elf_phdrs(const struct zg_fh *fh, const Elf64_Ehdr *ehdr, unsig return NULL; } + if (ehdr->e_phoff > OFF_T_MAX) + ERR_EXIT("Unsupported offset"); + /* Cannot wraparound since `Elf64_Half`` is `uint16_t` */ phdrs_size = sizeof(*phdrs) * phnum; phdrs = util_malloc(phdrs_size); - zg_seek(fh, ehdr->e_phoff, ZG_CHECK); + zg_seek(fh, (off_t)ehdr->e_phoff, ZG_CHECK); zg_read(fh, phdrs, phdrs_size, ZG_CHECK); *phdr_count = phnum; return phdrs; diff --git a/zdump/zg.h b/zdump/zg.h index 977d27c3..d4f3d62b 100644 --- a/zdump/zg.h +++ b/zdump/zg.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,8 @@ #define U32_MAX ((u32) -1) #define U16_MAX ((u16) -1) #define U8_MAX ((u8) -1) +#define OFF_T_MAX LONG_MAX +STATIC_ASSERT(sizeof(off_t) == sizeof(long)) /* * IEC definitions