Skip to content

Commit 264d650

Browse files
committed
ext/phar/phar_object.c: Refactor extraction code to use zend_string*
1 parent bf44b6e commit 264d650

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

ext/phar/phar_object.c

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4108,7 +4108,7 @@ PHP_METHOD(Phar, delMetadata)
41084108
}
41094109
/* }}} */
41104110

4111-
static zend_result phar_extract_file(bool overwrite, phar_entry_info *entry, char *dest, size_t dest_len, char **error) /* {{{ */
4111+
static zend_result phar_extract_file(bool overwrite, phar_entry_info *entry, const zend_string *path, char **error) /* {{{ */
41124112
{
41134113
php_stream_statbuf ssb;
41144114
size_t len;
@@ -4136,7 +4136,7 @@ static zend_result phar_extract_file(bool overwrite, phar_entry_info *entry, cha
41364136
if (virtual_file_ex(&new_state, ZSTR_VAL(entry->filename), NULL, CWD_EXPAND) != 0 ||
41374137
new_state.cwd_length <= 1) {
41384138
if (EINVAL == errno && ZSTR_LEN(entry->filename) > 50) {
4139-
spprintf(error, 4096, "Cannot extract \"%.50s...\" to \"%s...\", extracted filename is too long for filesystem", ZSTR_VAL(entry->filename), dest);
4139+
spprintf(error, 4096, "Cannot extract \"%.50s...\" to \"%s...\", extracted filename is too long for filesystem", ZSTR_VAL(entry->filename), ZSTR_VAL(path));
41404140
} else {
41414141
spprintf(error, 4096, "Cannot extract \"%s\", internal error", ZSTR_VAL(entry->filename));
41424142
}
@@ -4158,7 +4158,7 @@ static zend_result phar_extract_file(bool overwrite, phar_entry_info *entry, cha
41584158
}
41594159
#endif
41604160

4161-
len = spprintf(&fullpath, 0, "%s/%s", dest, filename);
4161+
len = spprintf(&fullpath, 0, "%s/%s", ZSTR_VAL(path), filename);
41624162

41634163
if (len >= MAXPATHLEN) {
41644164
/* truncate for error message */
@@ -4199,9 +4199,9 @@ static zend_result phar_extract_file(bool overwrite, phar_entry_info *entry, cha
41994199
slash = zend_memrchr(filename, '/', filename_len);
42004200

42014201
if (slash) {
4202-
fullpath[dest_len + (slash - filename) + 1] = '\0';
4202+
fullpath[ZSTR_LEN(path) + (slash - filename) + 1] = '\0';
42034203
} else {
4204-
fullpath[dest_len] = '\0';
4204+
fullpath[ZSTR_LEN(path)] = '\0';
42054205
}
42064206

42074207
if (FAILURE == php_stream_stat_path(fullpath, &ssb)) {
@@ -4223,9 +4223,9 @@ static zend_result phar_extract_file(bool overwrite, phar_entry_info *entry, cha
42234223
}
42244224

42254225
if (slash) {
4226-
fullpath[dest_len + (slash - filename) + 1] = '/';
4226+
fullpath[ZSTR_LEN(path) + (slash - filename) + 1] = '/';
42274227
} else {
4228-
fullpath[dest_len] = '/';
4228+
fullpath[ZSTR_LEN(path)] = '/';
42294229
}
42304230

42314231
filename = NULL;
@@ -4285,28 +4285,28 @@ static zend_result phar_extract_file(bool overwrite, phar_entry_info *entry, cha
42854285
}
42864286
/* }}} */
42874287

4288-
static int extract_helper(phar_archive_data *archive, zend_string *search, char *pathto, size_t pathto_len, bool overwrite, char **error) { /* {{{ */
4288+
static int extract_helper(const phar_archive_data *archive, zend_string *search, const zend_string *path_to, bool overwrite, char **error) { /* {{{ */
42894289
int extracted = 0;
42904290
phar_entry_info *entry;
42914291

42924292
if (!search) {
42934293
/* nothing to match -- extract all files */
42944294
ZEND_HASH_MAP_FOREACH_PTR(&archive->manifest, entry) {
4295-
if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, error)) return -1;
4295+
if (FAILURE == phar_extract_file(overwrite, entry, path_to, error)) return -1;
42964296
extracted++;
42974297
} ZEND_HASH_FOREACH_END();
42984298
} else if (ZSTR_LEN(search) > 0 && '/' == ZSTR_VAL(search)[ZSTR_LEN(search) - 1]) {
42994299
/* ends in "/" -- extract all entries having that prefix */
43004300
ZEND_HASH_MAP_FOREACH_PTR(&archive->manifest, entry) {
43014301
if (!zend_string_starts_with(entry->filename, search)) continue;
4302-
if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, error)) return -1;
4302+
if (FAILURE == phar_extract_file(overwrite, entry, path_to, error)) return -1;
43034303
extracted++;
43044304
} ZEND_HASH_FOREACH_END();
43054305
} else {
43064306
/* otherwise, looking for an exact match */
43074307
entry = zend_hash_find_ptr(&archive->manifest, search);
43084308
if (NULL == entry) return 0;
4309-
if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, error)) return -1;
4309+
if (FAILURE == phar_extract_file(overwrite, entry, path_to, error)) return -1;
43104310
return 1;
43114311
}
43124312

@@ -4319,17 +4319,16 @@ PHP_METHOD(Phar, extractTo)
43194319
{
43204320
php_stream *fp;
43214321
php_stream_statbuf ssb;
4322-
char *pathto;
4322+
zend_string *path_to;
43234323
zend_string *filename = NULL;
4324-
size_t pathto_len;
43254324
int ret;
43264325
zval *zval_file;
43274326
HashTable *files_ht = NULL;
43284327
bool overwrite = 0;
43294328
char *error = NULL;
43304329

43314330
ZEND_PARSE_PARAMETERS_START(1, 3)
4332-
Z_PARAM_PATH(pathto, pathto_len)
4331+
Z_PARAM_PATH_STR(path_to)
43334332
Z_PARAM_OPTIONAL
43344333
Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(files_ht, filename)
43354334
Z_PARAM_BOOL(overwrite)
@@ -4347,30 +4346,30 @@ PHP_METHOD(Phar, extractTo)
43474346

43484347
php_stream_close(fp);
43494348

4350-
if (pathto_len < 1) {
4349+
if (ZSTR_LEN(path_to) == 0) {
43514350
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
43524351
"Invalid argument, extraction path must be non-zero length");
43534352
RETURN_THROWS();
43544353
}
43554354

4356-
if (pathto_len >= MAXPATHLEN) {
4357-
char *tmp = estrndup(pathto, 50);
4355+
if (ZSTR_LEN(path_to) >= MAXPATHLEN) {
4356+
char *tmp = estrndup(ZSTR_VAL(path_to), 50);
43584357
/* truncate for error message */
43594358
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Cannot extract to \"%s...\", destination directory is too long for filesystem", tmp);
43604359
efree(tmp);
43614360
RETURN_THROWS();
43624361
}
43634362

4364-
if (php_stream_stat_path(pathto, &ssb) < 0) {
4365-
ret = php_stream_mkdir(pathto, 0777, PHP_STREAM_MKDIR_RECURSIVE, NULL);
4363+
if (php_stream_stat_path(ZSTR_VAL(path_to), &ssb) < 0) {
4364+
ret = php_stream_mkdir(ZSTR_VAL(path_to), 0777, PHP_STREAM_MKDIR_RECURSIVE, NULL);
43664365
if (!ret) {
43674366
zend_throw_exception_ex(spl_ce_RuntimeException, 0,
4368-
"Unable to create path \"%s\" for extraction", pathto);
4367+
"Unable to create path \"%s\" for extraction", ZSTR_VAL(path_to));
43694368
RETURN_THROWS();
43704369
}
43714370
} else if (!(ssb.sb.st_mode & S_IFDIR)) {
43724371
zend_throw_exception_ex(spl_ce_RuntimeException, 0,
4373-
"Unable to use path \"%s\" for extraction, it is a file, must be a directory", pathto);
4372+
"Unable to use path \"%s\" for extraction, it is a file, must be a directory", ZSTR_VAL(path_to));
43744373
RETURN_THROWS();
43754374
}
43764375

@@ -4386,7 +4385,7 @@ PHP_METHOD(Phar, extractTo)
43864385
"Invalid argument, array of filenames to extract contains non-string value");
43874386
RETURN_THROWS();
43884387
}
4389-
switch (extract_helper(phar_obj->archive, Z_STR_P(zval_file), pathto, pathto_len, overwrite, &error)) {
4388+
switch (extract_helper(phar_obj->archive, Z_STR_P(zval_file), path_to, overwrite, &error)) {
43904389
case -1:
43914390
zend_throw_exception_ex(phar_ce_PharException, 0, "Extraction from phar \"%s\" failed: %s",
43924391
phar_obj->archive->fname, error);
@@ -4402,7 +4401,7 @@ PHP_METHOD(Phar, extractTo)
44024401
RETURN_TRUE;
44034402
}
44044403

4405-
ret = extract_helper(phar_obj->archive, filename, pathto, pathto_len, overwrite, &error);
4404+
ret = extract_helper(phar_obj->archive, filename, path_to, overwrite, &error);
44064405
if (-1 == ret) {
44074406
zend_throw_exception_ex(phar_ce_PharException, 0, "Extraction from phar \"%s\" failed: %s",
44084407
phar_obj->archive->fname, error);

0 commit comments

Comments
 (0)