]> git.deb.at Git - pkg/abook.git/blobdiff - xmalloc.c
- add xrealloc_inc
[pkg/abook.git] / xmalloc.c
index 086f3fbb69ed9b5fe351a9078af9e75d29abf80a..bc22a97b19faa6867ee9f8535a5b0ea52d3e2a3b 100644 (file)
--- a/xmalloc.c
+++ b/xmalloc.c
@@ -78,22 +78,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 +106,38 @@ 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;
+}
+
+void
+xfree(void *ptr)
+{
+       free(ptr);
+       ptr = NULL;
+}
+