Skip to content

Make PDO connection construction single-shot#191

Closed
iliaal wants to merge 1 commit into
PHP-8.4from
fix/pdo-guard-reconstruct
Closed

Make PDO connection construction single-shot#191
iliaal wants to merge 1 commit into
PHP-8.4from
fix/pdo-guard-reconstruct

Conversation

@iliaal

@iliaal iliaal commented Jul 23, 2026

Copy link
Copy Markdown
Owner

Constructing a PDO over an already constructed handle re-runs connection setup and, for a persistent connection, frees the pemalloc'd dbh with efree(), corrupting the heap. The same sink is also reachable by reentering __construct() from a uri: DSN stream wrapper or a failed connect()'s destructor, and by retrying a persistent construct that failed after swapping in the handle. The fix marks construction in progress with an ABI-neutral reserved bit (sizeof unchanged) and rejects reconstruction for both __construct() and connect(). A construct that fails now throws on retry rather than corrupting.

$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_PERSISTENT => true]);
$pdo->__construct('sqlite::memory:', null, null, [PDO::ATTR_PERSISTENT => true]);

Constructing a PDO handle over an already constructed one re-ran
connection setup; for a persistent connection the second pass freed the
pemalloc'd dbh with efree() and corrupted the heap. Besides a plain
second __construct(), the same sink was reachable by reentering during a
uri: DSN stream open, by retrying a persistent construct that failed
after swapping in the persistent handle, and by a subclass destructor
reentering while a failed persistent connect() unwinds. Track
construction with an is_constructing flag, set as soon as the handle
exists for both __construct() and connect(), carried onto the persistent
handle and cleared only on success; construction is rejected once the
driver is attached or a construction is in progress. A construct that
fails now leaves the handle unusable instead of allowing a retry.
@iliaal
iliaal force-pushed the fix/pdo-guard-reconstruct branch from 5806ad2 to 980028e Compare July 23, 2026 14:35
@iliaal

iliaal commented Jul 23, 2026

Copy link
Copy Markdown
Owner Author

Promoted upstream: php#22874

@iliaal iliaal closed this Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant