Skip to content

Commit

Permalink
Use Z_PARAM_CLASS in PDOStatement::fetchObject()
Browse files Browse the repository at this point in the history
Instead of implementing custom logic.
  • Loading branch information
nikic committed Aug 13, 2020
1 parent 1511bda commit 670036e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
3 changes: 3 additions & 0 deletions Zend/zend_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))) { \
Expand Down
26 changes: 8 additions & 18 deletions ext/pdo/pdo_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
}
}
/* }}} */

Expand Down
6 changes: 6 additions & 0 deletions ext/pdo_mysql/tests/pdo_mysql_stmt_fetchobject.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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!

0 comments on commit 670036e

Please sign in to comment.