From 4a7dacb5ee0117a951c8c0e5b1738131b17dda33 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sun, 28 Jan 2018 22:04:24 +0100 Subject: [PATCH] Don't loop over indexes in Phar::extractTo() Instead use a more idiomatic foreach loop. The behavior is not strictly the same, but I see no reason why this specific case should enforce continuously indexed integer keys. Also handle references in the array while at it. --- ext/phar/phar_object.c | 44 ++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c index 7bba619574..6234505995 100644 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -4385,8 +4385,7 @@ PHP_METHOD(Phar, extractTo) char *pathto; zend_string *filename; size_t pathto_len; - int ret, i; - int nelems; + int ret; zval *zval_file; zval *zval_files = NULL; zend_bool overwrite = 0; @@ -4444,31 +4443,30 @@ PHP_METHOD(Phar, extractTo) filename = Z_STR_P(zval_files); break; case IS_ARRAY: - nelems = zend_hash_num_elements(Z_ARRVAL_P(zval_files)); - if (nelems == 0 ) { + if (zend_hash_num_elements(Z_ARRVAL_P(zval_files)) == 0) { RETURN_FALSE; } - for (i = 0; i < nelems; i++) { - if ((zval_file = zend_hash_index_find(Z_ARRVAL_P(zval_files), i)) != NULL) { - if (IS_STRING != Z_TYPE_P(zval_file)) { - zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, - "Invalid argument, array of filenames to extract contains non-string value"); + + ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(zval_files), zval_file) { + ZVAL_DEREF(zval_file); + if (IS_STRING != Z_TYPE_P(zval_file)) { + zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, + "Invalid argument, array of filenames to extract contains non-string value"); + return; + } + switch (extract_helper(phar_obj->archive, Z_STR_P(zval_file), pathto, pathto_len, overwrite, &error)) { + case -1: + zend_throw_exception_ex(phar_ce_PharException, 0, "Extraction from phar \"%s\" failed: %s", + phar_obj->archive->fname, error); + efree(error); + return; + case 0: + zend_throw_exception_ex(phar_ce_PharException, 0, + "Phar Error: attempted to extract non-existent file or directory \"%s\" from phar \"%s\"", + ZSTR_VAL(Z_STR_P(zval_file)), phar_obj->archive->fname); return; - } - switch (extract_helper(phar_obj->archive, Z_STR_P(zval_file), pathto, pathto_len, overwrite, &error)) { - case -1: - zend_throw_exception_ex(phar_ce_PharException, 0, "Extraction from phar \"%s\" failed: %s", - phar_obj->archive->fname, error); - efree(error); - return; - case 0: - zend_throw_exception_ex(phar_ce_PharException, 0, - "Phar Error: attempted to extract non-existent file or directory \"%s\" from phar \"%s\"", - ZSTR_VAL(Z_STR_P(zval_file)), phar_obj->archive->fname); - return; - } } - } + } ZEND_HASH_FOREACH_END(); RETURN_TRUE; default: zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, -- 2.50.1