Skip to content

Commit 9929a67

Browse files
peffgitster
authored andcommitted
unpack_loose_rest(): avoid numeric comparison of zlib status
When unpacking the actual content of a loose object file, we insist both that the status code we got is Z_STREAM_END, and that we consumed all bytes. If we didn't, we'll return an error, but the specific error message we produce depends on which of the two error conditions we saw. So we'll check both a second time to decide which error to produce. But this second time, our status code check is loose: it checks for a negative status value. This can get confused by zlib codes which are not negative, such as Z_NEED_DICT. In this case we'd erroneously print nothing at all, when we should say "corrupt loose object". Instead, this second check should check explicitly against Z_STREAM_END. Note that Z_OK is "0", so the existing code also produced no message for Z_OK. But it's impossible to see that status, since we only break out of the inflate loop when we stop seeing Z_OK (so a stream which has more bytes than its object header claims would eventually yield Z_BUF_ERROR). There's no test here, as it would require a loose object whose zlib stream returns Z_NEED_DICT in the middle of the object content. I think that is probably possible, but even our Z_NEED_DICT test in t1006 does not trigger this, because we hit that error while reading the header. I found this bug while reviewing all callers of git_inflate() for bugs similar to the one we saw in unpack_loose_header(). This was the only other case that did a numeric comparison rather than explicitly checking for Z_STREAM_END. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 67a6b1a commit 9929a67

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

object-file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,7 @@ static void *unpack_loose_rest(git_zstream *stream,
13521352
return buf;
13531353
}
13541354

1355-
if (status < 0)
1355+
if (status != Z_STREAM_END)
13561356
error(_("corrupt loose object '%s'"), oid_to_hex(oid));
13571357
else if (stream->avail_in)
13581358
error(_("garbage at end of loose object '%s'"),

0 commit comments

Comments
 (0)