[PATCH] Hide -1 alignment passed to memalign from compiler

H.J. Lu hjl.tools@gmail.com
Mon Dec 16 21:55:49 GMT 2024


Since -1 isn't a power of two, compiler may reject it.  Hide -1 from
Clang 19 which issues an error:

tst-memalign.c:86:31: error: requested alignment is not a power of 2 [-Werror,-Wnon-power-of-two-alignment]
   86 |   p = memalign (((size_t) -1) - 1, pagesize);
      |                 ~~~~~~~~~~~~~~^~~
tst-memalign.c:86:31: error: requested alignment must be 4294967296 bytes or smaller; maximum alignment assumed [-Werror,-Wbuiltin-assume-aligned-alignment]
   86 |   p = memalign (((size_t) -1) - 1, pagesize);

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 malloc/tst-memalign.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/malloc/tst-memalign.c b/malloc/tst-memalign.c
index 563f6413d2..b96d416356 100644
--- a/malloc/tst-memalign.c
+++ b/malloc/tst-memalign.c
@@ -25,6 +25,9 @@
 
 static int errors = 0;
 
+/* NB: Hide -1 alignment from compiler since -1 isn't a power of two.  */
+size_t minus_one = -1;
+
 static void
 merror (const char *msg)
 {
@@ -83,7 +86,7 @@ do_test (void)
   errno = 0;
 
   /* Test to expose integer overflow in malloc internals from BZ #16038.  */
-  p = memalign (-1, pagesize);
+  p = memalign (minus_one, pagesize);
 
   save = errno;
 
-- 
2.47.1



More information about the Libc-alpha mailing list