[php-src] PHP-8.4: ext/pdo: Keep statement class when ATTR_STATEMENT_CLASS is rejected

From: Date: Tue, 29 Sep 2026 14:47:19 +0000
Subject: [php-src] PHP-8.4: ext/pdo: Keep statement class when ATTR_STATEMENT_CLASS is rejected
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-141683@lists.php.net to get a copy of this message
Author: Ilia Alshanetsky (iliaal) Date: 2026-09-29T10:13:44-04:00 Commit: https://github.com/php/php-src/commit/beee995c90598189e046e2a4c75b0bd5caa4b744 Raw diff: https://github.com/php/php-src/commit/beee995c90598189e046e2a4c75b0bd5caa4b744.diff ext/pdo: Keep statement class when ATTR_STATEMENT_CLASS is rejected pdo_dbh_attribute_set() installed the new PDO::ATTR_STATEMENT_CLASS class and released the old constructor arguments before checking that constructor_args is an array, so a rejected setAttribute() left the new class in place for the next prepare() or query(). Validate constructor_args first, and report its type rather than the whole value's in the TypeError, here and in prepare(). Closes GH-23968 Changed paths: M NEWS M ext/pdo/pdo_dbh.c M ext/pdo/tests/pdo_030.phpt Diff: diff --git a/NEWS b/NEWS index 2e5b71aa8fc0..85b41dfb7cae 100644 --- a/NEWS +++ b/NEWS @@ -102,6 +102,10 @@ PHP NEWS that is not in the result set. (Ilia Alshanetsky) . Fixed bug GH-23962 (Destroying a persistent PDO instance rolls back a transaction still in use by another instance). (Lazizbek Ergashev) + . Fixed PDO::setAttribute() installing a PDO::ATTR_STATEMENT_CLASS class + whose constructor arguments it rejects. (Ilia Alshanetsky) + . Fixed PDO::ATTR_STATEMENT_CLASS constructor_args type errors reporting + "array given" regardless of the value passed. (Ilia Alshanetsky) - PDO_Firebird: . Fixed bug GH-23758 (PDO_Firebird returns null for non-null empty BLOBs). diff --git a/ext/pdo/pdo_dbh.c b/ext/pdo/pdo_dbh.c index 1dc43c2b9728..d3fcf6101249 100644 --- a/ext/pdo/pdo_dbh.c +++ b/ext/pdo/pdo_dbh.c @@ -631,7 +631,7 @@ PHP_METHOD(PDO, prepare) if ((item = zend_hash_index_find(Z_ARRVAL_P(value), 1)) != NULL) { if (Z_TYPE_P(item) != IS_ARRAY) { zend_type_error("PDO::ATTR_STATEMENT_CLASS constructor_args must be of type ?array, %s given", - zend_zval_value_name(value)); + zend_zval_value_name(item)); RETURN_THROWS(); } ZVAL_COPY_VALUE(&ctor_args, item); @@ -925,17 +925,18 @@ static bool pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value) / zend_type_error("User-supplied statement class cannot have a public constructor"); return false; } + item = zend_hash_index_find(Z_ARRVAL_P(value), 1); + if (item != NULL && Z_TYPE_P(item) != IS_ARRAY) { + zend_type_error("PDO::ATTR_STATEMENT_CLASS constructor_args must be of type ?array, %s given", + zend_zval_value_name(item)); + return false; + } dbh->def_stmt_ce = pce; if (!Z_ISUNDEF(dbh->def_stmt_ctor_args)) { zval_ptr_dtor(&dbh->def_stmt_ctor_args); ZVAL_UNDEF(&dbh->def_stmt_ctor_args); } - if ((item = zend_hash_index_find(Z_ARRVAL_P(value), 1)) != NULL) { - if (Z_TYPE_P(item) != IS_ARRAY) { - zend_type_error("PDO::ATTR_STATEMENT_CLASS constructor_args must be of type ?array, %s given", - zend_zval_value_name(value)); - return false; - } + if (item != NULL) { ZVAL_COPY(&dbh->def_stmt_ctor_args, item); } return true; diff --git a/ext/pdo/tests/pdo_030.phpt b/ext/pdo/tests/pdo_030.phpt index 88308c18ca83..097551211a02 100644 --- a/ext/pdo/tests/pdo_030.phpt +++ b/ext/pdo/tests/pdo_030.phpt @@ -75,6 +75,17 @@ echo "===QUERY===\n"; var_dump($db->getAttribute(PDO::ATTR_STATEMENT_CLASS)); $db->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('PDOStatementx', array($db))); var_dump($db->getAttribute(PDO::ATTR_STATEMENT_CLASS)); +try { + $db->setAttribute(PDO::ATTR_STATEMENT_CLASS, [PDOStatement::class, 'invalid-args']); +} catch (TypeError $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} +var_dump($db->getAttribute(PDO::ATTR_STATEMENT_CLASS)); +try { + $db->prepare('SELECT * FROM test030', [PDO::ATTR_STATEMENT_CLASS => [PDOStatement::class, 'invalid-args']]); +} catch (TypeError $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} $stmt = $db->query('SELECT * FROM test030'); var_dump(get_class($stmt)); var_dump(get_class($stmt->dbh)); @@ -112,6 +123,18 @@ array(2) { } } } +TypeError: PDO::ATTR_STATEMENT_CLASS constructor_args must be of type ?array, string given +array(2) { + [0]=> + string(13) "PDOStatementX" + [1]=> + array(1) { + [0]=> + object(PDODatabase)#%d (0) { + } + } +} +TypeError: PDO::ATTR_STATEMENT_CLASS constructor_args must be of type ?array, string given PDODatabase::query() PDOStatementX::__construct() string(13) "PDOStatementX"

« previous php.cvs (#141683) next »