Skip to content

Commit 67a6b1a

Browse files
peffgitster
authored andcommitted
unpack_loose_header(): avoid numeric comparison of zlib status
When unpacking a loose header, we try to inflate the first 32 bytes. We'd expect either Z_OK (we filled up the output buffer, but there are more bytes in the object) or Z_STREAM_END (this is a tiny object whose header and content fit in the buffer). We check for that with "if (status < Z_OK)", making the assumption that all of the errors we'd see have negative values (as Z_OK itself is "0", and Z_STREAM_END is "1"). But there's at least one case this misses: Z_NEED_DICT is "2". This isn't something we'd ever expect to see, but if we do see it, we should consider it an error (since we have no dictionary to load). Instead, the current code interprets Z_NEED_DICT as success and looks for the object header's terminating NUL in the bytes we've read. This will generaly be zero bytes if the dictionary is mentioned at the start of the stream. So we'll fail to find it and complain "the header is too long" (ULHR_LONG). But really, the problem is that the object is malformed, and we should return ULHR_BAD. This is a minor bug, as we consider both cases to be an error. But it does mean we print the wrong error message. The test case added in the previous patch triggers this code, so we can just confirm the error message we see here. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0b1493c commit 67a6b1a

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

object-file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
12731273
obj_read_unlock();
12741274
status = git_inflate(stream, 0);
12751275
obj_read_lock();
1276-
if (status < Z_OK)
1276+
if (status != Z_OK && status != Z_STREAM_END)
12771277
return ULHR_BAD;
12781278

12791279
/*

t/t1006-cat-file.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,8 @@ test_expect_success 'object reading handles zlib dictionary' - <<\EOT
865865
printf '\170\273\017\112\003\143' >$objpath &&
866866
867867
test_must_fail git cat-file blob $blob 2>err &&
868+
test_grep ! 'too long' err &&
869+
test_grep 'error: unable to unpack' err &&
868870
test_grep 'error: inflate: needs dictionary' err
869871
EOT
870872

0 commit comments

Comments
 (0)