Skip to content

[PDO] Bound-check column index in getColumnMeta() - #23654

Open
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/pdo-getcolumnmeta-bounds-84
Open

[PDO] Bound-check column index in getColumnMeta()#23654
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/pdo-getcolumnmeta-bounds-84

Conversation

@iliaal

@iliaal iliaal commented Sep 10, 2026

Copy link
Copy Markdown
Member

PDOStatement::getColumnMeta() indexed columns as soon as the driver hook succeeded, without checking that columns had been described or that the index was in range, so a driver that reports success unconditionally (pdo_odbc) segfaulted on an unexecuted statement. Both cases now raise ValueError, matching the fetch paths. Drivers that report failure themselves still return false.

Comment on lines +21 to +32
} catch (ValueError $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
} catch (ValueError $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
$stmt->execute();
var_dump($stmt->getColumnMeta(0));
try {
var_dump($stmt->getColumnMeta(5));
} catch (ValueError $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
$stmt->execute();
var_dump($stmt->getColumnMeta(0));
try {
var_dump($stmt->getColumnMeta(5));
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}

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.

Took the $e::class part. The thrown type changed to PDOException in the meantime, and ext/pdo tests catch that rather than Throwable.

Comment thread ext/pdo/pdo_stmt.c
Comment on lines +1703 to +1706
if (stmt->columns == NULL || colno >= stmt->column_count) {
zend_value_error("Invalid column index");
RETURN_THROWS();
}

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?

PDOStatement::getColumnMeta() indexed stmt->columns as soon as the driver
hook reported success, without checking that the columns had been
described or that the index was in range, so pdo_odbc, whose hook always
reports success, read out of bounds and crashed. Raise SQLSTATE 07009 and
return false instead, matching what the drivers that bound-check the
index in their own hook already return.
@iliaal
iliaal force-pushed the fix/pdo-getcolumnmeta-bounds-84 branch from 731ecbc to cb00636 Compare September 11, 2026 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants