]> granicus.if.org Git - php/commitdiff
Don't use quiet zpp in RecursiveIteratorIterator ctor
authorNikita Popov <nikita.ppv@gmail.com>
Mon, 23 Mar 2020 13:22:51 +0000 (14:22 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Mon, 23 Mar 2020 13:22:51 +0000 (14:22 +0100)
Don't be a special snowflake, generate a standard TypeError here.

ext/spl/spl_iterators.c
ext/spl/tests/iterator_062.phpt
ext/spl/tests/recursive_tree_iterator_002.phpt

index 8aa35081e1b642a1a852037ca0e71e0f618c7134..563eac89c7f75691408d89486cb6446c6872a609 100644 (file)
@@ -486,56 +486,55 @@ static void spl_recursive_it_it_construct(INTERNAL_FUNCTION_PARAMETERS, zend_cla
        zend_error_handling error_handling;
        zval caching_it, aggregate_retval;
 
-       zend_replace_error_handling(EH_THROW, spl_ce_InvalidArgumentException, &error_handling);
-
        switch (rit_type) {
                case RIT_RecursiveTreeIterator: {
                        zval caching_it_flags, *user_caching_it_flags = NULL;
                        mode = RIT_SELF_FIRST;
                        flags = RTIT_BYPASS_KEY;
 
-                       if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "o|lzl", &iterator, &flags, &user_caching_it_flags, &mode) == SUCCESS) {
-                               if (instanceof_function(Z_OBJCE_P(iterator), zend_ce_aggregate)) {
-                                       zend_call_method_with_0_params(Z_OBJ_P(iterator), Z_OBJCE_P(iterator), &Z_OBJCE_P(iterator)->iterator_funcs_ptr->zf_new_iterator, "getiterator", &aggregate_retval);
-                                       iterator = &aggregate_retval;
-                               } else {
-                                       Z_ADDREF_P(iterator);
-                               }
+                       if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|lzl", &iterator, &flags, &user_caching_it_flags, &mode) == FAILURE) {
+                               RETURN_THROWS();
+                       }
 
-                               if (user_caching_it_flags) {
-                                       ZVAL_COPY(&caching_it_flags, user_caching_it_flags);
-                               } else {
-                                       ZVAL_LONG(&caching_it_flags, CIT_CATCH_GET_CHILD);
-                               }
-                               spl_instantiate_arg_ex2(spl_ce_RecursiveCachingIterator, &caching_it, iterator, &caching_it_flags);
-                               zval_ptr_dtor(&caching_it_flags);
+                       zend_replace_error_handling(EH_THROW, spl_ce_InvalidArgumentException, &error_handling);
+                       if (instanceof_function(Z_OBJCE_P(iterator), zend_ce_aggregate)) {
+                               zend_call_method_with_0_params(Z_OBJ_P(iterator), Z_OBJCE_P(iterator), &Z_OBJCE_P(iterator)->iterator_funcs_ptr->zf_new_iterator, "getiterator", &aggregate_retval);
+                               iterator = &aggregate_retval;
+                       } else {
+                               Z_ADDREF_P(iterator);
+                       }
 
-                               zval_ptr_dtor(iterator);
-                               iterator = &caching_it;
+                       if (user_caching_it_flags) {
+                               ZVAL_COPY(&caching_it_flags, user_caching_it_flags);
                        } else {
-                               iterator = NULL;
+                               ZVAL_LONG(&caching_it_flags, CIT_CATCH_GET_CHILD);
                        }
+                       spl_instantiate_arg_ex2(spl_ce_RecursiveCachingIterator, &caching_it, iterator, &caching_it_flags);
+                       zval_ptr_dtor(&caching_it_flags);
+
+                       zval_ptr_dtor(iterator);
+                       iterator = &caching_it;
                        break;
                }
                case RIT_RecursiveIteratorIterator:
                default: {
                        mode = RIT_LEAVES_ONLY;
                        flags = 0;
+                       if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|ll", &iterator, &mode, &flags) == FAILURE) {
+                               RETURN_THROWS();
+                       }
 
-                       if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "o|ll", &iterator, &mode, &flags) == SUCCESS) {
-                               if (instanceof_function(Z_OBJCE_P(iterator), zend_ce_aggregate)) {
-                                       zend_call_method_with_0_params(Z_OBJ_P(iterator), Z_OBJCE_P(iterator), &Z_OBJCE_P(iterator)->iterator_funcs_ptr->zf_new_iterator, "getiterator", &aggregate_retval);
-                                       iterator = &aggregate_retval;
-                               } else {
-                                       Z_ADDREF_P(iterator);
-                               }
+                       zend_replace_error_handling(EH_THROW, spl_ce_InvalidArgumentException, &error_handling);
+                       if (instanceof_function(Z_OBJCE_P(iterator), zend_ce_aggregate)) {
+                               zend_call_method_with_0_params(Z_OBJ_P(iterator), Z_OBJCE_P(iterator), &Z_OBJCE_P(iterator)->iterator_funcs_ptr->zf_new_iterator, "getiterator", &aggregate_retval);
+                               iterator = &aggregate_retval;
                        } else {
-                               iterator = NULL;
+                               Z_ADDREF_P(iterator);
                        }
                        break;
                }
        }
-       if (!iterator || !instanceof_function(Z_OBJCE_P(iterator), spl_ce_RecursiveIterator)) {
+       if (!instanceof_function(Z_OBJCE_P(iterator), spl_ce_RecursiveIterator)) {
                if (iterator) {
                        zval_ptr_dtor(iterator);
                }
index 30011343d2ec6894de1218e03305a975eb078b49..a1440439c1abe338cc8c189229fd735c97c2ae23 100644 (file)
@@ -10,9 +10,9 @@ class myRecursiveIteratorIterator extends RecursiveIteratorIterator {
 
 try {
     $it = new myRecursiveIteratorIterator();
-} catch (InvalidArgumentException $e) {
-    echo 'InvalidArgumentException thrown';
+} catch (TypeError $e) {
+    echo $e->getMessage(), "\n";
 }
 ?>
 --EXPECT--
-InvalidArgumentException thrown
+RecursiveIteratorIterator::__construct() expects at least 1 parameter, 0 given
index e32b3bd00ef18a8e8770f709fe26859b1b494923..048dccaac3d9f601b21a9a25993ad25cacc81180 100644 (file)
@@ -6,9 +6,9 @@ error_reporting=E_ALL&~E_NOTICE
 <?php
 try {
     new RecursiveTreeIterator();
-} catch (InvalidArgumentException $e) {
-    echo "InvalidArgumentException thrown\n";
+} catch (TypeError $e) {
+    echo $e->getMessage(), "\n";
 }
 ?>
 --EXPECT--
-InvalidArgumentException thrown
+RecursiveTreeIterator::__construct() expects at least 1 parameter, 0 given