xmalloc: calling xrealloc() or xreallocarray() with a 0-size is UB in C23

And likely to in future POSIX too.
This commit is contained in:
Daniel Eklöf 2025-02-03 08:31:31 +01:00
parent ab4426f987
commit f718cb3fb0
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F

View file

@ -32,15 +32,17 @@ xcalloc(size_t nmemb, size_t size)
void * void *
xrealloc(void *ptr, size_t size) xrealloc(void *ptr, size_t size)
{ {
xassert(size != 0);
void *alloc = realloc(ptr, size); void *alloc = realloc(ptr, size);
return unlikely(size == 0) ? alloc : check_alloc(alloc); return check_alloc(alloc);
} }
void * void *
xreallocarray(void *ptr, size_t n, size_t size) xreallocarray(void *ptr, size_t n, size_t size)
{ {
xassert(n != 0 && size != 0);
void *alloc = reallocarray(ptr, n, size); void *alloc = reallocarray(ptr, n, size);
return unlikely(size == 0) ? alloc : check_alloc(alloc); return check_alloc(alloc);
} }
char * char *