From f5e6f9bd27d09b5fef6e0170c43a97a035067b7a Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Thu, 13 Aug 2020 14:59:39 +0200 Subject: [PATCH] Avoid fatal error on not found class in mysqli_fetch_object() Instead use C zpp modifier and throw TypeError. --- ext/mysqli/mysqli.c | 12 ++---------- ext/mysqli/tests/mysqli_fetch_object.phpt | 11 +++++++---- ext/mysqli/tests/mysqli_fetch_object_oo.phpt | 13 ++++++++----- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/ext/mysqli/mysqli.c b/ext/mysqli/mysqli.c index 505b4c05c6..595c39fead 100644 --- a/ext/mysqli/mysqli.c +++ b/ext/mysqli/mysqli.c @@ -1164,19 +1164,11 @@ void php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAMETERS, int override_flags zend_class_entry *ce = NULL; if (into_object) { - zend_string *class_name = NULL; - - if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|Sa", &mysql_result, mysqli_result_class_entry, &class_name, &ctor_params) == FAILURE) { + if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|Ca", &mysql_result, mysqli_result_class_entry, &ce, &ctor_params) == FAILURE) { RETURN_THROWS(); } - if (class_name == NULL) { + if (ce == NULL) { ce = zend_standard_class_def; - } else { - ce = zend_fetch_class(class_name, ZEND_FETCH_CLASS_AUTO); - } - if (!ce) { - php_error_docref(NULL, E_WARNING, "Could not find class '%s'", ZSTR_VAL(class_name)); - return; } if (UNEXPECTED(ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) { zend_throw_error(NULL, "Class %s cannot be instantiated", ZSTR_VAL(ce->name)); diff --git a/ext/mysqli/tests/mysqli_fetch_object.phpt b/ext/mysqli/tests/mysqli_fetch_object.phpt index c97738efb5..221ad6ab3d 100644 --- a/ext/mysqli/tests/mysqli_fetch_object.phpt +++ b/ext/mysqli/tests/mysqli_fetch_object.phpt @@ -128,8 +128,11 @@ require_once('skipifconnectfailure.inc'); $obj = mysqli_fetch_object($res, 'mysqli_fetch_object_private_constructor', array('a', 'b')); mysqli_free_result($res); - // Fatal error, script execution will end - var_dump(mysqli_fetch_object($res, 'this_class_does_not_exist')); + try { + var_dump(mysqli_fetch_object($res, 'this_class_does_not_exist')); + } catch (TypeError $e) { + echo $e->getMessage(), "\n"; + } mysqli_close($link); @@ -146,5 +149,5 @@ NULL NULL mysqli_result object is already closed [0] mysqli_fetch_object(): Argument #3 ($params) must be of type array, string given in %s on line %d - -Fatal error: Class "this_class_does_not_exist" not found in %s on line %d +mysqli_fetch_object(): Argument #2 ($class_name) must be a valid class name, this_class_does_not_exist given +done! diff --git a/ext/mysqli/tests/mysqli_fetch_object_oo.phpt b/ext/mysqli/tests/mysqli_fetch_object_oo.phpt index c3eff52ed0..d67c3fe6a8 100644 --- a/ext/mysqli/tests/mysqli_fetch_object_oo.phpt +++ b/ext/mysqli/tests/mysqli_fetch_object_oo.phpt @@ -118,8 +118,11 @@ require_once('skipifconnectfailure.inc'); echo $exception->getMessage() . "\n"; } - // Fatal error, script execution will end - var_dump($res->fetch_object('this_class_does_not_exist')); + try { + var_dump($res->fetch_object('this_class_does_not_exist')); + } catch (TypeError $exception) { + echo $exception->getMessage() . "\n"; + } $mysqli->close(); print "done!"; @@ -130,12 +133,12 @@ require_once('skipifconnectfailure.inc'); ?> --EXPECTF-- mysqli object is not fully initialized -[0] mysqli_result::fetch_object(): Argument #1 ($class_name) must be of type string, mysqli given in %s on line %d +[0] Object of class mysqli could not be converted to string in %s on line %d [0] mysqli_result::fetch_object() expects at most 2 parameters, 3 given in %s on line %d [0] mysqli_result::fetch_object(): Argument #2 ($params) must be of type array, null given in %s on line %d Exception: Too few arguments to function mysqli_fetch_object_construct::__construct(), 1 passed and exactly 2 expected NULL NULL mysqli_result object is already closed - -Fatal error: Class "this_class_does_not_exist" not found in %s on line %d +mysqli_result::fetch_object(): Argument #1 ($class_name) must be a valid class name, this_class_does_not_exist given +done! -- 2.40.0