Fix loose object uncompression check.
The thing is, if the output buffer is empty, we should *still* actually use the zlib routines to *unpack* that empty output buffer. But we had a test that said "only unpack if we still expect more output". So we wouldn't use up all the zlib stream, because we felt that we didn't need it, because we already had all the bytes we wanted. And it was "true": we did have all the output data. We just needed to also eat all the input data! We've had this bug before - thinking that we don't need to inflate() anything because we already had it all.. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
Mar 19, 2007 at 22:49 UTC
456cdf6edbf0c14b75fd9048708e9abea43e6aa1
1 file changed
+15
-2
sha1_file.c
+15
-2
@@ -1030,14 +1030,27 @@ static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size
1030
n = size;
1031
memcpy(buf, (char *) buffer + bytes, n);
1032
bytes = n;
1033
- if (bytes < size) {
1033
+ if (bytes <= size) {
1034
+ /*
1035
+ * The above condition must be (bytes <= size), not
1036
+ * (bytes < size). In other words, even though we
1037
+ * expect no more output and set avail_out to zer0,
1038
+ * the input zlib stream may have bytes that express
1039
+ * "this concludes the stream", and we *do* want to
1040
+ * eat that input.
1041
+ *
1042
+ * Otherwise we would not be able to test that we
1043
+ * consumed all the input to reach the expected size;
1044
+ * we also want to check that zlib tells us that all
1045
+ * went well with status == Z_STREAM_END at the end.
1046
+ */
1047
stream->next_out = buf + bytes;
1048
stream->avail_out = size - bytes;
1049
while (status == Z_OK)
1050
status = inflate(stream, Z_FINISH);
1051
}
1052
buf[size] = 0;
1040
- if ((status == Z_OK || status == Z_STREAM_END) && !stream->avail_in) {
1053
+ if (status == Z_STREAM_END && !stream->avail_in) {
1054
inflateEnd(stream);
1055
return buf;
1056
}