Skip to content

Commit 7801023

Browse files
authored
zip: skip the entry name allocation when there is no add_path (#22975)
php_zip_add_file() takes a char*/size_t pair, so without a prefix to concatenate the zend_string was allocated and released once per entry only to pass one through. Pass file_stripped directly, and hold basename until after the call because file_stripped can point into it. Closes GH-22975
1 parent 87b510b commit 7801023

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

‎ext/zip/php_zip.c‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ static bool php_zip_extract_file(struct zip * za, char *dest, const char *file,
289289
/* }}} */
290290

291291
static zend_result php_zip_add_file(ze_zip_object *obj, const char *filename, size_t filename_len,
292-
char *entry_name, size_t entry_name_len, /* unused if replace >= 0 */
292+
const char *entry_name, size_t entry_name_len, /* unused if replace >= 0 */
293293
zip_uint64_t offset_start, zip_uint64_t offset_len,
294294
zend_long replace, /* index to replace, add new file if < 0 */
295295
zip_flags_t flags
@@ -1846,7 +1846,9 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
18461846
file_stripped_len = Z_STRLEN_P(zval_file);
18471847
}
18481848

1849-
zend_string *entry_name;
1849+
zend_string *entry_name = NULL;
1850+
const char *entry_name_str = file_stripped;
1851+
size_t entry_name_len = file_stripped_len;
18501852
if (opts.add_path) {
18511853
if ((ZSTR_LEN(opts.add_path) + file_stripped_len) > MAXPATHLEN) {
18521854
if (basename) {
@@ -1861,20 +1863,21 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
18611863
ZSTR_VAL(opts.add_path), ZSTR_LEN(opts.add_path),
18621864
file_stripped, file_stripped_len
18631865
);
1864-
} else {
1865-
entry_name = zend_string_init(file_stripped, file_stripped_len, false);
1866+
entry_name_str = ZSTR_VAL(entry_name);
1867+
entry_name_len = ZSTR_LEN(entry_name);
18661868
}
1867-
ZEND_ASSERT(ZSTR_LEN(entry_name) <= MAXPATHLEN);
1869+
ZEND_ASSERT(entry_name_len <= MAXPATHLEN);
1870+
1871+
const zend_result status = php_zip_add_file(ze_obj, Z_STRVAL_P(zval_file), Z_STRLEN_P(zval_file),
1872+
entry_name_str, entry_name_len, 0, 0, -1, opts.flags);
18681873

18691874
if (basename) {
18701875
zend_string_release_ex(basename, false);
18711876
basename = NULL;
18721877
}
1873-
1874-
const zend_result status = php_zip_add_file(ze_obj, Z_STRVAL_P(zval_file), Z_STRLEN_P(zval_file),
1875-
ZSTR_VAL(entry_name), ZSTR_LEN(entry_name), 0, 0, -1, opts.flags);
1876-
1877-
zend_string_release_ex(entry_name, false);
1878+
if (entry_name) {
1879+
zend_string_release_ex(entry_name, false);
1880+
}
18781881
if (status == FAILURE) {
18791882
zend_array_destroy(Z_ARR_P(return_value));
18801883
RETURN_FALSE;

0 commit comments

Comments
 (0)