Commit e32e32b9907 for php
commit e32e32b990751f2b5dce1205d7d215459ccc8b87
Author: David Carlier <devnexen@gmail.com>
Date: Fri Nov 27 14:54:42 2026 +0000
ext/pdo_pgsql: dangling running statement after a failed query().
PDO::query() detaches the handle from a failed statement before releasing it,
so pgsql_stmt_dtor() skipped clearing H->running_stmt and the connection kept a
pointer to the freed statement that the next lazy query dereferenced in
pgsql_stmt_finish(). Clear it as well when the handle is merely detached; an
already destroyed handle (bug72294) is still left untouched.
Fix GH-25838
Co-authored-by: Yun Dou <dixyes@gmail.com>
Close GH-25841
diff --git a/NEWS b/NEWS
index 8dd09e91ac9..7a3f2087bd2 100644
--- a/NEWS
+++ b/NEWS
@@ -190,6 +190,10 @@ PHP NEWS
. Fixed a crash when executing a prepared statement again after a failed
execute() with libmysqlclient. (Ilia Alshanetsky)
+- PDO_PGSQL:
+ . Fixed bug GH-25838 (use-after-free when a lazy statement follows a failed
+ query). (Yun Dou)
+
- PDO_SQLite:
. Fixed bug GH-25588 (Use-after-free of a string parameter omitted on a later
execute() or rebound while fetching). (Ilia Alshanetsky)
diff --git a/ext/pdo_pgsql/pgsql_statement.c b/ext/pdo_pgsql/pgsql_statement.c
index b2af41a339b..60199f86269 100644
--- a/ext/pdo_pgsql/pgsql_statement.c
+++ b/ext/pdo_pgsql/pgsql_statement.c
@@ -191,7 +191,7 @@ static int pgsql_stmt_dtor(pdo_stmt_t *stmt)
pgsql_stmt_finish(S, FIN_DISCARD|(server_obj_usable ? FIN_CLOSE|FIN_ABORT : 0));
- if (server_obj_usable && S->H->running_stmt == S) {
+ if ((server_obj_usable || stmt->database_object_handle == NULL) && S->H->running_stmt == S) {
S->H->running_stmt = NULL;
}
diff --git a/ext/pdo_pgsql/tests/gh25838.phpt b/ext/pdo_pgsql/tests/gh25838.phpt
new file mode 100644
index 00000000000..02b57516127
--- /dev/null
+++ b/ext/pdo_pgsql/tests/gh25838.phpt
@@ -0,0 +1,30 @@
+--TEST--
+GH-25838 (pdo_pgsql: use-after-free when a lazy statement follows a failed query)
+--EXTENSIONS--
+pdo_pgsql
+--SKIPIF--
+<?php
+require __DIR__ . '/config.inc';
+require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
+PDOTest::skip();
+?>
+--CREDITS--
+Yun Dou
+--FILE--
+<?php
+require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
+$pdo = PDOTest::test_factory(__DIR__ . '/common.phpt');
+$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+
+try {
+ $pdo->query('SELECT 1 / 0');
+} catch (PDOException $e) {
+ echo $e->getCode(), PHP_EOL;
+}
+
+$pdo->setAttribute(PDO::ATTR_PREFETCH, 0);
+echo $pdo->query('SELECT 43')->fetchColumn(), PHP_EOL;
+?>
+--EXPECT--
+22012
+43