Skip to content

Commit 03e7c45

Browse files
peffgitster
authored andcommitted
unpack_loose_header(): simplify next_out assignment
When using OBJECT_INFO_ALLOW_UNKNOWN_TYPE to unpack a header that doesn't fit into our initial 32-byte buffer, we loop over calls git_inflate(), feeding it our buffer to the "next_out" pointer each time. As the code is written, we reset next_out after each inflate call (and after reading the output), ready for the next loop. This isn't wrong, but there are a few advantages to setting up "next_out" right before each inflate call, rather than after: 1. It drops a few duplicated lines of code. 2. It makes it obvious that we always feed a fresh buffer on each call (and thus can never see Z_BUF_ERROR due to due to a lack of output space). 3. After we exit the loop, we'll leave stream->next_out pointing to the end of the fetched data (this is how zlib callers find out how much data is in the buffer). This doesn't matter in practice, since nobody looks at it again. But it's probably the least-surprising thing to do, as it matches how next_out is left when the whole thing fits in the initial 32-byte buffer (and we don't enter the loop at all). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8216cf9 commit 03e7c45

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

object-file.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,18 +1296,17 @@ enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
12961296
* reading the stream.
12971297
*/
12981298
strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
1299-
stream->next_out = buffer;
1300-
stream->avail_out = bufsiz;
13011299

13021300
do {
1301+
stream->next_out = buffer;
1302+
stream->avail_out = bufsiz;
1303+
13031304
obj_read_unlock();
13041305
status = git_inflate(stream, 0);
13051306
obj_read_lock();
13061307
strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
13071308
if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
13081309
return 0;
1309-
stream->next_out = buffer;
1310-
stream->avail_out = bufsiz;
13111310
} while (status != Z_STREAM_END);
13121311
return ULHR_TOO_LONG;
13131312
}

0 commit comments

Comments
 (0)