Skip to content

ext/zip: use ZIP_RDONLY when calling zip_open() in zip_stream.c - #23976

Open
DanielEScherzer wants to merge 2 commits into
php:PHP-8.6from
DanielEScherzer:zip-read-only
Open

DanielEScherzer wants to merge 2 commits into
php:PHP-8.6from
DanielEScherzer:zip-read-only

Conversation

@DanielEScherzer

Copy link
Copy Markdown
Member

The code paths that call zip_open() are used for stats and reading, not writing. Previously ZIP_CREATE would create an in-memory zip archive, and if it were written to, would persist it to disk when closed, but since the archive was never written to, this functionality was unneeded. Use the ZIP_RDONLY flag to make it clearer that the zip archive is not being written to.

@LamentXU123

Copy link
Copy Markdown
Member

The Windows CI seems related.

A follow-up commit will switch the uses of the `ZIP_CREATE` to instead use
`ZIP_RDONLY` when creation is not desired; add tests to confirm that doing so
does not change the actual behavior of the extension.
The code paths that call `zip_open()` are used for stats and reading, not
writing. Previously `ZIP_CREATE` would create an in-memory zip archive, and if
it were written to, would persist it to disk when closed, but since the archive
was never written to, this functionality was unneeded. Use the `ZIP_RDONLY`
flag to make it clearer that the zip archive is not being written to.

@LamentXU123 LamentXU123 left a comment

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.

Interesting fix.

$target = __FILE__;

echo "Stat:\n";
var_dump(stat("zip://$target#entry1.txt"));

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.

I doubt this will actually test this patch. Perhaps

--TEST--
fstat() on an existing zip entry
--EXTENSIONS--
zip
--FILE--
<?php
$target = __DIR__ . '/test.zip';
$fp = fopen("zip://$target#entry1.txt", 'rb');

$stat = fstat($fp);
var_dump($stat['size']);
var_dump(stream_get_contents($fp));

fclose($fp);
?>
--EXPECT--
int(8)
string(8) "entry #1"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The point of this test is the behavior of stat when using zip: for something that isn't a zip - the stats for an existing zip entry are covered by ext/zip/tests/stats_existing.phpt. Was your suggestion meant to be for that file?

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.

Yes, sorry for the confusion :)

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