X-Git-Url: https://git.deb.at/w?a=blobdiff_plain;f=xmalloc.c;h=d291f4dd76149b86679cb29e51691ca0fb72cbc6;hb=9fdb9852aeafd99361725c0432ad69110f24a1be;hp=086f3fbb69ed9b5fe351a9078af9e75d29abf80a;hpb=7a7531e544d236d58ca077ce56b7d6d0c60faa09;p=pkg%2Fabook.git diff --git a/xmalloc.c b/xmalloc.c index 086f3fb..d291f4d 100644 --- a/xmalloc.c +++ b/xmalloc.c @@ -36,6 +36,7 @@ #include #include #include +#include "xmalloc.h" static void xmalloc_default_error_handler(int err) @@ -78,22 +79,6 @@ xmalloc0(size_t size) return p; } -void * -xrealloc(void *ptr, size_t size) -{ - if((ptr = realloc(ptr, size)) == NULL) - (*xmalloc_handle_error)(errno); - - return ptr; -} - -void -xfree(void *ptr) -{ - free(ptr); - ptr = NULL; -} - static void * _xmalloc_inc(size_t size, size_t inc, int zero) { @@ -122,3 +107,31 @@ xmalloc0_inc(size_t size, size_t inc) return _xmalloc_inc(size, inc, 1); } +void * +xrealloc(void *ptr, size_t size) +{ + if((ptr = realloc(ptr, size)) == NULL) + (*xmalloc_handle_error)(errno); + + return ptr; +} + +void * +xrealloc_inc(void *ptr, size_t size, size_t inc) +{ + size_t total_size = size + inc; + + /* + * check if the calculation overflowed + */ + if(total_size < size) { + (*xmalloc_handle_error)(EINVAL); + return NULL; + } + + if((ptr = realloc(ptr, total_size)) == NULL) + (*xmalloc_handle_error)(errno); + + return ptr; +} +