mirror of https://github.com/rusefi/lua.git
'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:
parent
b5bc898467
commit
6d763a2500
|
@ -1011,8 +1011,13 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||||
free(ptr);
|
free(ptr);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else
|
else { /* cannot fail when shrinking a block */
|
||||||
return realloc(ptr, nsize);
|
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 */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue