Skip to content

fix: Add a OwnedZipFileEntry for #177 - #1270

Merged
Pr0methean merged 3 commits into
masterfrom
possible-fix-177
Nov 6, 2026
Merged

Pr0methean merged 3 commits into
masterfrom
possible-fix-177

Conversation

@Its-Just-Nans

@Its-Just-Nans Its-Just-Nans commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Possible fix for #177 - but add a new OwnedZipFileEntry


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

@amazon-q-developer amazon-q-developer Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

This PR adds OwnedZipFileEntry, an owned counterpart to the lifetime-parameterized ZipFileEntry<'a>, enabling callers to store a ZIP entry's metadata beyond the lifetime of the original reader (resolving #177). The design — a Box<[u8]> for the raw name plus an owned ZipFileData, with as_ref() returning a borrowed ZipFileEntry<'_> — is sound in principle.

Critical Issues

1. From<ZipFile<'a, R>> for ZipFileEntry<'a> drains the reader unconditionally (src/read/zipfile.rs, lines 292–299).
After mem::take(&mut file.data), file.data becomes Cow::Owned(ZipFileData::default()). The Drop impl treats any Cow::Owned data as a signal that the file came from a streaming reader and must drain the remaining bytes. This means every call to the new From conversion will drain the underlying reader on drop, even for seekable archive reads where that is both wasteful and incorrect.

2. Test relies on an implicit assumption about header_start being 0 (tests/stream.rs, lines 132–137).
check_content reopens mimetype.zip from scratch and calls with_reader using metadata recovered from a streaming read. This works only because header_start happens to be 0 for this fixture. The pattern is not general and the failure mode (wrong seek target) is silent in the success path.


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.

Comment thread src/read/zipfile.rs Outdated
Comment on lines +292 to +299
impl<'a, R: Read + ?Sized> From<ZipFile<'a, R>> for ZipFileEntry<'a> {
fn from(mut file: ZipFile<'a, R>) -> Self {
Self {
file_name_raw: core::mem::take(&mut file.file_name_raw),
data: core::mem::take(&mut file.data),
}
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛑 Logic Error: After mem::take(&mut file.data), file.data is left as Cow::Owned(ZipFileData::default()). The Drop impl for ZipFile checks if let Cow::Owned(_) = self.data and, finding it owned, calls take_raw_reader() to drain the underlying reader — even for seekable archive files where draining is unnecessary and incorrect. This means every ZipFile-to-ZipFileEntry conversion via From will unconditionally drain the reader after the conversion completes. Replace mem::take on data with a variant that leaves a Cow::Borrowed sentinel, or use ManuallyDrop / std::mem::forget to skip the drop.

Comment thread tests/stream.rs
Comment on lines +132 to +137
fn check_content(owned_zip_file: OwnedZipFileEntry) {
let zip_file_entry = owned_zip_file.as_ref();
let mut reader = Cursor::new(include_bytes!("./data/mimetype.zip"));
let mut zip_file = zip_file_entry
.with_reader(&mut reader, ZipReadOptions::default())
.unwrap();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check_content function opens a brand-new Cursor over the raw mimetype.zip bytes and passes it to with_reader. The OwnedZipFileEntry was obtained from read_zipfile_from_stream, which reads sequentially; the header_start stored in the entry's ZipFileData reflects the byte offset in that stream. When find_content is called on the fresh cursor, it seeks to that offset and reads the local file header. This will work only if mimetype.zip actually begins at byte 0 and the stored offset matches — which happens to be true for this specific fixture. However, this pattern is fragile: if header_start is non-zero or the local file header check fails, with_reader returns an error at runtime. The test doesn't cover this failure path and silently relies on a coincidence of the fixture. Add an assertion or comment documenting that header_start must be 0 for this pattern to be safe, or guard against with_reader returning Err.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read_zipfile_from_stream doesn't currently work with leading non-ZIP data anyway, but this sounds like it could be an extra barrier to ever removing that limitation.

@Pr0methean
Pr0methean added this pull request to the merge queue Nov 6, 2026
Merged via the queue into master with commit b3ad5df Nov 6, 2026
240 of 241 checks passed
@Pr0methean
Pr0methean deleted the possible-fix-177 branch November 6, 2026 01:49
@Pr0methean Pr0methean mentioned this pull request Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants