gh-156434: Restore entry offsets when repack() fails - #156435
Open
fedonman wants to merge 1 commit into
Open
Conversation
_ZipRepacker.repack() updates ZipInfo.header_offset for each member before moving that member's bytes, so a failing _copy_bytes() left the in-memory offsets describing a layout that was never written. remove() has already set _didModify, so a later close() committed a central directory built from those offsets, and the archive became unreadable even though the caller had handled the error. ZipFile.repack() now snapshots the offsets of the surviving and the removed entries and restores them if the repack raises. The copy only ever moves data to lower offsets, so a partial move never overwrites its own source and the archive falls back to the state of a remove() with no repack().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ZipFile.repack()now saves theheader_offsetof every entry it hands to the repacker, including the removed ones, and restores them if the repack raises. A failed move no longer leaves the in-memory offsets describing a layout that was never written, so a laterclose()no longer commits it.No changelog entry:
remove()andrepack()are new in 3.16 and have not been released, so this belongs with the original entry for gh-51067.ZipFile.close()writes wrong offsets after a failedrepack()#156434