[PATCH v2] malloc: Add asserts for malloc assumptions
Wilco Dijkstra
Wilco.Dijkstra@arm.com
Mon Mar 2 17:23:12 GMT 2026
Currently malloc has various assumptions, some documented, some implicit.
Add a few asserts to check the most fundamental assumptions using verify().
Remove some odd #define void.
---
diff --git a/malloc/malloc.c b/malloc/malloc.c
index d2776bc50e979059eb04c72790132b97698ac51b..fab6ce7a4e20c70f8cd4288d95c2d06718c969d1 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -198,14 +198,6 @@
There are several other #defined constants and macros that you
probably don't want to touch unless you are extending or adapting malloc. */
-/*
- void* is the pointer type that malloc should say it returns
-*/
-
-#ifndef void
-#define void void
-#endif /*void*/
-
#include <stddef.h> /* for size_t */
#include <stdlib.h> /* for getenv(), abort() */
#include <unistd.h> /* for __libc_enable_secure */
@@ -259,6 +251,11 @@
#include <sys/random.h>
#include <not-cancel.h>
+verify (sizeof (unsigned long) == sizeof (size_t));
+verify (sizeof (void *) == sizeof (size_t));
+verify (sizeof (void *) == 4 || sizeof (void *) == 8);
+verify (PTRDIFF_MAX <= SIZE_MAX / 2);
+
/*
Debugging:
@@ -1263,9 +1260,6 @@ nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
static __always_inline size_t
checked_request2size (size_t req) __nonnull (1)
{
- _Static_assert (PTRDIFF_MAX <= SIZE_MAX / 2,
- "PTRDIFF_MAX is not more than half of SIZE_MAX");
-
if (__glibc_unlikely (req > PTRDIFF_MAX))
return SIZE_MAX;
More information about the Libc-alpha
mailing list