Skip to content

Commit cf30947

Browse files
committed
ext/phar:phar_split_fname(): use zend_string for entry
This saves on some reallocations
1 parent 18f97ab commit cf30947

7 files changed

Lines changed: 46 additions & 52 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, NULL, 2, 2);
352+
zend_string *arch = phar_split_fname(url_from, strlen(url_from), 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, NULL, 2, 2);
478+
zend_string *arch = phar_split_fname(url, strlen(url), 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, NULL, 2, 0);
49+
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), 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, NULL, 2, 0);
94+
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), 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, NULL, 2, 0);
494+
arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), 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, NULL, 2, 0);
730+
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), 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, NULL, 2, 0);
787+
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 2, 0);
788788
if (arch) {
789789
phar_archive_data *phar;
790790

ext/phar/phar.c

Lines changed: 5 additions & 9 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, char **entry, size_t *entry_len, int executable, int for_create, const char **error) /* {{{ */
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) /* {{{ */
21672167
{
21682168
const char *ext_str;
21692169
#ifdef PHP_WIN32
@@ -2222,13 +2222,9 @@ zend_string* phar_split_fname_ex(const char *filename, size_t filename_len, char
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-
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);
2225+
*entry = phar_fix_filepath(ext_str+ext_len, computed_entry_len, false);
22292226
} else {
2230-
*entry_len = 1;
2231-
*entry = estrndup("/", 1);
2227+
*entry = ZSTR_CHAR('/');
22322228
}
22332229
}
22342230

@@ -2242,8 +2238,8 @@ zend_string* phar_split_fname_ex(const char *filename, size_t filename_len, char
22422238
}
22432239
/* }}} */
22442240

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);
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);
22472243
}
22482244

22492245
/**

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, 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);
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);
479479

480480
typedef enum {
481481
pcr_use_query,

ext/phar/phar_object.c

Lines changed: 26 additions & 27 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, NULL, 2, 0);
418+
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), 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, *entry = NULL, *path, *actual;
440-
size_t fname_len, entry_len;
439+
char *fname, *path, *actual;
440+
size_t fname_len;
441441
size_t path_len, actual_len;
442442
phar_archive_data *pphar;
443443
#ifdef PHP_WIN32
@@ -469,9 +469,8 @@ PHP_METHOD(Phar, mount)
469469
#endif
470470

471471
zend_string *arch = NULL;
472-
if (fname_len > 7 && !memcmp(fname, "phar://", 7) && (arch = phar_split_fname(fname, fname_len, NULL, NULL, 2, 0))) {
473-
entry = NULL;
474-
472+
zend_string *entry = NULL;
473+
if (fname_len > 7 && !memcmp(fname, "phar://", 7) && (arch = phar_split_fname(fname, fname_len, NULL, 2, 0))) {
475474
if (path_len > 7 && !memcmp(path, "phar://", 7)) {
476475
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);
477476
zend_string_release_ex(arch, false);
@@ -495,8 +494,8 @@ PHP_METHOD(Phar, mount)
495494
zend_throw_exception_ex(phar_ce_PharException, 0, "Mounting of %s to %s within phar %s failed", path, actual, ZSTR_VAL(arch));
496495
}
497496

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

502501
if (arch) {
@@ -512,9 +511,9 @@ PHP_METHOD(Phar, mount)
512511
}
513512

514513
goto carry_on;
515-
} else if (NULL != (arch = phar_split_fname(path, path_len, &entry, &entry_len, 2, 0))) {
516-
path = entry;
517-
path_len = entry_len;
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);
518517
goto carry_on2;
519518
}
520519

@@ -1068,9 +1067,8 @@ static const spl_other_handler phar_spl_foreign_handler = {
10681067
*/
10691068
PHP_METHOD(Phar, __construct)
10701069
{
1071-
char *fname, *alias = NULL, *error, *entry = NULL, *save_fname;
1070+
char *fname, *alias = NULL, *error, *save_fname;
10721071
size_t fname_len, alias_len = 0;
1073-
size_t entry_len;
10741072
bool is_data;
10751073
zend_long flags = SPL_FILE_DIR_SKIPDOTS|SPL_FILE_DIR_UNIXPATHS;
10761074
zend_long format = 0;
@@ -1101,7 +1099,8 @@ PHP_METHOD(Phar, __construct)
11011099
#ifdef PHP_WIN32
11021100
phar_unixify_path_separators(fname, fname_len);
11031101
#endif
1104-
zend_string *arch = phar_split_fname(fname, fname_len, &entry, &entry_len, !is_data, 2);
1102+
zend_string *entry = NULL;
1103+
zend_string *arch = phar_split_fname(fname, fname_len, &entry, !is_data, 2);
11051104
if (arch) {
11061105
/* use arch (the basename for the archive) for fname instead of fname */
11071106
/* this allows support for RecursiveDirectoryIterator of subdirectories */
@@ -1117,7 +1116,7 @@ PHP_METHOD(Phar, __construct)
11171116
}
11181117

11191118
if (entry) {
1120-
efree(entry);
1119+
zend_string_release_ex(entry, false);
11211120
}
11221121

11231122
if (error) {
@@ -1150,7 +1149,7 @@ PHP_METHOD(Phar, __construct)
11501149
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
11511150
"Phar class can only be used for executable tar and zip archives");
11521151
}
1153-
efree(entry);
1152+
zend_string_release_ex(entry, false);
11541153
RETURN_THROWS();
11551154
}
11561155

@@ -1168,9 +1167,9 @@ PHP_METHOD(Phar, __construct)
11681167
file_name_for_recursive_director_iterator_constructor = zend_string_concat3(
11691168
ZEND_STRL("phar://"),
11701169
phar_data->fname, phar_data->fname_len,
1171-
entry, entry_len
1170+
ZSTR_VAL(entry), ZSTR_LEN(entry)
11721171
);
1173-
efree(entry);
1172+
zend_string_release_ex(entry, false);
11741173
} else {
11751174
file_name_for_recursive_director_iterator_constructor = zend_string_concat2(
11761175
ZEND_STRL("phar://"),
@@ -1269,7 +1268,7 @@ PHP_METHOD(Phar, unlinkArchive)
12691268

12701269
const zend_string *zend_file_name = zend_get_executed_filename_ex();
12711270
if (zend_file_name && zend_string_starts_with_literal_ci(zend_file_name, "phar://")) {
1272-
zend_string *arch = phar_split_fname(ZSTR_VAL(zend_file_name), ZSTR_LEN(zend_file_name), NULL, NULL, 2, 0);
1271+
zend_string *arch = phar_split_fname(ZSTR_VAL(zend_file_name), ZSTR_LEN(zend_file_name), NULL, 2, 0);
12731272
if (arch) {
12741273
if (ZSTR_LEN(arch) == fname_len && !memcmp(ZSTR_VAL(arch), fname, ZSTR_LEN(arch))) {
12751274
zend_string_release_ex(arch, false);
@@ -4373,9 +4372,8 @@ PHP_METHOD(Phar, extractTo)
43734372
/* {{{ Construct a Phar entry object */
43744373
PHP_METHOD(PharFileInfo, __construct)
43754374
{
4376-
char *fname, *entry, *error;
4375+
char *fname, *error;
43774376
size_t fname_len;
4378-
size_t entry_len;
43794377
phar_entry_object *entry_obj;
43804378
phar_entry_info *entry_info;
43814379
phar_archive_data *phar_data;
@@ -4398,7 +4396,8 @@ PHP_METHOD(PharFileInfo, __construct)
43984396
RETURN_THROWS();
43994397
}
44004398

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

44084407
if (phar_open_from_filename(ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, REPORT_ERRORS, &phar_data, &error) == FAILURE) {
44094408
zend_string_release_ex(arch, false);
4410-
efree(entry);
4409+
zend_string_release_ex(entry, false);
44114410
if (error) {
44124411
zend_throw_exception_ex(spl_ce_RuntimeException, 0,
44134412
"Cannot open phar file '%s': %s", fname, error);
@@ -4419,16 +4418,16 @@ PHP_METHOD(PharFileInfo, __construct)
44194418
RETURN_THROWS();
44204419
}
44214420

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

44304429
zend_string_release_ex(arch, false);
4431-
efree(entry);
4430+
zend_string_release_ex(entry, false);
44324431

44334432
entry_obj->entry = entry_info;
44344433
if (!entry_info->is_persistent && !entry_info->is_temp_dir) {

ext/phar/stream.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ 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 *entry = NULL, *error;
62-
size_t entry_len;
61+
char *error;
6362

6463
if (strncasecmp(filename, "phar://", 7)) {
6564
return NULL;
@@ -71,8 +70,9 @@ php_url* phar_parse_url(php_stream_wrapper *wrapper, const char *filename, const
7170
return NULL;
7271
}
7372

73+
zend_string *entry = NULL;
7474
const char *arch_error;
75-
zend_string *arch = phar_split_fname_ex(filename, strlen(filename), &entry, &entry_len, 2, (mode[0] == 'w' ? 2 : 0), &arch_error);
75+
zend_string *arch = phar_split_fname_ex(filename, strlen(filename), &entry, 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,8 +86,7 @@ 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 = zend_string_init(entry, entry_len, 0);
90-
efree(entry);
89+
resource->path = entry;
9190

9291
#ifdef MBO_0
9392
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, NULL, 1, 0);
305+
arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), 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, NULL, 1, 0);
350+
arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 1, 0);
351351
if (!arch) {
352352
return ret;
353353
}

0 commit comments

Comments
 (0)