]> granicus.if.org Git - php/commitdiff
Don't loop over indexes in Phar::extractTo()
authorNikita Popov <nikita.ppv@gmail.com>
Sun, 28 Jan 2018 21:04:24 +0000 (22:04 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Sun, 28 Jan 2018 21:05:44 +0000 (22:05 +0100)
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

index 7bba6195747269567fdc9ba4f6368caffeb48c06..6234505995c824716c55d6547bae04d0f38d550f 100644 (file)
@@ -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,