1 From 3d93b6354927247a1569caf22ad27b07e97ee904 Mon Sep 17 00:00:00 2001
2 From: Lasse Collin <lasse.collin@tukaani.org>
3 Date: Fri, 28 Sep 2012 20:11:09 +0300
4 Subject: [PATCH] xz: Improve handling of failed realloc in xrealloc.
6 Thanks to Jim Meyering.
8 src/xz/util.c | 14 ++++++++++++--
9 1 files changed, 12 insertions(+), 2 deletions(-)
11 diff --git a/src/xz/util.c b/src/xz/util.c
12 index 987b443..35850f4 100644
15 @@ -26,9 +26,19 @@ xrealloc(void *ptr, size_t size)
19 + // Save ptr so that we can free it if realloc fails.
20 + // The point is that message_fatal ends up calling stdio functions
21 + // which in some libc implementations might allocate memory from
22 + // the heap. Freeing ptr improves the chances that there's free
23 + // memory for stdio functions if they need it.
25 ptr = realloc(ptr, size);
27 - message_fatal("%s", strerror(errno));
30 + const int saved_errno = errno;
32 + message_fatal("%s", strerror(saved_errno));