2021-04-18 Paul Eggert <eggert@cs.ucla.edu>
+ safe-alloc: simplify via reallocarray
+ * lib/safe-alloc.c: Do not include xalloc-oversized.h.
+ (safe_alloc_alloc_n, safe_alloc_realloc_n):
+ Use reallocarray to check for size or ptrdiff_t overflow.
+ * modules/reallocarray (License): Switch from LGPL to LGPLv2+, as
+ this is needed for safe-alloc and anyway is more appropriate for
+ this library function common with BSD.
+ * modules/safe-alloc (Depends-on): Depend on reallocarray
+ rather than xalloc-oversized.
+
xalloc-oversized: fix SIZE_MAX optimization bug
* lib/xalloc-oversized.h (xalloc_count_t): Remove; no longer
needed and was evidently error-prone anyway.
/* Specification. */
#include "safe-alloc.h"
-#include "xalloc-oversized.h"
-
#include <stdlib.h>
#include <stddef.h>
#include <errno.h>
return 0;
}
- if (xalloc_oversized (count, size))
- {
- errno = ENOMEM;
- return -1;
- }
-
if (zeroed)
*(void **) ptrptr = calloc (count, size);
else
- *(void **) ptrptr = malloc (count * size);
+ *(void **) ptrptr = reallocarray (NULL, count, size);
if (*(void **) ptrptr == NULL)
return -1;
*(void **) ptrptr = NULL;
return 0;
}
- if (xalloc_oversized (count, size))
- {
- errno = ENOMEM;
- return -1;
- }
- tmp = realloc (*(void **) ptrptr, size * count);
+ tmp = reallocarray (*(void **) ptrptr, size, count);
if (!tmp)
return -1;
*(void **) ptrptr = tmp;