Skip to content

Fix the default value of $ctorArgs param in PDOStatement::fetchObject() #6937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions ext/pdo/pdo_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ PHP_METHOD(PDOStatement, fetchObject)
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_CLASS_OR_NULL(ce)
Z_PARAM_ARRAY(ctor_args)
Z_PARAM_ARRAY_OR_NULL(ctor_args)
ZEND_PARSE_PARAMETERS_END();

PHP_STMT_GET_OBJ;
Expand All @@ -1266,12 +1266,10 @@ PHP_METHOD(PDOStatement, fetchObject)

do_fetch_opt_finish(stmt, 0);

if (ctor_args) {
if (Z_TYPE_P(ctor_args) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(ctor_args))) {
ZVAL_ARR(&stmt->fetch.cls.ctor_args, zend_array_dup(Z_ARRVAL_P(ctor_args)));
} else {
ZVAL_UNDEF(&stmt->fetch.cls.ctor_args);
}
if (ctor_args && zend_hash_num_elements(Z_ARRVAL_P(ctor_args))) {
ZVAL_ARR(&stmt->fetch.cls.ctor_args, zend_array_dup(Z_ARRVAL_P(ctor_args)));
} else {
ZVAL_UNDEF(&stmt->fetch.cls.ctor_args);
}
if (ce) {
stmt->fetch.cls.ce = ce;
Expand Down
115 changes: 115 additions & 0 deletions ext/pdo_mysql/tests/pdo_mysql_stmt_fetchobject_ctor_args.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
--TEST--
MySQL PDO: PDOStatement->fetchObject() with $ctorArgs
--SKIPIF--
<?php
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
MySQLPDOTest::skip();
$db = MySQLPDOTest::factory();

try {
$query = "SELECT '', NULL, \"\" FROM DUAL";
$stmt = $db->prepare($query);
$ok = @$stmt->execute();
} catch (PDOException $e) {
die("skip: Test cannot be run with SQL mode ANSI");
}
if (!$ok)
die("skip: Test cannot be run with SQL mode ANSI");
?>
--FILE--
<?php
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
/** @var PDO $db */
$db = MySQLPDOTest::factory();
MySQLPDOTest::createTestTable($db);

$query = "SELECT id FROM test ORDER BY id ASC LIMIT 1";
$stmt = $db->prepare($query);

class Foo {
public int $a;
public int $id;

public function __construct($a) {
$this->a = $a;
}
}

class Bar {
public int $id;
}

$stmt->execute();
try {
$obj = $stmt->fetchObject(Foo::class);
} catch (ArgumentCountError $exception) {
echo $exception->getMessage() . "\n";
}

$stmt->execute();
try {
$obj = $stmt->fetchObject(Foo::class, null);
} catch (ArgumentCountError $exception) {
echo $exception->getMessage() . "\n";
}

$stmt->execute();
try {
$obj = $stmt->fetchObject(Foo::class, []);
} catch (ArgumentCountError $exception) {
echo $exception->getMessage() . "\n";
}

$stmt->execute();
$obj = $stmt->fetchObject(Foo::class, ["a" => 123]);
var_dump($obj);

$stmt->execute();
$obj = $stmt->fetchObject(Bar::class);
var_dump($obj);

$stmt->execute();
$obj = $stmt->fetchObject(Bar::class, null);
var_dump($obj);

$stmt->execute();
$obj = $stmt->fetchObject(Bar::class, []);
var_dump($obj);

try {
$stmt->execute();
$obj = $stmt->fetchObject(Bar::class, ["a" => 123]);
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

?>
--CLEAN--
<?php
require __DIR__ . '/mysql_pdo_test.inc';
MySQLPDOTest::dropTestTable();
?>
--EXPECTF--
Too few arguments to function Foo::__construct(), 0 passed and exactly 1 expected
Too few arguments to function Foo::__construct(), 0 passed and exactly 1 expected
Too few arguments to function Foo::__construct(), 0 passed and exactly 1 expected
object(Foo)#%d (2) {
["a"]=>
int(123)
["id"]=>
int(1)
}
object(Bar)#%d (1) {
["id"]=>
int(1)
}
object(Bar)#%d (1) {
["id"]=>
int(1)
}
object(Bar)#%d (1) {
["id"]=>
int(1)
}
User-supplied statement does not accept constructor arguments