Skip to content

Commit 4f3c28a

Browse files
authored
phar: Simplify phar_open_archive_fp() (#20753)
By returning the stream directly, we avoid calling some helpers functions and it becomes more clear on what stream the code actually acts upon.
1 parent 1faf17b commit 4f3c28a

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

ext/phar/phar.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,28 +2310,29 @@ ZEND_ATTRIBUTE_NONNULL zend_result phar_postprocess_file(phar_entry_data *idata,
23102310
/* verify local file header */
23112311
phar_zip_file_header local;
23122312
phar_zip_data_desc desc;
2313+
php_stream *stream = phar_open_archive_fp(idata->phar);
23132314

2314-
if (SUCCESS != phar_open_archive_fp(idata->phar)) {
2315+
if (!stream) {
23152316
spprintf(error, 0, "phar error: unable to open zip-based phar archive \"%s\" to verify local file header for file \"%s\"",
23162317
idata->phar->fname, ZSTR_VAL(entry->filename));
23172318
return FAILURE;
23182319
}
2319-
php_stream_seek(phar_get_entrypfp(idata->internal_file), entry->header_offset, SEEK_SET);
2320+
php_stream_seek(stream, entry->header_offset, SEEK_SET);
23202321

2321-
if (sizeof(local) != php_stream_read(phar_get_entrypfp(idata->internal_file), (char *) &local, sizeof(local))) {
2322+
if (sizeof(local) != php_stream_read(stream, (char *) &local, sizeof(local))) {
23222323
spprintf(error, 0, "phar error: internal corruption of zip-based phar \"%s\" (cannot read local file header for file \"%s\")",
23232324
idata->phar->fname, ZSTR_VAL(entry->filename));
23242325
return FAILURE;
23252326
}
23262327

23272328
/* check for data descriptor */
23282329
if (((PHAR_ZIP_16(local.flags)) & 0x8) == 0x8) {
2329-
php_stream_seek(phar_get_entrypfp(idata->internal_file),
2330+
php_stream_seek(stream,
23302331
entry->header_offset + sizeof(local) +
23312332
PHAR_ZIP_16(local.filename_len) +
23322333
PHAR_ZIP_16(local.extra_len) +
23332334
entry->compressed_filesize, SEEK_SET);
2334-
if (sizeof(desc) != php_stream_read(phar_get_entrypfp(idata->internal_file),
2335+
if (sizeof(desc) != php_stream_read(stream,
23352336
(char *) &desc, sizeof(desc))) {
23362337
spprintf(error, 0, "phar error: internal corruption of zip-based phar \"%s\" (cannot read local data descriptor for file \"%s\")",
23372338
idata->phar->fname, ZSTR_VAL(entry->filename));

ext/phar/phar_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ php_stream *phar_get_efp(phar_entry_info *entry, bool follow_links);
440440
ZEND_ATTRIBUTE_NONNULL zend_result phar_copy_entry_fp(phar_entry_info *source, phar_entry_info *dest, char **error);
441441
ZEND_ATTRIBUTE_NONNULL zend_result phar_open_entry_fp(phar_entry_info *entry, char **error, bool follow_links);
442442
phar_entry_info *phar_get_link_source(phar_entry_info *entry);
443-
zend_result phar_open_archive_fp(phar_archive_data *phar);
443+
php_stream *phar_open_archive_fp(phar_archive_data *phar);
444444
zend_result phar_copy_on_write(phar_archive_data **pphar);
445445

446446
/* tar functions in tar.c */

ext/phar/stream.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,13 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, const cha
254254
} else {
255255
php_stream *stream = phar_get_pharfp(phar);
256256
if (stream == NULL) {
257-
if (UNEXPECTED(FAILURE == phar_open_archive_fp(phar))) {
257+
stream = phar_open_archive_fp(phar);
258+
if (UNEXPECTED(!stream)) {
258259
php_stream_wrapper_log_error(wrapper, options, "phar error: could not reopen phar \"%s\"", ZSTR_VAL(resource->host));
259260
efree(internal_file);
260261
php_url_free(resource);
261262
return NULL;
262263
}
263-
stream = phar_get_pharfp(phar);
264264
}
265265

266266
phar_entry_info *entry;

ext/phar/util.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,12 @@ php_stream *phar_get_efp(phar_entry_info *entry, bool follow_links) /* {{{ */
107107
}
108108

109109
if (phar_get_fp_type(entry) == PHAR_FP) {
110-
if (!phar_get_entrypfp(entry)) {
110+
php_stream *stream = phar_get_entrypfp(entry);
111+
if (!stream) {
111112
/* re-open just in time for cases where our refcount reached 0 on the phar archive */
112-
phar_open_archive_fp(entry->phar);
113+
stream = phar_open_archive_fp(entry->phar);
113114
}
114-
return phar_get_entrypfp(entry);
115+
return stream;
115116
} else if (phar_get_fp_type(entry) == PHAR_UFP) {
116117
return phar_get_entrypufp(entry);
117118
} else if (entry->fp_type == PHAR_MOD) {
@@ -708,24 +709,23 @@ static inline void phar_set_pharfp(phar_archive_data *phar, php_stream *fp)
708709
PHAR_G(cached_fp)[phar->phar_pos].fp = fp;
709710
}
710711

711-
/* initialize a phar_archive_data's read-only fp for existing phar data */
712-
zend_result phar_open_archive_fp(phar_archive_data *phar) /* {{{ */
712+
/* Initialize a phar_archive_data's read-only fp for existing phar data.
713+
* The stream is owned by the `phar` object and must not be closed manually. */
714+
php_stream *phar_open_archive_fp(phar_archive_data *phar) /* {{{ */
713715
{
714-
if (phar_get_pharfp(phar)) {
715-
return SUCCESS;
716+
php_stream *stream = phar_get_pharfp(phar);
717+
if (stream) {
718+
return stream;
716719
}
717720

718721
if (php_check_open_basedir(phar->fname)) {
719-
return FAILURE;
722+
return NULL;
720723
}
721724

722-
phar_set_pharfp(phar, php_stream_open_wrapper(phar->fname, "rb", IGNORE_URL|STREAM_MUST_SEEK|0, NULL));
723-
724-
if (!phar_get_pharfp(phar)) {
725-
return FAILURE;
726-
}
725+
stream = php_stream_open_wrapper(phar->fname, "rb", IGNORE_URL|STREAM_MUST_SEEK, NULL);
726+
phar_set_pharfp(phar, stream);
727727

728-
return SUCCESS;
728+
return stream;
729729
}
730730
/* }}} */
731731

@@ -829,7 +829,7 @@ ZEND_ATTRIBUTE_NONNULL zend_result phar_open_entry_fp(phar_entry_info *entry, ch
829829
}
830830

831831
if (!phar_get_pharfp(phar)) {
832-
if (FAILURE == phar_open_archive_fp(phar)) {
832+
if (!phar_open_archive_fp(phar)) {
833833
spprintf(error, 4096, "phar error: Cannot open phar archive \"%s\" for reading", phar->fname);
834834
return FAILURE;
835835
}

0 commit comments

Comments
 (0)