Skip to content

Commit f5e6f9b

Browse files
committed
Avoid fatal error on not found class in mysqli_fetch_object()
Instead use C zpp modifier and throw TypeError.
1 parent a10f887 commit f5e6f9b

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

ext/mysqli/mysqli.c

+2-10
Original file line numberDiff line numberDiff line change
@@ -1164,19 +1164,11 @@ void php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAMETERS, int override_flags
11641164
zend_class_entry *ce = NULL;
11651165

11661166
if (into_object) {
1167-
zend_string *class_name = NULL;
1168-
1169-
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|Sa", &mysql_result, mysqli_result_class_entry, &class_name, &ctor_params) == FAILURE) {
1167+
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|Ca", &mysql_result, mysqli_result_class_entry, &ce, &ctor_params) == FAILURE) {
11701168
RETURN_THROWS();
11711169
}
1172-
if (class_name == NULL) {
1170+
if (ce == NULL) {
11731171
ce = zend_standard_class_def;
1174-
} else {
1175-
ce = zend_fetch_class(class_name, ZEND_FETCH_CLASS_AUTO);
1176-
}
1177-
if (!ce) {
1178-
php_error_docref(NULL, E_WARNING, "Could not find class '%s'", ZSTR_VAL(class_name));
1179-
return;
11801172
}
11811173
if (UNEXPECTED(ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) {
11821174
zend_throw_error(NULL, "Class %s cannot be instantiated", ZSTR_VAL(ce->name));

ext/mysqli/tests/mysqli_fetch_object.phpt

+7-4
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,11 @@ require_once('skipifconnectfailure.inc');
128128
$obj = mysqli_fetch_object($res, 'mysqli_fetch_object_private_constructor', array('a', 'b'));
129129
mysqli_free_result($res);
130130

131-
// Fatal error, script execution will end
132-
var_dump(mysqli_fetch_object($res, 'this_class_does_not_exist'));
131+
try {
132+
var_dump(mysqli_fetch_object($res, 'this_class_does_not_exist'));
133+
} catch (TypeError $e) {
134+
echo $e->getMessage(), "\n";
135+
}
133136

134137

135138
mysqli_close($link);
@@ -146,5 +149,5 @@ NULL
146149
NULL
147150
mysqli_result object is already closed
148151
[0] mysqli_fetch_object(): Argument #3 ($params) must be of type array, string given in %s on line %d
149-
150-
Fatal error: Class "this_class_does_not_exist" not found in %s on line %d
152+
mysqli_fetch_object(): Argument #2 ($class_name) must be a valid class name, this_class_does_not_exist given
153+
done!

ext/mysqli/tests/mysqli_fetch_object_oo.phpt

+8-5
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,11 @@ require_once('skipifconnectfailure.inc');
118118
echo $exception->getMessage() . "\n";
119119
}
120120

121-
// Fatal error, script execution will end
122-
var_dump($res->fetch_object('this_class_does_not_exist'));
121+
try {
122+
var_dump($res->fetch_object('this_class_does_not_exist'));
123+
} catch (TypeError $exception) {
124+
echo $exception->getMessage() . "\n";
125+
}
123126

124127
$mysqli->close();
125128
print "done!";
@@ -130,12 +133,12 @@ require_once('skipifconnectfailure.inc');
130133
?>
131134
--EXPECTF--
132135
mysqli object is not fully initialized
133-
[0] mysqli_result::fetch_object(): Argument #1 ($class_name) must be of type string, mysqli given in %s on line %d
136+
[0] Object of class mysqli could not be converted to string in %s on line %d
134137
[0] mysqli_result::fetch_object() expects at most 2 parameters, 3 given in %s on line %d
135138
[0] mysqli_result::fetch_object(): Argument #2 ($params) must be of type array, null given in %s on line %d
136139
Exception: Too few arguments to function mysqli_fetch_object_construct::__construct(), 1 passed and exactly 2 expected
137140
NULL
138141
NULL
139142
mysqli_result object is already closed
140-
141-
Fatal error: Class "this_class_does_not_exist" not found in %s on line %d
143+
mysqli_result::fetch_object(): Argument #1 ($class_name) must be a valid class name, this_class_does_not_exist given
144+
done!

0 commit comments

Comments
 (0)