Commit 0a1f5550ed8 for php
commit 0a1f5550ed89c452fe873a6c604139ceebe326b4
Author: Ilia Alshanetsky <ilia@ilia.ws>
Date: Fri Dec 4 12:17:30 2026 -0500
ext/odbc: Free results when their last reference is dropped
Since the resource-to-object migration the connection's results table held
a reference to every Odbc\Result, so a result and its statement handle stayed
alive until odbc_free_result(), odbc_close() or the end of the request.
Overwriting a result in a loop leaked one open cursor per query.
odbc_result_free() already removes a result from the table, so make the table
non-owning, as ODBCG(connections) is for links.
Based on the patch by merrillh in GH-26053.
Fixes GH-26053
Closes GH-26078
diff --git a/NEWS b/NEWS
index 1b3b63932f9..b6106e37db8 100644
--- a/NEWS
+++ b/NEWS
@@ -10,6 +10,10 @@ PHP NEWS
. Fixed stale or missing global error code and message in MessageFormatter
and IntlDateFormatter::formatObject(). (ChrisAbner)
+- ODBC:
+ . Fixed bug GH-26053 (Odbc\Result not freed when its last reference is
+ dropped). (merrillh, Ilia Alshanetsky)
+
- Opcache:
. Fixed bug GH-25926 (Cross-request UAF read in opcache_get_configuration()
via dangling JIT_G(options) after runtime ini_set('opcache.jit')).
diff --git a/ext/odbc/php_odbc.c b/ext/odbc/php_odbc.c
index 20302d56cb8..d8e220fa5c9 100644
--- a/ext/odbc/php_odbc.c
+++ b/ext/odbc/php_odbc.c
@@ -84,7 +84,6 @@ static void odbc_insert_new_result(odbc_connection *connection, zval *result)
res->index = connection->results.nNextFreeElement;
zend_hash_index_add_new(&connection->results, res->index, result);
- Z_ADDREF_P(result);
}
static inline odbc_link *odbc_link_from_obj(zend_object *obj)
@@ -2179,7 +2178,7 @@ bool odbc_sqlconnect(zval *zv, char *db, char *uid, char *pwd, int cur_opt, bool
object_init_ex(zv, odbc_connection_ce);
link = Z_ODBC_LINK_P(zv);
link->connection = pecalloc(1, sizeof(odbc_connection), persistent);
- zend_hash_init(&link->connection->results, 0, NULL, ZVAL_PTR_DTOR, true);
+ zend_hash_init(&link->connection->results, 0, NULL, NULL, true);
link->persistent = persistent;
link->hash = zend_string_init(hash, hash_len, persistent);
if (persistent) {
diff --git a/ext/odbc/tests/gh26053.phpt b/ext/odbc/tests/gh26053.phpt
new file mode 100644
index 00000000000..dd4f828a982
--- /dev/null
+++ b/ext/odbc/tests/gh26053.phpt
@@ -0,0 +1,57 @@
+--TEST--
+GH-26053 (Odbc\Result not freed when its last reference is dropped)
+--EXTENSIONS--
+odbc
+--INI--
+odbc.allow_persistent=1
+--SKIPIF--
+<?php
+include __DIR__ . "/config.inc";
+$conn = @odbc_connect(ODBC_SQLITE_DSN, null, null);
+if (!$conn) {
+ die("skip requires the SQLite3 ODBC driver");
+}
+?>
+--FILE--
+<?php
+include __DIR__ . "/config.inc";
+
+foreach (['odbc_connect', 'odbc_pconnect'] as $connect) {
+ echo "$connect:\n";
+ $conn = $connect(ODBC_SQLITE_DSN, null, null);
+
+ $r = odbc_exec($conn, "SELECT 1");
+ $weak = WeakReference::create($r);
+ $r = odbc_exec($conn, "SELECT 2");
+ echo "overwritten result freed: "; var_dump($weak->get() === null);
+
+ $weak = WeakReference::create($r);
+ unset($r);
+ echo "unset result freed: "; var_dump($weak->get() === null);
+
+ $stmt = odbc_prepare($conn, "SELECT ?");
+ $weak = WeakReference::create($stmt);
+ unset($stmt);
+ echo "unset prepared statement freed: "; var_dump($weak->get() === null);
+
+ $r = odbc_exec($conn, "SELECT 3");
+ odbc_close($conn);
+ try {
+ odbc_fetch_row($r);
+ } catch (Error $e) {
+ echo $e::class, ": ", $e->getMessage(), PHP_EOL;
+ }
+ unset($r);
+}
+?>
+--EXPECT--
+odbc_connect:
+overwritten result freed: bool(true)
+unset result freed: bool(true)
+unset prepared statement freed: bool(true)
+Error: ODBC result has already been closed
+odbc_pconnect:
+overwritten result freed: bool(true)
+unset result freed: bool(true)
+unset prepared statement freed: bool(true)
+Error: ODBC result has already been closed