Skip to content

Commit aa3ab76

Browse files
committed
Revert "ext/phar:phar_split_fname(): use zend_string for entry"
This reverts commit 04a0da5.
1 parent 04a0da5 commit aa3ab76

7 files changed

Lines changed: 52 additions & 46 deletions

File tree

ext/phar/dirstream.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
349349
php_url *resource = NULL;
350350

351351
/* pre-readonly check, we need to know if this is a data phar */
352-
zend_string *arch = phar_split_fname(url_from, strlen(url_from), NULL, 2, 2);
352+
zend_string *arch = phar_split_fname(url_from, strlen(url_from), NULL, NULL, 2, 2);
353353
if (!arch) {
354354
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\", no phar archive specified", url_from);
355355
return 0;
@@ -475,7 +475,7 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
475475
php_url *resource = NULL;
476476

477477
/* pre-readonly check, we need to know if this is a data phar */
478-
zend_string *arch = phar_split_fname(url, strlen(url), NULL, 2, 2);
478+
zend_string *arch = phar_split_fname(url, strlen(url), NULL, NULL, 2, 2);
479479
if (!arch) {
480480
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot remove directory \"%s\", no phar archive specified, or phar archive does not exist", url);
481481
return 0;

ext/phar/func_interceptors.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ PHP_FUNCTION(phar_opendir) /* {{{ */
4646
goto skip_phar;
4747
}
4848

49-
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 2, 0);
49+
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, NULL, 2, 0);
5050
if (arch) {
5151
php_stream_context *context = NULL;
5252
php_stream *stream;
@@ -91,7 +91,7 @@ static zend_string* phar_get_name_for_relative_paths(zend_string *filename, bool
9191
return NULL;
9292
}
9393

94-
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 2, 0);
94+
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, NULL, 2, 0);
9595
if (!arch) {
9696
return NULL;
9797
}
@@ -491,7 +491,7 @@ static void phar_file_stat(const char *filename, size_t filename_length, int typ
491491
phar = PHAR_G(last_phar);
492492
goto splitted;
493493
}
494-
arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 2, 0);
494+
arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, NULL, 2, 0);
495495
if (arch) {
496496
/* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
497497
zend_result has_archive = phar_get_archive(&phar, ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL);
@@ -727,7 +727,7 @@ PHP_FUNCTION(phar_is_file) /* {{{ */
727727
goto skip_phar;
728728
}
729729

730-
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 2, 0);
730+
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, NULL, 2, 0);
731731
if (arch) {
732732
phar_archive_data *phar;
733733

@@ -784,7 +784,7 @@ PHP_FUNCTION(phar_is_link) /* {{{ */
784784
goto skip_phar;
785785
}
786786

787-
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 2, 0);
787+
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, NULL, 2, 0);
788788
if (arch) {
789789
phar_archive_data *phar;
790790

ext/phar/phar.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,7 +2163,7 @@ zend_string* phar_fix_filepath(const char *path, size_t path_length, bool use_cw
21632163
*
21642164
* This is used by phar_parse_url()
21652165
*/
2166-
zend_string* phar_split_fname_ex(const char *filename, size_t filename_len, zend_string **entry, int executable, int for_create, const char **error) /* {{{ */
2166+
zend_string* phar_split_fname_ex(const char *filename, size_t filename_len, char **entry, size_t *entry_len, int executable, int for_create, const char **error) /* {{{ */
21672167
{
21682168
const char *ext_str;
21692169
#ifdef PHP_WIN32
@@ -2222,9 +2222,13 @@ zend_string* phar_split_fname_ex(const char *filename, size_t filename_len, zend
22222222
size_t computed_entry_len = filename_len - arch_len;
22232223
/* We don't need to unixify the path on Windows,
22242224
* as ext_str is derived from filename that was already unixify */
2225-
*entry = phar_fix_filepath(ext_str+ext_len, computed_entry_len, false);
2225+
zend_string *entry_str = phar_fix_filepath(ext_str+ext_len, computed_entry_len, false);
2226+
*entry = estrndup(ZSTR_VAL(entry_str), ZSTR_LEN(entry_str));
2227+
*entry_len = ZSTR_LEN(entry_str);
2228+
zend_string_release_ex(entry_str, false);
22262229
} else {
2227-
*entry = ZSTR_CHAR('/');
2230+
*entry_len = 1;
2231+
*entry = estrndup("/", 1);
22282232
}
22292233
}
22302234

@@ -2238,8 +2242,8 @@ zend_string* phar_split_fname_ex(const char *filename, size_t filename_len, zend
22382242
}
22392243
/* }}} */
22402244

2241-
zend_string* phar_split_fname(const char *filename, size_t filename_len, zend_string **entry, int executable, int for_create) {
2242-
return phar_split_fname_ex(filename, filename_len, entry, executable, for_create, NULL);
2245+
zend_string* phar_split_fname(const char *filename, size_t filename_len, char **entry, size_t *entry_len, int executable, int for_create) {
2246+
return phar_split_fname_ex(filename, filename_len, entry, entry_len, executable, for_create, NULL);
22432247
}
22442248

22452249
/**

ext/phar/phar_internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,8 @@ ZEND_ATTRIBUTE_NONNULL zend_result phar_get_entry_data(phar_entry_data **ret, ch
474474
ZEND_ATTRIBUTE_NONNULL_ARGS(1, 4) int phar_flush_ex(phar_archive_data *archive, zend_string *user_stub, bool is_default_stub, char **error);
475475
ZEND_ATTRIBUTE_NONNULL int phar_flush(phar_archive_data *archive, char **error);
476476
zend_result phar_detect_phar_fname_ext(const char *filename, size_t filename_len, const char **ext_str, size_t *ext_len, int executable, int for_create, bool is_complete);
477-
zend_string* phar_split_fname_ex(const char *filename, size_t filename_len, zend_string **entry, int executable, int for_create, const char **error);
478-
zend_string* phar_split_fname(const char *filename, size_t filename_len, zend_string **entry, int executable, int for_create);
477+
zend_string* phar_split_fname_ex(const char *filename, size_t filename_len, char **entry, size_t *entry_len, int executable, int for_create, const char **error);
478+
zend_string* phar_split_fname(const char *filename, size_t filename_len, char **entry, size_t *entry_len, int executable, int for_create);
479479

480480
typedef enum {
481481
pcr_use_query,

ext/phar/phar_object.c

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ PHP_METHOD(Phar, running)
415415
}
416416

417417
if (zend_string_starts_with_literal_ci(fname, "phar://")) {
418-
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 2, 0);
418+
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, NULL, 2, 0);
419419
if (retphar) {
420420
RETVAL_STRINGL(ZSTR_VAL(fname), ZSTR_LEN(arch) + 7);
421421
zend_string_release_ex(arch, false);
@@ -436,8 +436,8 @@ PHP_METHOD(Phar, running)
436436
*/
437437
PHP_METHOD(Phar, mount)
438438
{
439-
char *fname, *path, *actual;
440-
size_t fname_len;
439+
char *fname, *entry = NULL, *path, *actual;
440+
size_t fname_len, entry_len;
441441
size_t path_len, actual_len;
442442
phar_archive_data *pphar;
443443
#ifdef PHP_WIN32
@@ -469,8 +469,9 @@ PHP_METHOD(Phar, mount)
469469
#endif
470470

471471
zend_string *arch = NULL;
472-
zend_string *entry = NULL;
473-
if (fname_len > 7 && !memcmp(fname, "phar://", 7) && (arch = phar_split_fname(fname, fname_len, NULL, 2, 0))) {
472+
if (fname_len > 7 && !memcmp(fname, "phar://", 7) && (arch = phar_split_fname(fname, fname_len, NULL, NULL, 2, 0))) {
473+
entry = NULL;
474+
474475
if (path_len > 7 && !memcmp(path, "phar://", 7)) {
475476
zend_throw_exception_ex(phar_ce_PharException, 0, "Can only mount internal paths within a phar archive, use a relative path instead of \"%s\"", path);
476477
zend_string_release_ex(arch, false);
@@ -494,8 +495,8 @@ PHP_METHOD(Phar, mount)
494495
zend_throw_exception_ex(phar_ce_PharException, 0, "Mounting of %s to %s within phar %s failed", path, actual, ZSTR_VAL(arch));
495496
}
496497

497-
if (entry && path == ZSTR_VAL(entry)) {
498-
zend_string_release_ex(entry, false);
498+
if (entry && path == entry) {
499+
efree(entry);
499500
}
500501

501502
if (arch) {
@@ -511,9 +512,9 @@ PHP_METHOD(Phar, mount)
511512
}
512513

513514
goto carry_on;
514-
} else if (NULL != (arch = phar_split_fname(path, path_len, &entry, 2, 0))) {
515-
path = ZSTR_VAL(entry);
516-
path_len = ZSTR_LEN(entry);
515+
} else if (NULL != (arch = phar_split_fname(path, path_len, &entry, &entry_len, 2, 0))) {
516+
path = entry;
517+
path_len = entry_len;
517518
goto carry_on2;
518519
}
519520

@@ -1067,8 +1068,9 @@ static const spl_other_handler phar_spl_foreign_handler = {
10671068
*/
10681069
PHP_METHOD(Phar, __construct)
10691070
{
1070-
char *fname, *alias = NULL, *error, *save_fname;
1071+
char *fname, *alias = NULL, *error, *entry = NULL, *save_fname;
10711072
size_t fname_len, alias_len = 0;
1073+
size_t entry_len;
10721074
bool is_data;
10731075
zend_long flags = SPL_FILE_DIR_SKIPDOTS|SPL_FILE_DIR_UNIXPATHS;
10741076
zend_long format = 0;
@@ -1099,8 +1101,7 @@ PHP_METHOD(Phar, __construct)
10991101
#ifdef PHP_WIN32
11001102
phar_unixify_path_separators(fname, fname_len);
11011103
#endif
1102-
zend_string *entry = NULL;
1103-
zend_string *arch = phar_split_fname(fname, fname_len, &entry, !is_data, 2);
1104+
zend_string *arch = phar_split_fname(fname, fname_len, &entry, &entry_len, !is_data, 2);
11041105
if (arch) {
11051106
/* use arch (the basename for the archive) for fname instead of fname */
11061107
/* this allows support for RecursiveDirectoryIterator of subdirectories */
@@ -1116,7 +1117,7 @@ PHP_METHOD(Phar, __construct)
11161117
}
11171118

11181119
if (entry) {
1119-
zend_string_release_ex(entry, false);
1120+
efree(entry);
11201121
}
11211122

11221123
if (error) {
@@ -1149,7 +1150,7 @@ PHP_METHOD(Phar, __construct)
11491150
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
11501151
"Phar class can only be used for executable tar and zip archives");
11511152
}
1152-
zend_string_release_ex(entry, false);
1153+
efree(entry);
11531154
RETURN_THROWS();
11541155
}
11551156

@@ -1167,9 +1168,9 @@ PHP_METHOD(Phar, __construct)
11671168
file_name_for_recursive_director_iterator_constructor = zend_string_concat3(
11681169
ZEND_STRL("phar://"),
11691170
phar_data->fname, phar_data->fname_len,
1170-
ZSTR_VAL(entry), ZSTR_LEN(entry)
1171+
entry, entry_len
11711172
);
1172-
zend_string_release_ex(entry, false);
1173+
efree(entry);
11731174
} else {
11741175
file_name_for_recursive_director_iterator_constructor = zend_string_concat2(
11751176
ZEND_STRL("phar://"),
@@ -1268,7 +1269,7 @@ PHP_METHOD(Phar, unlinkArchive)
12681269

12691270
const zend_string *zend_file_name = zend_get_executed_filename_ex();
12701271
if (zend_file_name && zend_string_starts_with_literal_ci(zend_file_name, "phar://")) {
1271-
zend_string *arch = phar_split_fname(ZSTR_VAL(zend_file_name), ZSTR_LEN(zend_file_name), NULL, 2, 0);
1272+
zend_string *arch = phar_split_fname(ZSTR_VAL(zend_file_name), ZSTR_LEN(zend_file_name), NULL, NULL, 2, 0);
12721273
if (arch) {
12731274
if (ZSTR_LEN(arch) == fname_len && !memcmp(ZSTR_VAL(arch), fname, ZSTR_LEN(arch))) {
12741275
zend_string_release_ex(arch, false);
@@ -4372,8 +4373,9 @@ PHP_METHOD(Phar, extractTo)
43724373
/* {{{ Construct a Phar entry object */
43734374
PHP_METHOD(PharFileInfo, __construct)
43744375
{
4375-
char *fname, *error;
4376+
char *fname, *entry, *error;
43764377
size_t fname_len;
4378+
size_t entry_len;
43774379
phar_entry_object *entry_obj;
43784380
phar_entry_info *entry_info;
43794381
phar_archive_data *phar_data;
@@ -4396,8 +4398,7 @@ PHP_METHOD(PharFileInfo, __construct)
43964398
RETURN_THROWS();
43974399
}
43984400

4399-
zend_string *entry = NULL;
4400-
zend_string *arch = phar_split_fname(fname, fname_len, &entry, 2, 0);
4401+
zend_string *arch = phar_split_fname(fname, fname_len, &entry, &entry_len, 2, 0);
44014402
if (!arch) {
44024403
zend_throw_exception_ex(spl_ce_RuntimeException, 0,
44034404
"'%s' is not a valid phar archive URL (must have at least phar://filename.phar)", fname);
@@ -4406,7 +4407,7 @@ PHP_METHOD(PharFileInfo, __construct)
44064407

44074408
if (phar_open_from_filename(ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, REPORT_ERRORS, &phar_data, &error) == FAILURE) {
44084409
zend_string_release_ex(arch, false);
4409-
zend_string_release_ex(entry, false);
4410+
efree(entry);
44104411
if (error) {
44114412
zend_throw_exception_ex(spl_ce_RuntimeException, 0,
44124413
"Cannot open phar file '%s': %s", fname, error);
@@ -4418,16 +4419,16 @@ PHP_METHOD(PharFileInfo, __construct)
44184419
RETURN_THROWS();
44194420
}
44204421

4421-
if ((entry_info = phar_get_entry_info_dir(phar_data, ZSTR_VAL(entry), ZSTR_LEN(entry), 1, &error, true)) == NULL) {
4422+
if ((entry_info = phar_get_entry_info_dir(phar_data, entry, entry_len, 1, &error, true)) == NULL) {
44224423
zend_throw_exception_ex(spl_ce_RuntimeException, 0,
4423-
"Cannot access phar file entry '%s' in archive '%s'%s%s", ZSTR_VAL(entry), ZSTR_VAL(arch), error ? ", " : "", error ? error : "");
4424+
"Cannot access phar file entry '%s' in archive '%s'%s%s", entry, ZSTR_VAL(arch), error ? ", " : "", error ? error : "");
44244425
zend_string_release_ex(arch, false);
4425-
zend_string_release_ex(entry, false);
4426+
efree(entry);
44264427
RETURN_THROWS();
44274428
}
44284429

44294430
zend_string_release_ex(arch, false);
4430-
zend_string_release_ex(entry, false);
4431+
efree(entry);
44314432

44324433
entry_obj->entry = entry_info;
44334434
if (!entry_info->is_persistent && !entry_info->is_temp_dir) {

ext/phar/stream.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ const php_stream_wrapper php_stream_phar_wrapper = {
5858
php_url* phar_parse_url(php_stream_wrapper *wrapper, const char *filename, const char *mode, int options) /* {{{ */
5959
{
6060
php_url *resource;
61-
char *error;
61+
char *entry = NULL, *error;
62+
size_t entry_len;
6263

6364
if (strncasecmp(filename, "phar://", 7)) {
6465
return NULL;
@@ -70,9 +71,8 @@ php_url* phar_parse_url(php_stream_wrapper *wrapper, const char *filename, const
7071
return NULL;
7172
}
7273

73-
zend_string *entry = NULL;
7474
const char *arch_error;
75-
zend_string *arch = phar_split_fname_ex(filename, strlen(filename), &entry, 2, (mode[0] == 'w' ? 2 : 0), &arch_error);
75+
zend_string *arch = phar_split_fname_ex(filename, strlen(filename), &entry, &entry_len, 2, (mode[0] == 'w' ? 2 : 0), &arch_error);
7676
if (!arch) {
7777
if (!(options & PHP_STREAM_URL_STAT_QUIET)) {
7878
if (arch_error && !entry) {
@@ -86,7 +86,8 @@ php_url* phar_parse_url(php_stream_wrapper *wrapper, const char *filename, const
8686
resource = ecalloc(1, sizeof(php_url));
8787
resource->scheme = ZSTR_INIT_LITERAL("phar", 0);
8888
resource->host = arch;
89-
resource->path = entry;
89+
resource->path = zend_string_init(entry, entry_len, 0);
90+
efree(entry);
9091

9192
#ifdef MBO_0
9293
if (resource) {

ext/phar/util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ zend_string *phar_find_in_include_path(zend_string *filename, phar_archive_data
302302
if (!is_file_a_phar_wrapper) {
303303
return NULL;
304304
}
305-
arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 1, 0);
305+
arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, NULL, 1, 0);
306306
if (!arch) {
307307
return NULL;
308308
}
@@ -347,7 +347,7 @@ zend_string *phar_find_in_include_path(zend_string *filename, phar_archive_data
347347

348348
if (ret && zend_string_starts_with_literal_ci(ret, "phar://")) {
349349
/* found phar:// */
350-
arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 1, 0);
350+
arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, NULL, 1, 0);
351351
if (!arch) {
352352
return ret;
353353
}

0 commit comments

Comments
 (0)