Avoid fatal error on not found class in mysqli_fetch_object()

Instead use C zpp modifier and throw TypeError.
This commit is contained in:
Nikita Popov 2020-08-13 14:59:39 +02:00
parent a10f8876ef
commit f5e6f9bd27
3 changed files with 17 additions and 19 deletions

View file

@ -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));

View file

@ -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!

View file

@ -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!