Hi Bruno,

Bruno Haible <br...@clisp.org> writes:

>> However, I think that the correct way to write this would be to use
>> 'uintptr_t' which would work with large page sizes on more niche
>> architectures [1].
>
> Yes, the way to get rid of the warning is to cast from pointer to
> 'uintptr_t', not 'unsigned long'.
>
>> Can you check the attached patch before I push?
>
> The stdint.h part is right. What is not right, is to do a conversion
> from pointer to uintptr_t and then back from uintptr_t to pointer.
> That is inefficient on the CHERI architecture, IIRC.
> It's better to keep the addition on the 'char *' side.

Thanks for checking. I pushed the attached patch with those changes.

I assumed uintptr_t was correct for performing arithmetic on any
pointer. I know CHERI has some limitations, but I am not familiar with
them. Perhaps I will get around to reading the manual at some point [1].

Collin

[1] https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-947.pdf

>From a5c7974191230a73c67926cb38d8539d9252f942 Mon Sep 17 00:00:00 2001
Message-ID: <a5c7974191230a73c67926cb38d8539d9252f942.1751821732.git.collin.fu...@gmail.com>
From: Collin Funk <collin.fu...@gmail.com>
Date: Sun, 6 Jul 2025 10:07:35 -0700
Subject: [PATCH] pagealign_alloc: Don't assume pointers fit in 'unsigned
 long'.

* modules/pagealign_alloc (Depends-on): Add stdint-h.
* lib/pagealign_alloc.c: Include stdint.h.
(pagealign_alloc): Cast pointers to 'uintptr_t' instead of 'unsigned
long'.
---
 ChangeLog               | 8 ++++++++
 lib/pagealign_alloc.c   | 3 ++-
 modules/pagealign_alloc | 1 +
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index c8d09b3dac..286d589d23 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2025-07-06  Collin Funk  <collin.fu...@gmail.com>
+
+	pagealign_alloc: Don't assume pointers fit in 'unsigned long'.
+	* modules/pagealign_alloc (Depends-on): Add stdint-h.
+	* lib/pagealign_alloc.c: Include stdint.h.
+	(pagealign_alloc): Cast pointers to 'uintptr_t' instead of 'unsigned
+	long'.
+
 2025-07-05  Collin Funk  <collin.fu...@gmail.com>
 
 	strptime: Convert K&R definitions to ANSI C.
diff --git a/lib/pagealign_alloc.c b/lib/pagealign_alloc.c
index 3f3fc17169..e460356c3f 100644
--- a/lib/pagealign_alloc.c
+++ b/lib/pagealign_alloc.c
@@ -26,6 +26,7 @@
 
 #include <fcntl.h>
 #include <unistd.h>
+#include <stdint.h>
 
 #if HAVE_MMAP
 # include <sys/mman.h>
@@ -159,7 +160,7 @@ pagealign_alloc (size_t size)
       return NULL;
     }
   ret = (char *) unaligned_ptr
-        + ((- (unsigned long) unaligned_ptr) & (pagesize - 1));
+        + ((- (uintptr_t) unaligned_ptr) & (pagesize - 1));
   new_memnode (ret, unaligned_ptr);
 #endif /* HAVE_MMAP && HAVE_POSIX_MEMALIGN */
   return ret;
diff --git a/modules/pagealign_alloc b/modules/pagealign_alloc
index 59afe866de..bd2d8f4cbc 100644
--- a/modules/pagealign_alloc
+++ b/modules/pagealign_alloc
@@ -17,6 +17,7 @@ open
 stdlib-h
 xalloc
 unistd-h
+stdint-h
 
 configure.ac:
 gl_PAGEALIGN_ALLOC
-- 
2.50.0

Reply via email to