while( ! ISSPACE(*ptr) )
ptr++;
- /* includes also the trailing zero */
- alias_len = (size_t)(ptr - tmp + 1);
+ alias_len = (size_t)(ptr - tmp);
- if( (*alias = xmalloc(alias_len)) == NULL) {
- free(line);
- return 1;
- }
+ *alias = xmalloc_inc(alias_len, 1);
- strncpy(*alias, tmp, alias_len - 1);
- *(*alias + alias_len - 1) = 0;
+ strncpy(*alias, tmp, alias_len);
+ *(*alias + alias_len) = 0;
while(ISSPACE(*ptr))
ptr++;
else
size *= 2;
- buffer = xrealloc (buffer, size);
+ buffer = xrealloc(buffer, size);
}
}
break; /* the whole line has been read */
for (inc = size, p = NULL; inc > mininc; inc /= 2)
- if ((p = xrealloc(buf, size + inc)) !=
+ if ((p = xrealloc_inc(buf, size, inc)) !=
NULL)
break;
buf[--len] = '\0';
if (size - len > mucho) { /* a plenitude of unused memory? */
- p = xrealloc(buf, len+1);
+ p = xrealloc_inc(buf, len, 1);
if (p != NULL) {
buf = p;
size = len+1;
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;
+}
+
+void
+xfree(void *ptr)
+{
+ free(ptr);
+ ptr = NULL;
+}
+
void * xmalloc0(size_t);
void * xmalloc_inc(size_t, size_t);
void * xrealloc(void *, size_t);
+void * xrealloc_inc(void *, size_t, size_t);
void xfree(void *);
#endif