Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ PHP NEWS
registrations are freed while still reachable from the cycle collector.
(Ilia Alshanetsky)

- PDO:
. Fixed PDOStatement::getColumnMeta() reading out of bounds for an invalid
column index. (Ilia Alshanetsky)


24 Sep 2026, PHP 8.4.26

Expand Down
7 changes: 7 additions & 0 deletions ext/pdo/pdo_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,13 @@ PHP_METHOD(PDOStatement, getColumnMeta)
RETURN_FALSE;
}

if (stmt->columns == NULL || colno >= stmt->column_count) {
zval_ptr_dtor(return_value);
ZVAL_UNDEF(return_value);
pdo_raise_impl_error(stmt->dbh, stmt, "07009", "invalid column index");
RETURN_FALSE;
}
Comment on lines +1703 to +1708

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this make sense?

Suggested change
if (stmt->columns == NULL || colno >= stmt->column_count) {
zend_value_error("Invalid column index");
RETURN_THROWS();
}
if (stmt->columns == NULL || colno >= stmt->column_count) {
pdo_raise_impl_error(stmt->dbh, stmt, "07009", "invalid column index");
RETURN_FALSE;
}

07009 (Invalid descriptor index) seems like a good fit for this case.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adopted. Added a zval_ptr_dtor(return_value) with it, since the hook has already built the array and RETURN_FALSE would otherwise drop it.

The guard sits after the hook though, so it does not cover pdo_firebird, which indexes out_sqlda.sqlvar[colno] unchecked and then returns 1. I would fix that driver-side like GH-17837 rather than move the guard ahead of the hook, since that would change what the bound-checking drivers return. Sound right?


/* add stock items */
col = &stmt->columns[colno];
add_assoc_str(return_value, "name", zend_string_copy(col->name));
Expand Down
49 changes: 49 additions & 0 deletions ext/pdo_odbc/tests/getcolumnmeta_bounds.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--TEST--
PDO_odbc getColumnMeta() bounds checking on unexecuted statements and invalid indexes
--EXTENSIONS--
pdo_odbc
--SKIPIF--
<?php
try {
new Pdo\Odbc('odbc:Driver={SQLite3};Database=:memory:');
} catch (Throwable $e) {
die('skip SQLite3 ODBC driver not available');
}
?>
--FILE--
<?php
$pdo = new Pdo\Odbc('odbc:Driver={SQLite3};Database=:memory:',
null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->prepare('SELECT 1 AS one');

try {
var_dump($stmt->getColumnMeta(0));
} catch (PDOException $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}

$stmt->execute();
var_dump($stmt->getColumnMeta(0));

try {
var_dump($stmt->getColumnMeta(5));
} catch (PDOException $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}

echo "done\n";
?>
--EXPECTF--
PDOException: SQLSTATE[07009]: Invalid descriptor index: invalid column index
array(4) {
["pdo_type"]=>
int(2)
["name"]=>
string(3) "one"
["len"]=>
int(%d)
["precision"]=>
int(%d)
}
PDOException: SQLSTATE[07009]: Invalid descriptor index: invalid column index
done
Loading