'realloc' can fail when shrinking a block

According to ISO C, 'realloc' can fail when shrinking a block. If that
happens, 'l_alloc' simply ignores the fail and returns the original
block.
This commit is contained in:
Roberto Ierusalimschy 2020-08-12 11:13:47 -03:00
parent b5bc898467
commit 6d763a2500
1 changed files with 7 additions and 2 deletions

View File

@ -1011,8 +1011,13 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
free(ptr);
return NULL;
}
else
return realloc(ptr, nsize);
else { /* cannot fail when shrinking a block */
void *newptr = realloc(ptr, nsize);
if (newptr == NULL && ptr != NULL && nsize <= osize)
return ptr; /* keep the original block */
else /* no fail or not shrinking */
return newptr; /* use the new block */
}
}