From 670036e2a9b4520b8cb14be41625664f96bae4b4 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Thu, 13 Aug 2020 16:20:29 +0200 Subject: [PATCH] Use Z_PARAM_CLASS in PDOStatement::fetchObject() Instead of implementing custom logic. --- Zend/zend_API.h | 3 +++ ext/pdo/pdo_stmt.c | 26 ++++++------------- .../tests/pdo_mysql_stmt_fetchobject.phpt | 6 +++++ 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/Zend/zend_API.h b/Zend/zend_API.h index b6f5944078..adf84affd2 100644 --- a/Zend/zend_API.h +++ b/Zend/zend_API.h @@ -1405,6 +1405,9 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_value_error(uint32_t arg_num #define Z_PARAM_CLASS(dest) \ Z_PARAM_CLASS_EX(dest, 0, 0) +#define Z_PARAM_CLASS_OR_NULL(dest) \ + Z_PARAM_CLASS_EX(dest, 1, 0) + #define Z_PARAM_CLASS_NAME_OR_OBJ_EX(dest, allow_null) \ Z_PARAM_PROLOGUE(0, 0); \ if (UNEXPECTED(!zend_parse_arg_class_name_or_obj(_arg, &dest, allow_null))) { \ diff --git a/ext/pdo/pdo_stmt.c b/ext/pdo/pdo_stmt.c index 88e8136134..96f7574638 100644 --- a/ext/pdo/pdo_stmt.c +++ b/ext/pdo/pdo_stmt.c @@ -1191,14 +1191,14 @@ PHP_METHOD(PDOStatement, fetchObject) zend_long how = PDO_FETCH_CLASS; zend_long ori = PDO_FETCH_ORI_NEXT; zend_long off = 0; - zend_string *class_name = NULL; + zend_class_entry *ce = NULL; zend_class_entry *old_ce; zval old_ctor_args, *ctor_args = NULL; - int error = 0, old_arg_count; + int old_arg_count; ZEND_PARSE_PARAMETERS_START(0, 2) Z_PARAM_OPTIONAL - Z_PARAM_STR_EX(class_name, 1, 0) + Z_PARAM_CLASS_OR_NULL(ce) Z_PARAM_ARRAY(ctor_args) ZEND_PARSE_PARAMETERS_END(); @@ -1222,31 +1222,21 @@ PHP_METHOD(PDOStatement, fetchObject) ZVAL_UNDEF(&stmt->fetch.cls.ctor_args); } } - if (class_name && !error) { - stmt->fetch.cls.ce = zend_fetch_class(class_name, ZEND_FETCH_CLASS_AUTO); - - if (!stmt->fetch.cls.ce) { - pdo_raise_impl_error(stmt->dbh, stmt, "HY000", "Could not find user-supplied class"); - error = 1; - } - } else if (!error) { + if (ce) { + stmt->fetch.cls.ce = ce; + } else { stmt->fetch.cls.ce = zend_standard_class_def; } - if (!error && !do_fetch(stmt, TRUE, return_value, how, ori, off, 0)) { - error = 1; - } - if (error) { + if (!do_fetch(stmt, TRUE, return_value, how, ori, off, 0)) { PDO_HANDLE_STMT_ERR(); + RETVAL_FALSE; } do_fetch_opt_finish(stmt, 1); stmt->fetch.cls.ce = old_ce; ZVAL_COPY_VALUE(&stmt->fetch.cls.ctor_args, &old_ctor_args); stmt->fetch.cls.fci.param_count = old_arg_count; - if (error) { - RETURN_FALSE; - } } /* }}} */ diff --git a/ext/pdo_mysql/tests/pdo_mysql_stmt_fetchobject.phpt b/ext/pdo_mysql/tests/pdo_mysql_stmt_fetchobject.phpt index 9760682358..31ee2a3e07 100644 --- a/ext/pdo_mysql/tests/pdo_mysql_stmt_fetchobject.phpt +++ b/ext/pdo_mysql/tests/pdo_mysql_stmt_fetchobject.phpt @@ -67,6 +67,11 @@ try { var_dump($rows[$rowno - 1]); + try { + $stmt->fetchObject('class_does_not_exist'); + } catch (TypeError $e) { + echo $e->getMessage(), "\n"; + } } catch (PDOException $e) { // we should never get here, we use warnings, but never trust a system... printf("[001] %s, [%s} %s\n", @@ -106,4 +111,5 @@ object(myclass)#%d (4) { ["null"]=> NULL } +PDOStatement::fetchObject(): Argument #1 ($class_name) must be a valid class name, class_does_not_exist given done! -- 2.50.1