Repository navigation
Conversation
There was a problem hiding this comment.
Review Summary
This PR correctly reverts the new_append_with_config seek-to-dir_start that was introduced in #1302. The fix is minimal and well-targeted: removing the three lines that caused ZipWriter to position readwriter at the beginning of the central directory, which then caused finalize() to overwrite the CD magic with 00 00 00 00 when the new footer was shorter than the old one, breaking sequential streaming readers.
Core change (src/write.rs)
The removal of the post-metadata seek restores the pre-#1302 invariant: after get_metadata returns, the reader is positioned at dir_start + dir_size (end of the central directory), which is exactly where new content should begin writing. This leaves the existing local file headers and data untouched.
Test coverage (tests/end_to_end.rs)
Both new regression tests are well-constructed and directly exercise the two failure modes described in the PR — shorter-comment append and abort-file-after-append — using read_zipfile_from_stream to validate sequential readability.
No blocking defects found. The fix is correct and the regression tests are appropriate.
You can now have the agent implement changes and create commits directly on your pull request's source branch. Simply comment with /q followed by your request in natural language to ask the agent to make changes.
Seeking back to metadata.shared.dir_start in ZipWriter::new_append_with_config causes ZipWriter::finalize to overwrite dir_start with 0_u32 and relocate the central directory to stream_end whenever the rewritten footer is shorter than the existing archive tail (e.g. after shortening the archive comment or aborting a newly written entry), because generic W: Write + Seek cannot truncate the stream. This leaves 00 00 00 00 immediately after the last local file entry instead of a PK\x01\x02 central directory header, causing read_zipfile_from_stream to fail with InvalidArchive(Invalid local file header). Restore the pre-zip-rs#1302 stream position in new_append_with_config while keeping the AddressHoleCursor fix in tests/append_near_4gb.rs and adding streaming regression tests.
8508d8c to
ec90d2b
Compare
There was a problem hiding this comment.
Please restore this test. We need it to ensure that #1299 remains fixed.
|
@ByGh00st This (at least as you're describing it in the PR description) isn't actually a bug in write.rs, it's a bug in the fuzz_write test that it considers the archive streamable when this has happened, even though fuzz/write/src/main.rs lines 751-763 are supposed to detect that and clear the streamable flag. We don't reintroduce bugs in production code to work around bugs in test code. |
|
@Pr0methean I downloaded and replayed the 3 crash inputs from run All 3 write a single entry with a On the initial write, Minimal reproducer (passes on #[test]
fn new_append_extended_timestamp_shrink_breaks_stream() {
use std::io::{Cursor, Seek, SeekFrom, Write};
use zip::read::read_zipfile_from_stream;
use zip::write::FullFileOptions;
use zip::{CompressionMethod, ZipArchive, ZipWriter};
let mut opts = FullFileOptions::default()
.compression_method(CompressionMethod::Stored)
.validate_extra_fields(true);
opts.add_extra_field(0x5455, [47u8, 117, 255, 255], true).unwrap();
let mut writer = ZipWriter::new(Cursor::new(Vec::new()));
writer.start_file("a.txt", opts).unwrap();
writer.write_all(b"hello").unwrap();
let initial = writer.finish().unwrap().into_inner(); // len = 121, dir_start = 40
let writer = ZipWriter::new_append(Cursor::new(initial)).unwrap();
let mut after = writer.finish().unwrap(); // after.get_ref()[40..44] == [00, 00, 00, 50]
assert_eq!(ZipArchive::new(Cursor::new(after.get_ref())).unwrap().len(), 1);
after.seek(SeekFrom::Start(0)).unwrap();
while read_zipfile_from_stream(&mut after).unwrap().is_some() {}
}Since |
|
@ByGh00st Treat them as non-streamable. |
Summary
Restores the pre-#1302 stream position in
ZipWriter::new_append_with_configwhile keeping theAddressHoleCursortest fix intests/append_near_4gb.rsand adding streaming regression tests.Unblocks v9.6.0 (#1297). Follow-up: reopen #1299 for a
Truncate-based solution in v9.7+. /cc @Pr0methean @ychampionRoot Cause
ZipWriter<W>operates on genericW: Write + Seek, which cannot truncate the underlying stream when the rewritten central directory + EOCD is shorter than the existing archive tail (footer_end < stream_end).ZipArchive::get_metadataleftreadwriterpositioned at the end of the existing central directory (dir_start + dir_size), leaving the originalPK\x01\x02central directory header signature intact atdir_startimmediately after the last local file entry.metadata.shared.dir_start, if a subsequent operation produces a shorter tail (for example, shortening the archive comment or callingabort_file()after writing data past the new footer end),ZipWriter::finalize()(src/write.rs:1673-1706) enters theelse if footer_end < stream_endbranch:dir_startwith00 00 00 00and relocates the central directory and EOCD to the end of the stream.dir_start(immediately following the last local file entry) now starts with00 00 00 00instead ofPK\x01\x02, sequential streaming readers (read_zipfile_from_stream) fail after the last entry withErr(ZipError::InvalidArchive("Invalid local file header"))(caught byfuzz_writein CI run 43566959380).Supporting in-place central directory overwriting when the archive shrinks will require a truncation mechanism.
Test Plan
new_append_shorter_comment_preserves_stream_readabilityandnew_append_abort_file_preserves_stream_readabilityintests/end_to_end.rs(both fail on2b9a0e5withInvalidArchive("Invalid local file header")and pass with this fix).cargo fmt --checkcargo clippy --all-targets --all-features -- -D warningscargo test