diff options
-rw-r--r-- | include/utils.h | 1 | ||||
-rw-r--r-- | src/utils.c | 11 |
2 files changed, 12 insertions, 0 deletions
diff --git a/include/utils.h b/include/utils.h index bb58ba42..3199388a 100644 --- a/include/utils.h +++ b/include/utils.h @@ -138,6 +138,7 @@ extern void __memory_allocation_error(const char *filename, uint32_t line) __nor extern void xfree(const void *ptr); extern void *xmalloc(size_t size); +extern void *xmalloc_array(size_t nmemb, size_t size); extern void *xrealloc(void *ptr, size_t size); extern void *xzalloc(size_t size); extern char *xstrdup(const char *s); diff --git a/src/utils.c b/src/utils.c index 65dabf41..47f5b791 100644 --- a/src/utils.c +++ b/src/utils.c @@ -39,6 +39,17 @@ void *xmalloc(size_t size) return ptr; } +void *xmalloc_array(size_t nmemb, size_t size) +{ + assert(size != 0); + assert(nmemb != 0); + + if (nmemb > SIZE_MAX / size) + memory_allocation_error(); + + return xmalloc(nmemb * size); +} + void *xrealloc(void *ptr, size_t size) { ptr = realloc(ptr, size); |