Skip to content

Fix GH-17787: ZipArchive stream truncates when the archive is freed#22555

Open
eyupcanakman wants to merge 1 commit into
php:PHP-8.4from
eyupcanakman:fix/zip-stream-archive-lifetime
Open

Fix GH-17787: ZipArchive stream truncates when the archive is freed#22555
eyupcanakman wants to merge 1 commit into
php:PHP-8.4from
eyupcanakman:fix/zip-stream-archive-lifetime

Conversation

@eyupcanakman

Copy link
Copy Markdown
Contributor

ZipArchive::getStreamIndex() (and getStream/getStreamName) hands back a stream that reads from the archive's underlying zip_t without keeping a reference to it. When the ZipArchive object is freed while the stream is still open, as in the report where $z gets reassigned inside the read loop, the object destructor closes the zip_t and the stream stops reading early. cmb69 diagnosed this on the issue. The fix keeps the archive object alive for the stream's lifetime, so the borrowed handle stays valid until the stream is closed.

One case this does not cover is calling $zip->close() or reopening the same object while a stream is open, which hits the same truncation by a different route. I can extend the fix to those paths if you prefer.

…e is freed

getStream(), getStreamIndex() and getStreamName() return a stream that reads
from the ZipArchive's underlying zip_t but keeps no reference to it. When the
object is freed while the stream is still open, the destructor closes the
zip_t and reads stop partway through. Keep the archive object alive for the
stream's lifetime so the borrowed handle stays valid until the stream is
closed.
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.

1 participant