From 8c8149d146f744b2c0c64b80ba9220bd464de013 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Wed, 23 Dec 2020 21:52:40 +0100 Subject: XUtils: check for multiplication overflow in allocation size --- XUtils.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'XUtils.c') diff --git a/XUtils.c b/XUtils.c index cd5edb91..01f33424 100644 --- a/XUtils.c +++ b/XUtils.c @@ -13,6 +13,7 @@ in the source distribution for its full text. #include #include #include +#include #include #include #include @@ -36,9 +37,21 @@ void* xMalloc(size_t size) { return data; } +void* xMallocArray(size_t nmemb, size_t size) { + assert(nmemb > 0); + assert(size > 0); + if (SIZE_MAX / nmemb < size) { + fail(); + } + return xMalloc(nmemb * size); +} + void* xCalloc(size_t nmemb, size_t size) { assert(nmemb > 0); assert(size > 0); + if (SIZE_MAX / nmemb < size) { + fail(); + } void* data = calloc(nmemb, size); if (!data) { fail(); @@ -56,6 +69,15 @@ void* xRealloc(void* ptr, size_t size) { return data; } +void* xReallocArray(void* ptr, size_t nmemb, size_t size) { + assert(nmemb > 0); + assert(size > 0); + if (SIZE_MAX / nmemb < size) { + fail(); + } + return xRealloc(ptr, nmemb * size); +} + char* String_cat(const char* s1, const char* s2) { const size_t l1 = strlen(s1); const size_t l2 = strlen(s2); -- cgit v1.2.3