static int adjust_size(size_t *n)
{
/* Result of pointer difference must fit in ptrdiff_t. */
- if (*n > PTRDIFF_MAX - SIZE_ALIGN - PAGE_SIZE) {
- errno = ENOMEM;
- return -1;
+ if (*n-1 > PTRDIFF_MAX - SIZE_ALIGN - PAGE_SIZE) {
+ if (*n) {
+ errno = ENOMEM;
+ return -1;
+ } else {
+ *n = SIZE_ALIGN;
+ return 0;
+ }
}
*n = (*n + OVERHEAD + SIZE_ALIGN - 1) & SIZE_MASK;
return 0;
struct chunk *c;
int i, j;
- if (!n || adjust_size(&n) < 0) return 0;
+ if (adjust_size(&n) < 0) return 0;
if (n > MMAP_THRESHOLD) {
size_t len = n + PAGE_SIZE - 1 & -PAGE_SIZE;
void *new;
if (!p) return malloc(n);
- else if (!n) return free(p), (void *)0;
if (adjust_size(&n) < 0) return 0;