Skip to content

Commit ce5af02

Browse files
committed
ext/phar:phar_split_fname(): use zend_string for entry
This saves on some reallocations
1 parent 07dd179 commit ce5af02

7 files changed

Lines changed: 47 additions & 56 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: 6 additions & 13 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
@@ -2224,20 +2224,13 @@ zend_string* phar_split_fname_ex(const char *filename, size_t filename_len, char
22242224
/* TODO: can we handle the unixify path in phar_fix_filepath() directly ? */
22252225
char *fixed_path_for_windows = estrndup(ext_str+ext_len, computed_entry_len);
22262226
phar_unixify_path_separators(fixed_path_for_windows, computed_entry_len);
2227-
zend_string *entry_str = phar_fix_filepath(fixed_path_for_windows, computed_entry_len, false);
2228-
*entry = estrndup(ZSTR_VAL(entry_str), ZSTR_LEN(entry_str));
2229-
*entry_len = ZSTR_LEN(entry_str);
2230-
zend_string_release_ex(entry_str, false);
2227+
*entry = phar_fix_filepath(fixed_path_for_windows, computed_entry_len, false);
22312228
efree(fixed_path_for_windows);
22322229
#else
2233-
zend_string *entry_str = phar_fix_filepath(ext_str+ext_len, computed_entry_len, false);
2234-
*entry = estrndup(ZSTR_VAL(entry_str), ZSTR_LEN(entry_str));
2235-
*entry_len = ZSTR_LEN(entry_str);
2236-
zend_string_release_ex(entry_str, false);
2230+
*entry = phar_fix_filepath(ext_str+ext_len, computed_entry_len, false);
22372231
#endif
22382232
} else {
2239-
*entry_len = 1;
2240-
*entry = estrndup("/", 1);
2233+
*entry = ZSTR_CHAR('/');
22412234
}
22422235
}
22432236

@@ -2251,8 +2244,8 @@ zend_string* phar_split_fname_ex(const char *filename, size_t filename_len, char
22512244
}
22522245
/* }}} */
22532246

2254-
zend_string* phar_split_fname(const char *filename, size_t filename_len, char **entry, size_t *entry_len, int executable, int for_create) {
2255-
return phar_split_fname_ex(filename, filename_len, entry, entry_len, executable, for_create, NULL);
2247+
zend_string* phar_split_fname(const char *filename, size_t filename_len, zend_string **entry, int executable, int for_create) {
2248+
return phar_split_fname_ex(filename, filename_len, entry, executable, for_create, NULL);
22562249
}
22572250

22582251
/**

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

@@ -1164,8 +1163,8 @@ PHP_METHOD(Phar, __construct)
11641163
phar_obj->spl.oth_handler = &phar_spl_foreign_handler;
11651164

11661165
if (entry) {
1167-
fname_len = spprintf(&fname, 0, "phar://%s%s", phar_data->fname, entry);
1168-
efree(entry);
1166+
fname_len = spprintf(&fname, 0, "phar://%s%s", phar_data->fname, ZSTR_VAL(entry));
1167+
zend_string_release_ex(entry, false);
11691168
} else {
11701169
fname_len = spprintf(&fname, 0, "phar://%s", phar_data->fname);
11711170
}
@@ -1261,7 +1260,7 @@ PHP_METHOD(Phar, unlinkArchive)
12611260

12621261
const zend_string *zend_file_name = zend_get_executed_filename_ex();
12631262
if (zend_file_name && zend_string_starts_with_literal_ci(zend_file_name, "phar://")) {
1264-
zend_string *arch = phar_split_fname(ZSTR_VAL(zend_file_name), ZSTR_LEN(zend_file_name), NULL, NULL, 2, 0);
1263+
zend_string *arch = phar_split_fname(ZSTR_VAL(zend_file_name), ZSTR_LEN(zend_file_name), NULL, 2, 0);
12651264
if (arch) {
12661265
if (ZSTR_LEN(arch) == fname_len && !memcmp(ZSTR_VAL(arch), fname, ZSTR_LEN(arch))) {
12671266
zend_string_release_ex(arch, false);
@@ -4365,9 +4364,8 @@ PHP_METHOD(Phar, extractTo)
43654364
/* {{{ Construct a Phar entry object */
43664365
PHP_METHOD(PharFileInfo, __construct)
43674366
{
4368-
char *fname, *entry, *error;
4367+
char *fname, *error;
43694368
size_t fname_len;
4370-
size_t entry_len;
43714369
phar_entry_object *entry_obj;
43724370
phar_entry_info *entry_info;
43734371
phar_archive_data *phar_data;
@@ -4390,7 +4388,8 @@ PHP_METHOD(PharFileInfo, __construct)
43904388
RETURN_THROWS();
43914389
}
43924390

4393-
zend_string *arch = phar_split_fname(fname, fname_len, &entry, &entry_len, 2, 0);
4391+
zend_string *entry = NULL;
4392+
zend_string *arch = phar_split_fname(fname, fname_len, &entry, 2, 0);
43944393
if (!arch) {
43954394
zend_throw_exception_ex(spl_ce_RuntimeException, 0,
43964395
"'%s' is not a valid phar archive URL (must have at least phar://filename.phar)", fname);
@@ -4399,7 +4398,7 @@ PHP_METHOD(PharFileInfo, __construct)
43994398

44004399
if (phar_open_from_filename(ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, REPORT_ERRORS, &phar_data, &error) == FAILURE) {
44014400
zend_string_release_ex(arch, false);
4402-
efree(entry);
4401+
zend_string_release_ex(entry, false);
44034402
if (error) {
44044403
zend_throw_exception_ex(spl_ce_RuntimeException, 0,
44054404
"Cannot open phar file '%s': %s", fname, error);
@@ -4411,16 +4410,16 @@ PHP_METHOD(PharFileInfo, __construct)
44114410
RETURN_THROWS();
44124411
}
44134412

4414-
if ((entry_info = phar_get_entry_info_dir(phar_data, entry, entry_len, 1, &error, true)) == NULL) {
4413+
if ((entry_info = phar_get_entry_info_dir(phar_data, ZSTR_VAL(entry), ZSTR_LEN(entry), 1, &error, true)) == NULL) {
44154414
zend_throw_exception_ex(spl_ce_RuntimeException, 0,
4416-
"Cannot access phar file entry '%s' in archive '%s'%s%s", entry, ZSTR_VAL(arch), error ? ", " : "", error ? error : "");
4415+
"Cannot access phar file entry '%s' in archive '%s'%s%s", ZSTR_VAL(entry), ZSTR_VAL(arch), error ? ", " : "", error ? error : "");
44174416
zend_string_release_ex(arch, false);
4418-
efree(entry);
4417+
zend_string_release_ex(entry, false);
44194418
RETURN_THROWS();
44204419
}
44214420

44224421
zend_string_release_ex(arch, false);
4423-
efree(entry);
4422+
zend_string_release_ex(entry, false);
44244423

44254424
entry_obj->entry = entry_info;
44264425
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)