Skip to content

Commit 068c8db

Browse files
kocsismatenikic
andcommitted
Fix the signature of PDOStatement::fetchObject()
The ?array $ctorArgs = null parameter is changed to array $constructorArgs = [], and an additional memory leak revealed by the new test case is fixed. Closes GH-6937 Co-Authored-By: Nikita Popov <nikita.ppv@gmail.com>
1 parent 9e51b48 commit 068c8db

File tree

4 files changed

+108
-11
lines changed

4 files changed

+108
-11
lines changed

ext/pdo/pdo_stmt.c

+6-8
Original file line numberDiff line numberDiff line change
@@ -914,8 +914,8 @@ static bool do_fetch(pdo_stmt_t *stmt, zval *return_value, enum pdo_fetch_type h
914914
return 0;
915915
}
916916
if (!stmt->fetch.cls.fci.size) {
917-
if (!do_fetch_class_prepare(stmt))
918-
{
917+
if (!do_fetch_class_prepare(stmt)) {
918+
zval_ptr_dtor(return_value);
919919
return 0;
920920
}
921921
}
@@ -1266,12 +1266,10 @@ PHP_METHOD(PDOStatement, fetchObject)
12661266

12671267
do_fetch_opt_finish(stmt, 0);
12681268

1269-
if (ctor_args) {
1270-
if (Z_TYPE_P(ctor_args) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(ctor_args))) {
1271-
ZVAL_ARR(&stmt->fetch.cls.ctor_args, zend_array_dup(Z_ARRVAL_P(ctor_args)));
1272-
} else {
1273-
ZVAL_UNDEF(&stmt->fetch.cls.ctor_args);
1274-
}
1269+
if (ctor_args && zend_hash_num_elements(Z_ARRVAL_P(ctor_args))) {
1270+
ZVAL_ARR(&stmt->fetch.cls.ctor_args, zend_array_dup(Z_ARRVAL_P(ctor_args)));
1271+
} else {
1272+
ZVAL_UNDEF(&stmt->fetch.cls.ctor_args);
12751273
}
12761274
if (ce) {
12771275
stmt->fetch.cls.ce = ce;

ext/pdo/pdo_stmt.stub.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function fetchAll(int $mode = PDO::FETCH_DEFAULT, mixed ...$args) {}
4141
public function fetchColumn(int $column = 0) {}
4242

4343
/** @return object|false */
44-
public function fetchObject(?string $class = "stdClass", ?array $ctorArgs = null) {}
44+
public function fetchObject(?string $class = "stdClass", array $constructorArgs = []) {}
4545

4646
/** @return mixed */
4747
public function getAttribute(int $name) {}

ext/pdo/pdo_stmt_arginfo.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 80860ee99befe1258900120f0c226688f6606c6f */
2+
* Stub hash: 2717622c27bdc6aac5ec83609c11dec6cbc9f5d7 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDOStatement_bindColumn, 0, 0, 2)
55
ZEND_ARG_TYPE_MASK(0, column, MAY_BE_STRING|MAY_BE_LONG, NULL)
@@ -55,7 +55,7 @@ ZEND_END_ARG_INFO()
5555

5656
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDOStatement_fetchObject, 0, 0, 0)
5757
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, class, IS_STRING, 1, "\"stdClass\"")
58-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, ctorArgs, IS_ARRAY, 1, "null")
58+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, constructorArgs, IS_ARRAY, 0, "[]")
5959
ZEND_END_ARG_INFO()
6060

6161
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDOStatement_getAttribute, 0, 0, 1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
--TEST--
2+
MySQL PDO: PDOStatement->fetchObject() with $constructorArgs
3+
--SKIPIF--
4+
<?php
5+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
6+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
7+
MySQLPDOTest::skip();
8+
$db = MySQLPDOTest::factory();
9+
10+
try {
11+
$query = "SELECT '', NULL, \"\" FROM DUAL";
12+
$stmt = $db->prepare($query);
13+
$ok = @$stmt->execute();
14+
} catch (PDOException $e) {
15+
die("skip: Test cannot be run with SQL mode ANSI");
16+
}
17+
if (!$ok)
18+
die("skip: Test cannot be run with SQL mode ANSI");
19+
?>
20+
--FILE--
21+
<?php
22+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
23+
/** @var PDO $db */
24+
$db = MySQLPDOTest::factory();
25+
MySQLPDOTest::createTestTable($db);
26+
27+
$query = "SELECT id FROM test ORDER BY id ASC LIMIT 1";
28+
$stmt = $db->prepare($query);
29+
30+
class Foo {
31+
public int $a;
32+
public int $id;
33+
34+
public function __construct($a) {
35+
$this->a = $a;
36+
}
37+
}
38+
39+
class Bar {
40+
public int $id;
41+
}
42+
43+
$stmt->execute();
44+
try {
45+
$obj = $stmt->fetchObject(Foo::class);
46+
} catch (ArgumentCountError $exception) {
47+
echo $exception->getMessage() . "\n";
48+
}
49+
50+
$stmt->execute();
51+
try {
52+
$obj = $stmt->fetchObject(Foo::class, []);
53+
} catch (ArgumentCountError $exception) {
54+
echo $exception->getMessage() . "\n";
55+
}
56+
57+
$stmt->execute();
58+
$obj = $stmt->fetchObject(Foo::class, ["a" => 123]);
59+
var_dump($obj);
60+
61+
$stmt->execute();
62+
$obj = $stmt->fetchObject(Bar::class);
63+
var_dump($obj);
64+
65+
$stmt->execute();
66+
$obj = $stmt->fetchObject(Bar::class, []);
67+
var_dump($obj);
68+
69+
try {
70+
$stmt->execute();
71+
$obj = $stmt->fetchObject(Bar::class, ["a" => 123]);
72+
} catch (Error $exception) {
73+
echo $exception->getMessage() . "\n";
74+
}
75+
76+
?>
77+
--CLEAN--
78+
<?php
79+
require __DIR__ . '/mysql_pdo_test.inc';
80+
MySQLPDOTest::dropTestTable();
81+
?>
82+
--EXPECTF--
83+
Too few arguments to function Foo::__construct(), 0 passed and exactly 1 expected
84+
Too few arguments to function Foo::__construct(), 0 passed and exactly 1 expected
85+
object(Foo)#%d (2) {
86+
["a"]=>
87+
int(123)
88+
["id"]=>
89+
int(1)
90+
}
91+
object(Bar)#%d (1) {
92+
["id"]=>
93+
int(1)
94+
}
95+
object(Bar)#%d (1) {
96+
["id"]=>
97+
int(1)
98+
}
99+
User-supplied statement does not accept constructor arguments

0 commit comments

Comments
 (0)