aboutsummaryrefslogtreecommitdiff
path: root/xmalloc.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2025-02-03 07:31:31 +0000
committerDaniel Eklöf <daniel@ekloef.se>2025-02-05 12:35:17 +0000
commitf718cb3fb0d627cab8d952b94891e38238934090 (patch)
treeda25ca9fc5c946a76d992755cb2772f00061b222 /xmalloc.c
parenturl-mode: regex: make sure there's always room for the NULL terminator (diff)
xmalloc: calling xrealloc() or xreallocarray() with a 0-size is UB in C23
And likely to in future POSIX too.
Diffstat (limited to 'xmalloc.c')
-rw-r--r--xmalloc.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/xmalloc.c b/xmalloc.c
index 0a67cdb2..ccfb5c48 100644
--- a/xmalloc.c
+++ b/xmalloc.c
@@ -32,15 +32,17 @@ xcalloc(size_t nmemb, size_t size)
void *
xrealloc(void *ptr, size_t size)
{
+ xassert(size != 0);
void *alloc = realloc(ptr, size);
- return unlikely(size == 0) ? alloc : check_alloc(alloc);
+ return check_alloc(alloc);
}
void *
xreallocarray(void *ptr, size_t n, size_t size)
{
+ xassert(n != 0 && size != 0);
void *alloc = reallocarray(ptr, n, size);
- return unlikely(size == 0) ? alloc : check_alloc(alloc);
+ return check_alloc(alloc);
}
char *