Skip to content

Commit 620bb01

Browse files
committed
Prevent PDO driver attribute collisions
PDO drivers reuse driver-specific attribute IDs. Avoid applying MySQL options as unrelated SQLite options, and limit SQLite constructor options to persistence.
1 parent 54cacd3 commit 620bb01

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,7 @@ class WP_MySQL_On_SQLite extends PDO {
767767
* Optional driver options.
768768
*
769769
* Numeric keys are handled as standard PDO constructor options.
770+
* Driver-specific PDO options are not supported.
770771
*
771772
* @type int $mysql_version Optional. MySQL version to emulate. Default 80038.
772773
* @type PDO|null $pdo Optional. Existing SQLite PDO connection.
@@ -832,10 +833,17 @@ function ( $key ) {
832833
ARRAY_FILTER_USE_KEY
833834
);
834835

836+
$connection_pdo_options = array();
837+
if ( array_key_exists( PDO::ATTR_PERSISTENT, $pdo_options ) ) {
838+
// Persistence is connection-time-only. Do not apply it again after initialization.
839+
$connection_pdo_options[ PDO::ATTR_PERSISTENT ] = $pdo_options[ PDO::ATTR_PERSISTENT ];
840+
unset( $pdo_options[ PDO::ATTR_PERSISTENT ] );
841+
}
842+
835843
$connection_options = array(
836844
'journal_mode' => $options['journal_mode'] ?? null,
837845
'synchronous' => $options['synchronous'] ?? null,
838-
'pdo_options' => $pdo_options,
846+
'pdo_options' => $connection_pdo_options,
839847
);
840848
if ( isset( $options['pdo'] ) ) {
841849
$connection_options['pdo'] = $options['pdo'];
@@ -935,11 +943,6 @@ function ( string $sql, array $params ) {
935943
);
936944

937945
foreach ( $pdo_options as $attribute => $value ) {
938-
// Persistence is a connection-time-only option and was already passed
939-
// to the underlying PDO constructor when creating a new connection.
940-
if ( PDO::ATTR_PERSISTENT === $attribute ) {
941-
continue;
942-
}
943946
$this->setAttribute( $attribute, $value );
944947
}
945948
}
@@ -1383,6 +1386,12 @@ public function inTransaction(): bool {
13831386
* @return bool True on success, false on failure.
13841387
*/
13851388
public function setAttribute( $attribute, $value ): bool {
1389+
// PDO reserves IDs starting at 1000 for driver-specific attributes.
1390+
// These differ for MySQL and SQLite and are currently not supported.
1391+
if ( is_int( $attribute ) && 1000 <= $attribute ) {
1392+
return false;
1393+
}
1394+
13861395
// Track the caller's error mode while keeping internal SQLite operations in exception mode.
13871396
if ( PDO::ATTR_ERRMODE === $attribute ) {
13881397
$pdo = $this->connection->get_pdo();

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_PDO_API_Tests.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ public function test_constructor_accepts_null_options(): void {
6161
$this->assertInstanceOf( PDO::class, $driver );
6262
}
6363

64+
public function test_constructor_does_not_forward_driver_specific_pdo_options(): void {
65+
// PDO MySQL and PDO SQLite assign different options to attributes 1000 and 1002.
66+
foreach (
67+
array(
68+
1000 => true,
69+
1002 => 'SET NAMES utf8mb4',
70+
) as $attribute => $value
71+
) {
72+
$driver = new WP_MySQL_On_SQLite(
73+
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
74+
null,
75+
null,
76+
array( $attribute => $value )
77+
);
78+
79+
$this->assertEquals( 1, $driver->query( 'SELECT 1' )->fetchColumn() );
80+
}
81+
}
82+
6483
public function test_constructor_applies_pdo_options(): void {
6584
$driver = new WP_MySQL_On_SQLite(
6685
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
@@ -957,6 +976,11 @@ public function test_attr_default_fetch_mode(): void {
957976
);
958977
}
959978

979+
public function test_set_attribute_rejects_driver_specific_attributes(): void {
980+
// Attribute 1002 has unrelated meanings in PDO MySQL and PDO SQLite.
981+
$this->assertFalse( $this->driver->setAttribute( 1002, true ) );
982+
}
983+
960984
public function test_attr_stringify_fetches(): void {
961985
$this->driver->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
962986
$this->assertTrue( $this->driver->getAttribute( PDO::ATTR_STRINGIFY_FETCHES ) );

0 commit comments

Comments
 (0)