Skip to content

Commit 4021e1d

Browse files
thundererkamil-tekiela
authored andcommitted
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.
1 parent 171b97e commit 4021e1d

3 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ PHP NEWS
125125
. Fixed bug GH-20920 (LibreSSL build regression with PHP 8.5).
126126
(Eugene Gvozdetsky, Theo Buehler, Jakub Zelenka)
127127

128+
- PDO:
129+
. Fixed bug GH-25650 (PDO::query() reports the wrong argument numbers for
130+
FETCH_CLASS constructor arguments). (Tomasz Kowalczyk)
131+
128132
- PDO_Firebird:
129133
. Fixed stack buffer overflow with long connection user names, passwords,
130134
charsets, or roles. (Ilia Alshanetsky)

‎ext/pdo/pdo_stmt.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,7 @@ bool pdo_stmt_setup_fetch_mode(pdo_stmt_t *stmt, zend_long mode, uint32_t mode_a
17551755
}
17561756
if (Z_TYPE(args[1]) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL(args[1]))) {
17571757
if (cep->constructor == NULL) {
1758-
zend_argument_value_error(3, "must be empty when class provided in argument #2 ($class) does not have a constructor");
1758+
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);
17591759
return false;
17601760
}
17611761
fetch_ctor_args = Z_ARRVAL(args[1]);

‎ext/pdo/tests/pdo_stmt_class_ctor_errors.phpt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ try {
3838
} catch (\Throwable $e) {
3939
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
4040
}
41+
try {
42+
var_dump($db->query('SELECT id, val, val2 from pdo_fetch_all_class_ctor_error', PDO::FETCH_CLASS, 'TestBase', [0]));
43+
} catch (\Throwable $e) {
44+
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
45+
}
4146

4247
?>
4348
--CLEAN--
@@ -49,3 +54,4 @@ PDOTest::dropTableIfExists($db, "pdo_fetch_all_class_ctor_error");
4954
--EXPECT--
5055
ValueError: PDOStatement::fetchAll(): Argument #3 must be empty when class provided in argument #2 ($class) does not have a constructor
5156
ValueError: PDOStatement::setFetchMode(): Argument #3 must be empty when class provided in argument #2 ($class) does not have a constructor
57+
ValueError: PDO::query(): Argument #4 must be empty when class provided in argument #3 ($class) does not have a constructor

0 commit comments

Comments
 (0)