Merge branch 'jt/archive-zip-deflate-fix'

The deflate codepath in "git archive --format=zip" had a
longstanding bug coming from misuse of zlib API, which has been
corrected.

* jt/archive-zip-deflate-fix:
  archive: flush deflate stream until Z_STREAM_END
This commit is contained in:
Junio C Hamano
2025-08-07 08:14:38 -07:00

View File

@@ -492,14 +492,22 @@ static int write_zip_entry(struct archiver_args *args,
zstream.next_in = buf;
zstream.avail_in = 0;
result = git_deflate(&zstream, Z_FINISH);
if (result != Z_STREAM_END)
die("deflate error (%d)", result);
do {
result = git_deflate(&zstream, Z_FINISH);
if (result != Z_OK && result != Z_STREAM_END)
die("deflate error (%d)", result);
out_len = zstream.next_out - compressed;
if (out_len > 0) {
write_or_die(1, compressed, out_len);
compressed_size += out_len;
zstream.next_out = compressed;
zstream.avail_out = sizeof(compressed);
}
} while (result != Z_STREAM_END);
git_deflate_end(&zstream);
out_len = zstream.next_out - compressed;
write_or_die(1, compressed, out_len);
compressed_size += out_len;
zip_offset += compressed_size;
write_zip_data_desc(size, compressed_size, crc);