Skip to content

Commit 8adcb24

Browse files
committed
zip: skip the entry name allocation when there is no add_path
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.
1 parent 0dc8a54 commit 8adcb24

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
@@ -1845,7 +1845,9 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
18451845
file_stripped_len = Z_STRLEN_P(zval_file);
18461846
}
18471847

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

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

0 commit comments

Comments
 (0)