]> granicus.if.org Git - php/commitdiff
Avoid fatal error on not found class in mysqli_fetch_object()
authorNikita Popov <nikita.ppv@gmail.com>
Thu, 13 Aug 2020 12:59:39 +0000 (14:59 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Thu, 13 Aug 2020 14:13:02 +0000 (16:13 +0200)
Instead use C zpp modifier and throw TypeError.

ext/mysqli/mysqli.c
ext/mysqli/tests/mysqli_fetch_object.phpt
ext/mysqli/tests/mysqli_fetch_object_oo.phpt

index 505b4c05c668d0057cdfc2f1cda0640ffb52caae..595c39feadc838387045732603103d2f40a2aba1 100644 (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));
index c97738efb511f2f1a01682ce1507d941cf46c0c0..221ad6ab3d8d9d6238671ceea042b655adfdea3e 100644 (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!
index c3eff52ed022064f971381d2dee5e7fbe4f1ca4e..d67c3fe6a8a76075e36bdb757c626c4d2460eb8f 100644 (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!