From 83ead5567fd5a801609083595751518f4d03e140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Tue, 4 May 2021 13:35:32 +0200 Subject: [PATCH 1/4] Fix the default value of $ctorArgs param in PDOStatement::fetchObject() --- ext/pdo/pdo_stmt.stub.php | 2 +- ext/pdo/pdo_stmt_arginfo.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/pdo/pdo_stmt.stub.php b/ext/pdo/pdo_stmt.stub.php index e2c25b6a7a028..55766f0c00486 100644 --- a/ext/pdo/pdo_stmt.stub.php +++ b/ext/pdo/pdo_stmt.stub.php @@ -41,7 +41,7 @@ public function fetchAll(int $mode = PDO::FETCH_DEFAULT, mixed ...$args) {} public function fetchColumn(int $column = 0) {} /** @return object|false */ - public function fetchObject(?string $class = "stdClass", ?array $ctorArgs = null) {} + public function fetchObject(?string $class = "stdClass", array $ctorArgs = UNKNOWN) {} /** @return mixed */ public function getAttribute(int $name) {} diff --git a/ext/pdo/pdo_stmt_arginfo.h b/ext/pdo/pdo_stmt_arginfo.h index 7748ac54d62b7..e73e72843ace5 100644 --- a/ext/pdo/pdo_stmt_arginfo.h +++ b/ext/pdo/pdo_stmt_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 80860ee99befe1258900120f0c226688f6606c6f */ + * Stub hash: 9b654220433724fc075d2f1afd01ae4c05a70731 */ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDOStatement_bindColumn, 0, 0, 2) ZEND_ARG_TYPE_MASK(0, column, MAY_BE_STRING|MAY_BE_LONG, NULL) @@ -55,7 +55,7 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDOStatement_fetchObject, 0, 0, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, class, IS_STRING, 1, "\"stdClass\"") - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, ctorArgs, IS_ARRAY, 1, "null") + ZEND_ARG_TYPE_INFO(0, ctorArgs, IS_ARRAY, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDOStatement_getAttribute, 0, 0, 1) From 9db98b59f0c7ee285f6a800d0c883d06e833e842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Tue, 4 May 2021 16:53:41 +0200 Subject: [PATCH 2/4] Fix ZPP for $ctorArgs, and add test --- ext/pdo/pdo_stmt.c | 12 +- ext/pdo/pdo_stmt.stub.php | 2 +- ext/pdo/pdo_stmt_arginfo.h | 4 +- .../pdo_mysql_stmt_fetchobject_ctor_args.phpt | 115 ++++++++++++++++++ 4 files changed, 123 insertions(+), 10 deletions(-) create mode 100644 ext/pdo_mysql/tests/pdo_mysql_stmt_fetchobject_ctor_args.phpt diff --git a/ext/pdo/pdo_stmt.c b/ext/pdo/pdo_stmt.c index 80b50605b9230..4e45f13290da8 100644 --- a/ext/pdo/pdo_stmt.c +++ b/ext/pdo/pdo_stmt.c @@ -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; @@ -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; diff --git a/ext/pdo/pdo_stmt.stub.php b/ext/pdo/pdo_stmt.stub.php index 55766f0c00486..e2c25b6a7a028 100644 --- a/ext/pdo/pdo_stmt.stub.php +++ b/ext/pdo/pdo_stmt.stub.php @@ -41,7 +41,7 @@ public function fetchAll(int $mode = PDO::FETCH_DEFAULT, mixed ...$args) {} public function fetchColumn(int $column = 0) {} /** @return object|false */ - public function fetchObject(?string $class = "stdClass", array $ctorArgs = UNKNOWN) {} + public function fetchObject(?string $class = "stdClass", ?array $ctorArgs = null) {} /** @return mixed */ public function getAttribute(int $name) {} diff --git a/ext/pdo/pdo_stmt_arginfo.h b/ext/pdo/pdo_stmt_arginfo.h index e73e72843ace5..7748ac54d62b7 100644 --- a/ext/pdo/pdo_stmt_arginfo.h +++ b/ext/pdo/pdo_stmt_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 9b654220433724fc075d2f1afd01ae4c05a70731 */ + * Stub hash: 80860ee99befe1258900120f0c226688f6606c6f */ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDOStatement_bindColumn, 0, 0, 2) ZEND_ARG_TYPE_MASK(0, column, MAY_BE_STRING|MAY_BE_LONG, NULL) @@ -55,7 +55,7 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDOStatement_fetchObject, 0, 0, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, class, IS_STRING, 1, "\"stdClass\"") - ZEND_ARG_TYPE_INFO(0, ctorArgs, IS_ARRAY, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, ctorArgs, IS_ARRAY, 1, "null") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDOStatement_getAttribute, 0, 0, 1) diff --git a/ext/pdo_mysql/tests/pdo_mysql_stmt_fetchobject_ctor_args.phpt b/ext/pdo_mysql/tests/pdo_mysql_stmt_fetchobject_ctor_args.phpt new file mode 100644 index 0000000000000..66ec1d3ec3b95 --- /dev/null +++ b/ext/pdo_mysql/tests/pdo_mysql_stmt_fetchobject_ctor_args.phpt @@ -0,0 +1,115 @@ +--TEST-- +MySQL PDO: PDOStatement->fetchObject() with $ctorArgs +--SKIPIF-- +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-- +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-- + +--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 From 71101a978b5ee388888f3787bbe3eed0259ec962 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 5 May 2021 14:49:39 +0200 Subject: [PATCH 3/4] Fix memory leak --- ext/pdo/pdo_stmt.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/pdo/pdo_stmt.c b/ext/pdo/pdo_stmt.c index 4e45f13290da8..6b21cbd58a23a 100644 --- a/ext/pdo/pdo_stmt.c +++ b/ext/pdo/pdo_stmt.c @@ -914,8 +914,8 @@ static bool do_fetch(pdo_stmt_t *stmt, zval *return_value, enum pdo_fetch_type h return 0; } if (!stmt->fetch.cls.fci.size) { - if (!do_fetch_class_prepare(stmt)) - { + if (!do_fetch_class_prepare(stmt)) { + zval_ptr_dtor(return_value); return 0; } } From 35fc76068815e8c99f372fc755009234d8efcec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Wed, 5 May 2021 15:36:57 +0200 Subject: [PATCH 4/4] $ctorArgs shouldn't be nullable (and should be called $constructorArgs) --- ext/pdo/pdo_stmt.c | 2 +- ext/pdo/pdo_stmt.stub.php | 2 +- ext/pdo/pdo_stmt_arginfo.h | 4 ++-- .../pdo_mysql_stmt_fetchobject_ctor_args.phpt | 16 ---------------- 4 files changed, 4 insertions(+), 20 deletions(-) diff --git a/ext/pdo/pdo_stmt.c b/ext/pdo/pdo_stmt.c index 6b21cbd58a23a..ad7df5c241e37 100644 --- a/ext/pdo/pdo_stmt.c +++ b/ext/pdo/pdo_stmt.c @@ -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_OR_NULL(ctor_args) + Z_PARAM_ARRAY(ctor_args) ZEND_PARSE_PARAMETERS_END(); PHP_STMT_GET_OBJ; diff --git a/ext/pdo/pdo_stmt.stub.php b/ext/pdo/pdo_stmt.stub.php index e2c25b6a7a028..c80a3f460963f 100644 --- a/ext/pdo/pdo_stmt.stub.php +++ b/ext/pdo/pdo_stmt.stub.php @@ -41,7 +41,7 @@ public function fetchAll(int $mode = PDO::FETCH_DEFAULT, mixed ...$args) {} public function fetchColumn(int $column = 0) {} /** @return object|false */ - public function fetchObject(?string $class = "stdClass", ?array $ctorArgs = null) {} + public function fetchObject(?string $class = "stdClass", array $constructorArgs = []) {} /** @return mixed */ public function getAttribute(int $name) {} diff --git a/ext/pdo/pdo_stmt_arginfo.h b/ext/pdo/pdo_stmt_arginfo.h index 7748ac54d62b7..b7c052237e53a 100644 --- a/ext/pdo/pdo_stmt_arginfo.h +++ b/ext/pdo/pdo_stmt_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 80860ee99befe1258900120f0c226688f6606c6f */ + * Stub hash: 2717622c27bdc6aac5ec83609c11dec6cbc9f5d7 */ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDOStatement_bindColumn, 0, 0, 2) ZEND_ARG_TYPE_MASK(0, column, MAY_BE_STRING|MAY_BE_LONG, NULL) @@ -55,7 +55,7 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDOStatement_fetchObject, 0, 0, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, class, IS_STRING, 1, "\"stdClass\"") - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, ctorArgs, IS_ARRAY, 1, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, constructorArgs, IS_ARRAY, 0, "[]") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDOStatement_getAttribute, 0, 0, 1) diff --git a/ext/pdo_mysql/tests/pdo_mysql_stmt_fetchobject_ctor_args.phpt b/ext/pdo_mysql/tests/pdo_mysql_stmt_fetchobject_ctor_args.phpt index 66ec1d3ec3b95..7f99a2f94f5d4 100644 --- a/ext/pdo_mysql/tests/pdo_mysql_stmt_fetchobject_ctor_args.phpt +++ b/ext/pdo_mysql/tests/pdo_mysql_stmt_fetchobject_ctor_args.phpt @@ -47,13 +47,6 @@ try { 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, []); @@ -69,10 +62,6 @@ $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); @@ -93,7 +82,6 @@ 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) @@ -108,8 +96,4 @@ object(Bar)#%d (1) { ["id"]=> int(1) } -object(Bar)#%d (1) { - ["id"]=> - int(1) -} User-supplied statement does not accept constructor arguments