Hi Bruno,

On MinGW you can see the following warning in the CI:

    depbase=`echo pagealign_alloc.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
    x86_64-w64-mingw32-gcc -DHAVE_CONFIG_H -DEXEEXT=\".exe\" -DEXEEXT=\".exe\" 
-DNO_XMALLOC -DEXEEXT=\".exe\" -I. -I..  -DGNULIB_STRICT_CHECKING=1 
-I/mingw64/include  -g -O2 -MT pagealign_alloc.o -MD -MP -MF $depbase.Tpo -c -o 
pagealign_alloc.o pagealign_alloc.c &&\
    mv -f $depbase.Tpo $depbase.Po
    pagealign_alloc.c: In function 'pagealign_alloc':
    pagealign_alloc.c:162:15: warning: cast from pointer to integer of 
different size [-Wpointer-to-int-cast]
      162 |         + ((- (unsigned long) unaligned_ptr) & (pagesize - 1));
          |               ^

At first this seemed like an issue to me since on 64-bit Windows
'unsigned long' is only 32-bits. But on Windows I have only seen page
sizes be 4096 bytes. In that case, or any other where PAGESIZE fits in
'unsigned long', the operation should be fine despite losing the high
bits.

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].

Can you check the attached patch before I push?

Based on the git blame, you committed this line on 2005-03-03. It seems
like it was portable then, so not a regression.

Collin

[1] https://wiki.debian.org/ArchitectureSpecificsMemo#Summary

>From 47f10829e58409e955eef40a681b1f9fd9dd423e Mon Sep 17 00:00:00 2001
Message-ID: <47f10829e58409e955eef40a681b1f9fd9dd423e.1751772648.git.collin.fu...@gmail.com>
From: Collin Funk <collin.fu...@gmail.com>
Date: Sat, 5 Jul 2025 20:09:11 -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               | 6 ++++++
 lib/pagealign_alloc.c   | 5 +++--
 modules/pagealign_alloc | 1 +
 3 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index c8d09b3dac..4ebfa85f8c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2025-07-05  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'.
+
 	strptime: Convert K&R definitions to ANSI C.
 	* lib/strptime.c (LOCALE_PARAM) [_LIBC]: Adjust to match glibc, not
 	relevant for Gnulib.
diff --git a/lib/pagealign_alloc.c b/lib/pagealign_alloc.c
index 3f3fc17169..847548a58e 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>
@@ -158,8 +159,8 @@ pagealign_alloc (size_t size)
       errno = ENOMEM;
       return NULL;
     }
-  ret = (char *) unaligned_ptr
-        + ((- (unsigned long) unaligned_ptr) & (pagesize - 1));
+  ret = (void *) ((uintptr_t) unaligned_ptr
+                  + ((- (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