From: Nikita Popov Date: Mon, 19 Feb 2018 20:57:07 +0000 (+0100) Subject: Convert iterator by reference errors to exceptions X-Git-Tag: php-7.3.0alpha1~333 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ede663f38145e910703f47aa9c51f7947cc10540;p=php Convert iterator by reference errors to exceptions I'm using RuntimeException in SPL, because other SPL classes that throw this error used it. Error is used for everything else, because that's what core does. --- diff --git a/ext/com_dotnet/com_iterator.c b/ext/com_dotnet/com_iterator.c index 2bf1735bef..c60bff0ec8 100644 --- a/ext/com_dotnet/com_iterator.c +++ b/ext/com_dotnet/com_iterator.c @@ -146,7 +146,8 @@ zend_object_iterator *php_com_iter_get(zend_class_entry *ce, zval *object, int b zval ptr; if (by_ref) { - zend_error(E_ERROR, "An iterator cannot be used with foreach by reference"); + zend_throw_error(NULL, "An iterator cannot be used with foreach by reference"); + return NULL; } obj = CDNO_FETCH(object); diff --git a/ext/com_dotnet/com_saproxy.c b/ext/com_dotnet/com_saproxy.c index 01acd58274..81bbb63ca8 100644 --- a/ext/com_dotnet/com_saproxy.c +++ b/ext/com_dotnet/com_saproxy.c @@ -542,7 +542,8 @@ zend_object_iterator *php_com_saproxy_iter_get(zend_class_entry *ce, zval *objec int i; if (by_ref) { - zend_error(E_ERROR, "An iterator cannot be used with foreach by reference"); + zend_throw_error(NULL, "An iterator cannot be used with foreach by reference"); + return NULL; } I = ecalloc(1, sizeof(*I)); diff --git a/ext/date/php_date.c b/ext/date/php_date.c index 6bb1c7d5cc..bf2997d946 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -2007,7 +2007,8 @@ zend_object_iterator *date_object_period_get_iterator(zend_class_entry *ce, zval date_period_it *iterator = emalloc(sizeof(date_period_it)); if (by_ref) { - zend_error(E_ERROR, "An iterator cannot be used with foreach by reference"); + zend_throw_error(NULL, "An iterator cannot be used with foreach by reference"); + return NULL; } zend_iterator_init((zend_object_iterator*)iterator); diff --git a/ext/dom/dom_iterators.c b/ext/dom/dom_iterators.c index 715d900339..4930653274 100644 --- a/ext/dom/dom_iterators.c +++ b/ext/dom/dom_iterators.c @@ -264,7 +264,8 @@ zend_object_iterator *php_dom_get_iterator(zend_class_entry *ce, zval *object, i php_dom_iterator *iterator; if (by_ref) { - zend_error(E_ERROR, "An iterator cannot be used with foreach by reference"); + zend_throw_error(NULL, "An iterator cannot be used with foreach by reference"); + return NULL; } iterator = emalloc(sizeof(php_dom_iterator)); zend_iterator_init(&iterator->intern); diff --git a/ext/pdo/pdo_stmt.c b/ext/pdo/pdo_stmt.c index 31987ff2e5..423f0e6260 100644 --- a/ext/pdo/pdo_stmt.c +++ b/ext/pdo/pdo_stmt.c @@ -2469,7 +2469,8 @@ zend_object_iterator *pdo_stmt_iter_get(zend_class_entry *ce, zval *object, int struct php_pdo_iterator *I; if (by_ref) { - zend_error(E_ERROR, "An iterator cannot be used with foreach by reference"); + zend_throw_error(NULL, "An iterator cannot be used with foreach by reference"); + return NULL; } I = ecalloc(1, sizeof(struct php_pdo_iterator)); diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c index a497a360f8..7a5767871d 100644 --- a/ext/simplexml/simplexml.c +++ b/ext/simplexml/simplexml.c @@ -2425,7 +2425,8 @@ zend_object_iterator *php_sxe_get_iterator(zend_class_entry *ce, zval *object, i php_sxe_iterator *iterator; if (by_ref) { - zend_error(E_ERROR, "An iterator cannot be used with foreach by reference"); + zend_throw_error(NULL, "An iterator cannot be used with foreach by reference"); + return NULL; } iterator = emalloc(sizeof(php_sxe_iterator)); zend_iterator_init(&iterator->intern); diff --git a/ext/spl/spl_array.c b/ext/spl/spl_array.c index 0e610ee125..0093831dde 100644 --- a/ext/spl/spl_array.c +++ b/ext/spl/spl_array.c @@ -1175,7 +1175,8 @@ zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object, spl_array_object *array_object = Z_SPLARRAY_P(object); if (by_ref && (array_object->ar_flags & SPL_ARRAY_OVERLOADED_CURRENT)) { - zend_error(E_ERROR, "An iterator cannot be used with foreach by reference"); + zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0); + return NULL; } iterator = emalloc(sizeof(zend_user_iterator)); diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c index 6f0c19039a..5c144b0f98 100644 --- a/ext/spl/spl_directory.c +++ b/ext/spl/spl_directory.c @@ -1639,7 +1639,8 @@ zend_object_iterator *spl_filesystem_dir_get_iterator(zend_class_entry *ce, zval spl_filesystem_object *dir_object; if (by_ref) { - zend_error(E_ERROR, "An iterator cannot be used with foreach by reference"); + zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0); + return NULL; } dir_object = Z_SPLFILESYSTEM_P(object); iterator = spl_filesystem_object_to_iterator(dir_object); @@ -1838,7 +1839,8 @@ zend_object_iterator *spl_filesystem_tree_get_iterator(zend_class_entry *ce, zva spl_filesystem_object *dir_object; if (by_ref) { - zend_error(E_ERROR, "An iterator cannot be used with foreach by reference"); + zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0); + return NULL; } dir_object = Z_SPLFILESYSTEM_P(object); iterator = spl_filesystem_object_to_iterator(dir_object); diff --git a/ext/spl/spl_iterators.c b/ext/spl/spl_iterators.c index ee621243e0..fdb8c4b401 100644 --- a/ext/spl/spl_iterators.c +++ b/ext/spl/spl_iterators.c @@ -449,7 +449,8 @@ static zend_object_iterator *spl_recursive_it_get_iterator(zend_class_entry *ce, spl_recursive_it_object *object; if (by_ref) { - zend_error(E_ERROR, "An iterator cannot be used with foreach by reference"); + zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0); + return NULL; } iterator = emalloc(sizeof(spl_recursive_it_iterator)); object = Z_SPLRECURSIVE_IT_P(zobject); diff --git a/ext/spl/tests/DirectoryIterator_by_reference.phpt b/ext/spl/tests/DirectoryIterator_by_reference.phpt index 06127ec37b..3c5c5ac2b2 100644 --- a/ext/spl/tests/DirectoryIterator_by_reference.phpt +++ b/ext/spl/tests/DirectoryIterator_by_reference.phpt @@ -11,4 +11,7 @@ foreach( $it as &$file ) { } ?> --EXPECTF-- -Fatal error: An iterator cannot be used with foreach by reference in %s on line %d +Fatal error: Uncaught RuntimeException: An iterator cannot be used with foreach by reference in %s:%d +Stack trace: +#0 {main} + thrown in %s on line %d diff --git a/ext/spl/tests/array_019.phpt b/ext/spl/tests/array_019.phpt index d128f4de7b..30cc9972af 100644 --- a/ext/spl/tests/array_019.phpt +++ b/ext/spl/tests/array_019.phpt @@ -27,4 +27,7 @@ int(2) int(3) int(4) -Fatal error: An iterator cannot be used with foreach by reference in %sarray_019.php on line %d +Fatal error: Uncaught RuntimeException: An iterator cannot be used with foreach by reference in %s:%d +Stack trace: +#0 {main} + thrown in %s on line %d diff --git a/ext/spl/tests/iterator_069.phpt b/ext/spl/tests/iterator_069.phpt index 1ee44785b3..101fe91681 100644 --- a/ext/spl/tests/iterator_069.phpt +++ b/ext/spl/tests/iterator_069.phpt @@ -14,4 +14,7 @@ foreach ($recItIt as &$val) echo "$val\n"; ?> --EXPECTF-- -Fatal error: An iterator cannot be used with foreach by reference in %s on line %d +Fatal error: Uncaught RuntimeException: An iterator cannot be used with foreach by reference in %s:%d +Stack trace: +#0 {main} + thrown in %s on line %d