Subject: [PATCH 2/4] fsck: do not die when not enough memory to examine a pack entry

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Thu, 29 May 2014 19:57:05 +0700

fsck is a tool that error() is more preferred than die(), but many
functions embed die() inside beyond fsck's control.
unpack_compressed_entry()'s using xmallocz is such a function,
triggered from verify_packfile() -> unpack_entry(). Make it use
xmallocz_gentle() instead.

Noticed-by: Dale R. Worley <[email-masked]>
Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 sha1_file.c      | 4 +++-
 t/t1050-large.sh | 5 +++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/sha1_file.c b/sha1_file.c
index 3e9f55f..8ad906a 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1921,7 +1921,9 @@ static void *unpack_compressed_entry(struct packed_git *p,
 	git_zstream stream;
 	unsigned char *buffer, *in;
 
-	buffer = xmallocz(size);
+	buffer = xmallocz_gentle(size);
+	if (!buffer)
+		return NULL;
 	memset(&stream, 0, sizeof(stream));
 	stream.next_out = buffer;
 	stream.avail_out = size + 1;
diff --git a/t/t1050-large.sh b/t/t1050-large.sh
index fd10528..333909b 100755
--- a/t/t1050-large.sh
+++ b/t/t1050-large.sh
@@ -163,4 +163,9 @@ test_expect_success 'zip achiving, deflate' '
 	git archive --format=zip HEAD >/dev/null
 '
 
+test_expect_success 'fsck' '
+	test_must_fail git fsck 2>err &&
+	grep "attempting to allocate .* over limit" err
+'
+
 test_done
-- 
1.9.1.346.ga2b5940