diff --git a/src/boot/util.h b/src/boot/util.h index 98e9ae576e..e7a3ba0303 100644 --- a/src/boot/util.h +++ b/src/boot/util.h @@ -46,10 +46,16 @@ static inline void *xmalloc_multiply(size_t n, size_t size) { /* Use malloc attribute as this never returns p like userspace realloc. */ _malloc_ _alloc_(3) _returns_nonnull_ _warn_unused_result_ static inline void *xrealloc(void *p, size_t old_size, size_t new_size) { + assert(p || old_size == 0); + void *t = xmalloc(new_size); - new_size = MIN(old_size, new_size); - if (new_size > 0) - memcpy(t, p, new_size); + + size_t size = MIN(old_size, new_size); + if (size > 0) { + assert(p); + memcpy(t, p, size); + } + free(p); return t; }