#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include "xmalloc.h"
static void
xmalloc_default_error_handler(int err)
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)
{
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;
+}
+