-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix GH-17787: ZipArchive stream truncates when the archive is freed #22555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
eyupcanakman
wants to merge
3
commits into
php:PHP-8.4
from
eyupcanakman:fix/zip-stream-archive-lifetime
+99
−3
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| --TEST-- | ||
| GH-17787 (ZipArchive stream stops reading early when the archive is freed while the stream is open) | ||
| --EXTENSIONS-- | ||
| zip | ||
| --FILE-- | ||
| <?php | ||
| $name = __DIR__ . '/gh17787.zip'; | ||
| $data = str_repeat("The quick brown fox jumps over the lazy dog.\n", 4000); | ||
|
|
||
| $zip = new ZipArchive; | ||
| $zip->open($name, ZipArchive::CREATE | ZipArchive::OVERWRITE); | ||
| $zip->addFromString('entry.txt', $data); | ||
| $zip->close(); | ||
|
|
||
| $zip = new ZipArchive; | ||
| $zip->open($name, ZipArchive::RDONLY); | ||
| $stream = $zip->getStreamIndex(0, ZipArchive::FL_UNCHANGED); | ||
|
|
||
| // Free the archive while the stream is still open | ||
| $zip = null; | ||
|
|
||
| var_dump(stream_get_contents($stream) === $data); | ||
| fclose($stream); | ||
|
|
||
| // Same with getStreamName() | ||
| $zip = new ZipArchive; | ||
| $zip->open($name, ZipArchive::RDONLY); | ||
| $stream = $zip->getStreamName('entry.txt', ZipArchive::FL_UNCHANGED); | ||
| $zip = null; | ||
|
|
||
| var_dump(stream_get_contents($stream) === $data); | ||
| fclose($stream); | ||
|
|
||
| // Same with getStream() | ||
| $zip = new ZipArchive; | ||
| $zip->open($name, ZipArchive::RDONLY); | ||
| $stream = $zip->getStream('entry.txt'); | ||
| $zip = null; | ||
|
|
||
| var_dump(stream_get_contents($stream) === $data); | ||
| fclose($stream); | ||
|
|
||
| // Pending changes are still committed once the last stream is closed | ||
| $name = __DIR__ . '/gh17787_write.zip'; | ||
|
|
||
| $zip = new ZipArchive; | ||
| var_dump($zip->open($name, ZipArchive::CREATE | ZipArchive::OVERWRITE)); | ||
| $zip->addFromString('first.txt', 'first'); | ||
| $zip->close(); | ||
|
|
||
| $zip = new ZipArchive; | ||
| var_dump($zip->open($name)); | ||
| $zip->addFromString('second.txt', 'second'); | ||
| $stream = $zip->getStreamName('first.txt', ZipArchive::FL_UNCHANGED); | ||
| $zip = null; | ||
|
|
||
| var_dump(stream_get_contents($stream)); | ||
| fclose($stream); | ||
|
|
||
| $zip = new ZipArchive; | ||
| var_dump($zip->open($name, ZipArchive::RDONLY)); | ||
| var_dump($zip->numFiles); | ||
| var_dump($zip->getFromName('second.txt')); | ||
| $zip->close(); | ||
| ?> | ||
| --CLEAN-- | ||
| <?php | ||
| @unlink(__DIR__ . '/gh17787.zip'); | ||
| @unlink(__DIR__ . '/gh17787_write.zip'); | ||
| ?> | ||
| --EXPECT-- | ||
| bool(true) | ||
| bool(true) | ||
| bool(true) | ||
| bool(true) | ||
| bool(true) | ||
| string(5) "first" | ||
| bool(true) | ||
| int(2) | ||
| string(6) "second" |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say it is probably possible to avoid this change in a stable branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
php_stream_zip_openhas a single caller inphp_zip.c, the symbol isn't exported (noPHPAPI, hidden visibility) andphp_zip.hisn't installed, so the prototype change isn't visible outside the tree. Can keep the old signature and add a second function that takes the object if you'd rather avoid it on 8.4.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No you re right.
Would it be possible however to update the test as follow ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added both cases. The pending-changes one is a better test than I expected, since unpatched it does not just fail the read, it drops the queued write (
numFiles1,getFromNamefalse).One thing I hit while stress testing the patch. Pinning the object makes a subclass that stores its own stream uncollectable, because
php_zip_get_gccannot see theGC_ADDREFat zip_stream.c:266.Without the fix it prints bool(false). Over 500 iterations that is 700KB and one leaked fd each time. It needs the stream reachable from the object and never closed, and
fcloserecovers everything.I tried refcounting the zip_t instead so the stream never points back. That fixes the leak and breaks the fix, because
addFromStringhands libzip an object owned buffer viazip_source_buffer(..., 0)whichfree_storagefrees, so the queued entry writes back garbage.The same prototype with
addFileis fine, so it is the buffer ownership. Looks like the pin is needed here and a real fix means moving that ownership. Want me to open a separate issue for it?