Commit 2f83f3aa374 for php

commit 2f83f3aa374ceb5c6239b65b2761a24e940c3908
Author: lazerg <lazerg2@gmail.com>
Date:   Fri Sep 18 20:59:20 2026 +0000

    ext/pdo_sqlite: crash when a collation callback closes the statement cursor

    PDOStatement::closeCursor() calls sqlite3_reset() on the underlying
    sqlite3_stmt. When a collation callback calls it, the reset lands on the
    statement sqlite3_step() is still running, SQLite tears down the VDBE
    under itself, and the process segfaults. Re-entering execute() from the
    same callback also crashes, through the nested step. Re-entering fetch()
    does not crash, but the nested step silently skips a row.

    The driver statement now records that it is being stepped, and those
    paths throw an Error instead of reaching SQLite. The running query still
    finishes, so the statement is usable afterwards. gh23756_4.phpt documents
    that closeCursor() called from a shutdown function after the callback
    calls exit() keeps working normally.

    Fix GH-23756
    Close GH-23762

diff --git a/NEWS b/NEWS
index bd6d660d93b..30afb9e1238 100644
--- a/NEWS
+++ b/NEWS
@@ -181,6 +181,8 @@ PHP                                                                        NEWS
 - PDO_SQLite:
   . Fixed bug GH-25588 (Use-after-free of a string parameter omitted on a later
     execute() or rebound while fetching). (Ilia Alshanetsky)
+  . Fixed bug GH-23756 (Crash when a callback closes the cursor of or
+    re-executes the statement being executed). (Lazizbek Ergashev)

 - PGSQL:
   . Fixed bug GH-25772 (pg_fetch_object() crashes with JIT when the constructor
diff --git a/ext/pdo_sqlite/php_pdo_sqlite_int.h b/ext/pdo_sqlite/php_pdo_sqlite_int.h
index 43a07345ed6..8ac0e4fdec3 100644
--- a/ext/pdo_sqlite/php_pdo_sqlite_int.h
+++ b/ext/pdo_sqlite/php_pdo_sqlite_int.h
@@ -57,6 +57,7 @@ typedef struct {
 	sqlite3_stmt *stmt;
 	unsigned pre_fetched:1;
 	unsigned done:1;
+	unsigned stepping:1;
 } pdo_sqlite_stmt;

 extern const pdo_driver_t pdo_sqlite_driver;
diff --git a/ext/pdo_sqlite/sqlite_statement.c b/ext/pdo_sqlite/sqlite_statement.c
index b118b9b8310..708be7a74ef 100644
--- a/ext/pdo_sqlite/sqlite_statement.c
+++ b/ext/pdo_sqlite/sqlite_statement.c
@@ -39,16 +39,41 @@ static int pdo_sqlite_stmt_dtor(pdo_stmt_t *stmt)
 	return 1;
 }

+static int pdo_sqlite_stmt_step(pdo_sqlite_stmt *S)
+{
+	int return_code;
+	bool bailout = false;
+
+	S->stepping = 1;
+	zend_try {
+		return_code = sqlite3_step(S->stmt);
+	} zend_catch {
+		bailout = true;
+	} zend_end_try();
+	S->stepping = 0;
+
+	if (bailout) {
+		zend_bailout();
+	}
+
+	return return_code;
+}
+
 static int pdo_sqlite_stmt_execute(pdo_stmt_t *stmt)
 {
 	pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;

+	if (S->stepping) {
+		zend_throw_error(NULL, "Cannot execute a PDOStatement while it is executing");
+		return 0;
+	}
+
 	if (stmt->executed && !S->done) {
 		sqlite3_reset(S->stmt);
 	}

 	S->done = 0;
-	switch (sqlite3_step(S->stmt)) {
+	switch (pdo_sqlite_stmt_step(S)) {
 		case SQLITE_ROW:
 			S->pre_fetched = 1;
 			php_pdo_stmt_set_column_count(stmt, sqlite3_data_count(S->stmt));
@@ -79,6 +104,11 @@ static int pdo_sqlite_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_d

 	switch (event_type) {
 		case PDO_PARAM_EVT_EXEC_PRE:
+			if (S->stepping) {
+				zend_throw_error(NULL, "Cannot execute a PDOStatement while it is executing");
+				return 0;
+			}
+
 			if (stmt->executed && !S->done) {
 				sqlite3_reset(S->stmt);
 				S->done = 1;
@@ -207,6 +237,10 @@ static int pdo_sqlite_stmt_fetch(pdo_stmt_t *stmt,
 	if (!S->stmt) {
 		return 0;
 	}
+	if (S->stepping) {
+		zend_throw_error(NULL, "Cannot fetch from a PDOStatement while it is executing");
+		return 0;
+	}
 	if (S->pre_fetched) {
 		S->pre_fetched = 0;
 		return 1;
@@ -214,7 +248,7 @@ static int pdo_sqlite_stmt_fetch(pdo_stmt_t *stmt,
 	if (S->done) {
 		return 0;
 	}
-	i = sqlite3_step(S->stmt);
+	i = pdo_sqlite_stmt_step(S);
 	switch (i) {
 		case SQLITE_ROW:
 			return 1;
@@ -363,6 +397,12 @@ static int pdo_sqlite_stmt_col_meta(pdo_stmt_t *stmt, zend_long colno, zval *ret
 static int pdo_sqlite_stmt_cursor_closer(pdo_stmt_t *stmt)
 {
 	pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
+
+	if (S->stepping) {
+		zend_throw_error(NULL, "Cannot close the cursor of a PDOStatement while it is executing");
+		return 0;
+	}
+
 	sqlite3_reset(S->stmt);
 	return 1;
 }
diff --git a/ext/pdo_sqlite/tests/gh23756.phpt b/ext/pdo_sqlite/tests/gh23756.phpt
new file mode 100644
index 00000000000..caabd552c10
--- /dev/null
+++ b/ext/pdo_sqlite/tests/gh23756.phpt
@@ -0,0 +1,37 @@
+--TEST--
+GH-23756 (crash when closeCursor() is called from a collation callback)
+--CREDITS--
+djarfluka
+--EXTENSIONS--
+pdo_sqlite
+--FILE--
+<?php
+$db = Pdo\Sqlite::connect('sqlite::memory:');
+$db->exec('CREATE TABLE t (x TEXT)');
+$db->exec("INSERT INTO t VALUES ('b'), ('a'), ('c')");
+
+$stmt = null;
+$db->createCollation('evil', function ($a, $b) use (&$stmt) {
+    $stmt->closeCursor();
+    return $a <=> $b;
+});
+
+$stmt = $db->prepare('SELECT x FROM t ORDER BY x COLLATE evil');
+try {
+    $stmt->execute();
+} catch (Error $e) {
+    echo $e->getMessage(), "\n";
+}
+
+var_dump($db->query('SELECT x FROM t ORDER BY x')->fetchAll(PDO::FETCH_COLUMN));
+?>
+--EXPECT--
+Cannot close the cursor of a PDOStatement while it is executing
+array(3) {
+  [0]=>
+  string(1) "a"
+  [1]=>
+  string(1) "b"
+  [2]=>
+  string(1) "c"
+}
diff --git a/ext/pdo_sqlite/tests/gh23756_2.phpt b/ext/pdo_sqlite/tests/gh23756_2.phpt
new file mode 100644
index 00000000000..c48259df2ee
--- /dev/null
+++ b/ext/pdo_sqlite/tests/gh23756_2.phpt
@@ -0,0 +1,34 @@
+--TEST--
+GH-23756 (crash when closeCursor() is called from a collation callback while fetching)
+--EXTENSIONS--
+pdo_sqlite
+--FILE--
+<?php
+$db = Pdo\Sqlite::connect('sqlite::memory:');
+$db->exec('CREATE TABLE t (x TEXT)');
+$db->exec("INSERT INTO t VALUES ('b'), ('a'), ('c')");
+
+$stmt = null;
+$armed = false;
+$db->createCollation('evil', function ($a, $b) use (&$stmt, &$armed) {
+    if ($armed) {
+        $armed = false;
+        $stmt->closeCursor();
+    }
+    return $a <=> $b;
+});
+
+$stmt = $db->prepare("SELECT x FROM t WHERE x <> 'zzz' COLLATE evil");
+$stmt->execute();
+var_dump($stmt->fetchColumn());
+
+$armed = true;
+try {
+    $stmt->fetchColumn();
+} catch (Error $e) {
+    echo $e->getMessage(), "\n";
+}
+?>
+--EXPECT--
+string(1) "b"
+Cannot close the cursor of a PDOStatement while it is executing
diff --git a/ext/pdo_sqlite/tests/gh23756_3.phpt b/ext/pdo_sqlite/tests/gh23756_3.phpt
new file mode 100644
index 00000000000..7492ff26e75
--- /dev/null
+++ b/ext/pdo_sqlite/tests/gh23756_3.phpt
@@ -0,0 +1,40 @@
+--TEST--
+GH-23756 (crash when execute() is called from a collation callback)
+--EXTENSIONS--
+pdo_sqlite
+--FILE--
+<?php
+$db = Pdo\Sqlite::connect('sqlite::memory:');
+$db->exec('CREATE TABLE t (x TEXT)');
+$db->exec("INSERT INTO t VALUES ('b'), ('a'), ('c')");
+
+$stmt = null;
+$armed = false;
+$db->createCollation('evil', function ($a, $b) use (&$stmt, &$armed) {
+    if ($armed) {
+        $armed = false;
+        $stmt->execute();
+    }
+    return $a <=> $b;
+});
+
+$stmt = $db->prepare('SELECT x FROM t ORDER BY x COLLATE evil');
+$armed = true;
+try {
+    $stmt->execute();
+} catch (Error $e) {
+    echo 'without parameters: ', $e->getMessage(), "\n";
+}
+
+$stmt = $db->prepare('SELECT x FROM t WHERE x <> :p ORDER BY x COLLATE evil');
+$stmt->bindValue(':p', 'zzz');
+$armed = true;
+try {
+    $stmt->execute();
+} catch (Error $e) {
+    echo 'with a bound parameter: ', $e->getMessage(), "\n";
+}
+?>
+--EXPECT--
+without parameters: Cannot execute a PDOStatement while it is executing
+with a bound parameter: Cannot execute a PDOStatement while it is executing
diff --git a/ext/pdo_sqlite/tests/gh23756_4.phpt b/ext/pdo_sqlite/tests/gh23756_4.phpt
new file mode 100644
index 00000000000..3f740498833
--- /dev/null
+++ b/ext/pdo_sqlite/tests/gh23756_4.phpt
@@ -0,0 +1,24 @@
+--TEST--
+GH-23756 (closeCursor() still works after a collation callback bails out)
+--EXTENSIONS--
+pdo_sqlite
+--FILE--
+<?php
+$db = Pdo\Sqlite::connect('sqlite::memory:');
+$db->exec('CREATE TABLE t (x TEXT)');
+$db->exec("INSERT INTO t VALUES ('b'), ('a'), ('c')");
+
+$db->createCollation('evil', function ($a, $b) {
+    exit("bailing out\n");
+});
+
+register_shutdown_function(function () use (&$stmt) {
+    var_dump($stmt->closeCursor());
+});
+
+$stmt = $db->prepare('SELECT x FROM t ORDER BY x COLLATE evil');
+$stmt->execute();
+?>
+--EXPECT--
+bailing out
+bool(true)
diff --git a/ext/pdo_sqlite/tests/gh23756_5.phpt b/ext/pdo_sqlite/tests/gh23756_5.phpt
new file mode 100644
index 00000000000..8ea7bfaea73
--- /dev/null
+++ b/ext/pdo_sqlite/tests/gh23756_5.phpt
@@ -0,0 +1,34 @@
+--TEST--
+GH-23756 (fetch() is called from a collation callback while fetching)
+--EXTENSIONS--
+pdo_sqlite
+--FILE--
+<?php
+$db = Pdo\Sqlite::connect('sqlite::memory:');
+$db->exec('CREATE TABLE t (x TEXT)');
+$db->exec("INSERT INTO t VALUES ('b'), ('a'), ('c')");
+
+$stmt = null;
+$armed = false;
+$db->createCollation('evil', function ($a, $b) use (&$stmt, &$armed) {
+    if ($armed) {
+        $armed = false;
+        $stmt->fetch();
+    }
+    return $a <=> $b;
+});
+
+$stmt = $db->prepare("SELECT x FROM t WHERE x <> 'zzz' COLLATE evil");
+$stmt->execute();
+var_dump($stmt->fetchColumn());
+
+$armed = true;
+try {
+    $stmt->fetchColumn();
+} catch (Error $e) {
+    echo $e->getMessage(), "\n";
+}
+?>
+--EXPECT--
+string(1) "b"
+Cannot fetch from a PDOStatement while it is executing