]> granicus.if.org Git - php/commitdiff
Only set DIT type on successful initialization
authorNikita Popov <nikita.ppv@gmail.com>
Fri, 17 Jul 2020 14:42:21 +0000 (16:42 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Fri, 17 Jul 2020 14:42:56 +0000 (16:42 +0200)
ext/spl/spl_iterators.c
ext/spl/tests/AppendIterator_invalid_ctor.phpt [new file with mode: 0644]

index 047a72082dbd686398ba4ab4e8d2afdf6cb977b6..cee2b7b39f5427e66911353f2c24b0b6490393ab 100644 (file)
@@ -1290,7 +1290,6 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
                return NULL;
        }
 
-       intern->dit_type = dit_type;
        switch (dit_type) {
                case DIT_LimitIterator: {
                        intern->u.limit.offset = 0; /* start at beginning */
@@ -1364,6 +1363,7 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
                        if (zend_parse_parameters_none() == FAILURE) {
                                return NULL;
                        }
+                       intern->dit_type = DIT_AppendIterator;
                        zend_replace_error_handling(EH_THROW, spl_ce_InvalidArgumentException, &error_handling);
                        spl_instantiate(spl_ce_ArrayIterator, &intern->u.append.zarrayit);
                        zend_call_method_with_0_params(Z_OBJ(intern->u.append.zarrayit), spl_ce_ArrayIterator, &spl_ce_ArrayIterator->constructor, "__construct", NULL);
@@ -1385,8 +1385,6 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
                                zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Illegal mode " ZEND_LONG_FMT, mode);
                                return NULL;
                        }
-                       intern->u.regex.mode = mode;
-                       intern->u.regex.regex = zend_string_copy(regex);
 
                        zend_replace_error_handling(EH_THROW, spl_ce_InvalidArgumentException, &error_handling);
                        intern->u.regex.pce = pcre_get_compiled_regex_cache(regex);
@@ -1396,6 +1394,8 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
                                /* pcre_get_compiled_regex_cache has already sent error */
                                return NULL;
                        }
+                       intern->u.regex.mode = mode;
+                       intern->u.regex.regex = zend_string_copy(regex);
                        php_pcre_pce_incref(intern->u.regex.pce);
                        break;
                }
@@ -1420,6 +1420,7 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
                        break;
        }
 
+       intern->dit_type = dit_type;
        if (inc_refcount) {
                Z_ADDREF_P(zobject);
        }
diff --git a/ext/spl/tests/AppendIterator_invalid_ctor.phpt b/ext/spl/tests/AppendIterator_invalid_ctor.phpt
new file mode 100644 (file)
index 0000000..ae22270
--- /dev/null
@@ -0,0 +1,14 @@
+--TEST--
+AppendIterator::__construct() with invalid arguments
+--FILE--
+<?php
+
+new AppendIterator(null);
+
+?>
+--EXPECTF--
+Fatal error: Uncaught ArgumentCountError: AppendIterator::__construct() expects exactly 0 parameters, 1 given in %s:%d
+Stack trace:
+#0 %s(%d): AppendIterator->__construct(NULL)
+#1 {main}
+  thrown in %s on line %d