mirror of
https://git.tukaani.org/xz.git
synced 2024-04-04 12:36:23 +02:00
xz: Improve handling of failed realloc in xrealloc.
Thanks to Jim Meyering.
This commit is contained in:
parent
ab22562066
commit
3d93b63549
1 changed files with 12 additions and 2 deletions
|
@ -26,9 +26,19 @@ xrealloc(void *ptr, size_t size)
|
||||||
{
|
{
|
||||||
assert(size > 0);
|
assert(size > 0);
|
||||||
|
|
||||||
|
// Save ptr so that we can free it if realloc fails.
|
||||||
|
// The point is that message_fatal ends up calling stdio functions
|
||||||
|
// which in some libc implementations might allocate memory from
|
||||||
|
// the heap. Freeing ptr improves the chances that there's free
|
||||||
|
// memory for stdio functions if they need it.
|
||||||
|
void *p = ptr;
|
||||||
ptr = realloc(ptr, size);
|
ptr = realloc(ptr, size);
|
||||||
if (ptr == NULL)
|
|
||||||
message_fatal("%s", strerror(errno));
|
if (ptr == NULL) {
|
||||||
|
const int saved_errno = errno;
|
||||||
|
free(p);
|
||||||
|
message_fatal("%s", strerror(saved_errno));
|
||||||
|
}
|
||||||
|
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue