Commit 4021e1d4c4e for php
commit 4021e1d4c4e87ba110f0786fb2688bdddf875c28
Author: Tomasz Kowalczyk <tomasz@kowalczyk.cc>
Date: Sun Nov 22 15:53:43 2026 +0100
Fix GH-25650: PDO::query() reports wrong argument numbers for ctor args
pdo_stmt_setup_fetch_mode() hardcoded argument 3 and argument #2 in the
error for constructor arguments passed with a class that has no
constructor. That is only right for PDOStatement::setFetchMode().
PDO::query() passes the fetch mode as argument 2, so use the computed
constructor_arg_num and arg1_arg_num instead.
Closes GH-25651. Fixes GH-25650.
diff --git a/NEWS b/NEWS
index 1a8aeaec073..c9429f71cc0 100644
--- a/NEWS
+++ b/NEWS
@@ -125,6 +125,10 @@ PHP NEWS
. Fixed bug GH-20920 (LibreSSL build regression with PHP 8.5).
(Eugene Gvozdetsky, Theo Buehler, Jakub Zelenka)
+- PDO:
+ . Fixed bug GH-25650 (PDO::query() reports the wrong argument numbers for
+ FETCH_CLASS constructor arguments). (Tomasz Kowalczyk)
+
- PDO_Firebird:
. Fixed stack buffer overflow with long connection user names, passwords,
charsets, or roles. (Ilia Alshanetsky)
diff --git a/ext/pdo/pdo_stmt.c b/ext/pdo/pdo_stmt.c
index a27c491b5cd..5fe1bdbb4ad 100644
--- a/ext/pdo/pdo_stmt.c
+++ b/ext/pdo/pdo_stmt.c
@@ -1755,7 +1755,7 @@ bool pdo_stmt_setup_fetch_mode(pdo_stmt_t *stmt, zend_long mode, uint32_t mode_a
}
if (Z_TYPE(args[1]) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL(args[1]))) {
if (cep->constructor == NULL) {
- zend_argument_value_error(3, "must be empty when class provided in argument #2 ($class) does not have a constructor");
+ zend_argument_value_error(constructor_arg_num, "must be empty when class provided in argument #%d ($class) does not have a constructor", arg1_arg_num);
return false;
}
fetch_ctor_args = Z_ARRVAL(args[1]);
diff --git a/ext/pdo/tests/pdo_stmt_class_ctor_errors.phpt b/ext/pdo/tests/pdo_stmt_class_ctor_errors.phpt
index 85fdbbec37f..a904e718448 100644
--- a/ext/pdo/tests/pdo_stmt_class_ctor_errors.phpt
+++ b/ext/pdo/tests/pdo_stmt_class_ctor_errors.phpt
@@ -38,6 +38,11 @@ class TestBase
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
}
+try {
+ var_dump($db->query('SELECT id, val, val2 from pdo_fetch_all_class_ctor_error', PDO::FETCH_CLASS, 'TestBase', [0]));
+} catch (\Throwable $e) {
+ echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
+}
?>
--CLEAN--
@@ -49,3 +54,4 @@ class TestBase
--EXPECT--
ValueError: PDOStatement::fetchAll(): Argument #3 must be empty when class provided in argument #2 ($class) does not have a constructor
ValueError: PDOStatement::setFetchMode(): Argument #3 must be empty when class provided in argument #2 ($class) does not have a constructor
+ValueError: PDO::query(): Argument #4 must be empty when class provided in argument #3 ($class) does not have a constructor