public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix computations with (potentially) NULL pointer
@ 2023-11-13 22:58 Paul Pluzhnikov
  2023-11-14 12:57 ` Mark Wielaard
  0 siblings, 1 reply; 8+ messages in thread
From: Paul Pluzhnikov @ 2023-11-13 22:58 UTC (permalink / raw)
  To: elfutils-devel; +Cc: nafi, maennich, Paul Pluzhnikov

When map_address is NULL, computing map_address+offset is technically
undefined behavior, and triggers Clang/LLVM warning when using
-fsanitize=pointer-overflow.

Fix this by using uintptr_t to perform computations.

Signed-off-by: Shahriar "Nafi" Rouf <nafi@google.com>
---
 libelf/elf_begin.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c
index fe8c640a..da495bef 100644
--- a/libelf/elf_begin.c
+++ b/libelf/elf_begin.c
@@ -445,15 +445,15 @@ file_read_elf (int fildes, void *map_address, unsigned char *e_ident,
     {
       /* This pointer might not be directly usable if the alignment is
 	 not sufficient for the architecture.  */
-      Elf64_Ehdr *ehdr = (Elf64_Ehdr *) ((char *) map_address + offset);
+      uintptr_t ehdr = (uintptr_t) map_address + offset;
 
       /* This is a 64-bit binary.  */
       if (map_address != NULL && e_ident[EI_DATA] == MY_ELFDATA
 	  && (ALLOW_UNALIGNED
-	      || (((uintptr_t) ehdr) & (__alignof__ (Elf64_Ehdr) - 1)) == 0))
+	      || (ehdr & (__alignof__ (Elf64_Ehdr) - 1)) == 0))
 	{
 	  /* We can use the mmapped memory.  */
-	  elf->state.elf64.ehdr = ehdr;
+	  elf->state.elf64.ehdr = (Elf64_Ehdr *) ehdr;
 	}
       else
 	{
@@ -486,7 +486,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) ehdr + e_shoff)
+	      || (((ehdr + e_shoff)
 		   & (__alignof__ (Elf64_Shdr) - 1)) == 0)))
 	{
 	  if (unlikely (scncnt > 0 && e_shoff >= maxsize)
@@ -496,7 +496,7 @@ file_read_elf (int fildes, void *map_address, unsigned char *e_ident,
 
 	  if (scncnt > 0)
 	    elf->state.elf64.shdr
-	      = (Elf64_Shdr *) ((char *) ehdr + e_shoff);
+	      = (Elf64_Shdr *) (ehdr + e_shoff);
 
 	  for (size_t cnt = 0; cnt < scncnt; ++cnt)
 	    {
-- 
2.42.0.869.gea05f2083d-goog


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-11-14 20:52 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-13 22:58 [PATCH] Fix computations with (potentially) NULL pointer Paul Pluzhnikov
2023-11-14 12:57 ` Mark Wielaard
2023-11-14 16:12   ` Paul Pluzhnikov
2023-11-14 16:57     ` Mark Wielaard
2023-11-14 17:04       ` Paul Pluzhnikov
2023-11-14 17:55         ` Mark Wielaard
2023-11-14 18:56           ` Paul Pluzhnikov
2023-11-14 20:52             ` Mark Wielaard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).