[php-src] master: Merge branch 'PHP-8.5'
| From: | Ilia Alshanetsky | Date: | Wed, 16 Sep 2026 13:06:40 +0000 |
| Subject: | [php-src] master: Merge branch 'PHP-8.5' | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-141230@lists.php.net to get a copy of this message | ||
Author: Ilia Alshanetsky (iliaal)
Date: 2026-09-16T09:03:49-04:00
Commit: https://github.com/php/php-src/commit/1f397a9488c0a7f7f56450f8fa8eb0a952c941f8
Raw diff: https://github.com/php/php-src/commit/1f397a9488c0a7f7f56450f8fa8eb0a952c941f8.diff
Merge branch 'PHP-8.5'
* PHP-8.5:
ext/sqlite3: reject close() from inside a callback
Changed paths:
A ext/sqlite3/tests/sqlite3_close_from_callback.phpt
A ext/sqlite3/tests/sqlite3_close_from_callback_aggregate.phpt
A ext/sqlite3/tests/sqlite3_close_from_callback_authorizer.phpt
A ext/sqlite3/tests/sqlite3_close_from_callback_collation.phpt
M NEWS
M ext/sqlite3/php_sqlite3_structs.h
M ext/sqlite3/sqlite3.c
Diff:
diff --git a/NEWS b/NEWS
index bbac9779425e..220ecbfb11fe 100644
--- a/NEWS
+++ b/NEWS
@@ -44,6 +44,10 @@ PHP NEWS
. Fixed bug GH-23385 (SplDoublyLinkedList::serialize() use-after-free when
__serialize() removes an element). (David Carlier)
+- SQLite:
+ . Fixed a crash when SQLite3::close() is called from a userland callback.
+ (Ilia Alshanetsky)
+
- Standard:
. Fixed bug #60110 (fclose(), file_put_contents(), copy() do not return false
properly). (Jakub Zelenka, Ilija Tovilo)
diff --git a/ext/sqlite3/php_sqlite3_structs.h b/ext/sqlite3/php_sqlite3_structs.h
index e8488028240e..fbc8d4fd246b 100644
--- a/ext/sqlite3/php_sqlite3_structs.h
+++ b/ext/sqlite3/php_sqlite3_structs.h
@@ -48,6 +48,7 @@ typedef struct _php_sqlite3_func {
zend_fcall_info_cache func;
zend_fcall_info_cache step;
zend_fcall_info_cache fini;
+ unsigned int *in_callback_ptr;
} php_sqlite3_func;
/* Structure for SQLite collation function */
@@ -56,6 +57,7 @@ typedef struct _php_sqlite3_collation {
zend_string *collation_name;
zend_fcall_info_cache cmp_func;
+ unsigned int *in_callback_ptr;
} php_sqlite3_collation;
/* Structure for SQLite Database object. */
@@ -68,6 +70,8 @@ typedef struct _php_sqlite3_db_object {
php_sqlite3_collation *collations;
zend_fcall_info_cache authorizer_fcc;
+ unsigned int in_callback;
+
zend_llist free_list;
zend_object zo;
} php_sqlite3_db_object;
diff --git a/ext/sqlite3/sqlite3.c b/ext/sqlite3/sqlite3.c
index 45918923c1cb..4698fe5cd096 100644
--- a/ext/sqlite3/sqlite3.c
+++ b/ext/sqlite3/sqlite3.c
@@ -187,6 +187,10 @@ PHP_METHOD(SQLite3, close)
ZEND_PARSE_PARAMETERS_NONE();
if (db_obj->initialised) {
+ if (db_obj->in_callback) {
+ zend_throw_error(NULL, "Cannot close SQLite3 database while inside a callback");
+ RETURN_THROWS();
+ }
zend_llist_clean(&(db_obj->free_list));
if(db_obj->db) {
errcode = sqlite3_close(db_obj->db);
@@ -752,6 +756,9 @@ static int sqlite3_do_callback(zend_fcall_info_cache *fcc, uint32_t argc, sqlite
uint32_t fake_argc;
zend_result ret = SUCCESS;
php_sqlite3_agg_context *agg_context = NULL;
+ bool bailout = false;
+ php_sqlite3_func *cb_func = (php_sqlite3_func *)sqlite3_user_data(context);
+ unsigned int *in_callback = cb_func ? cb_func->in_callback_ptr : NULL;
if (is_agg) {
is_agg = 2;
@@ -759,6 +766,10 @@ static int sqlite3_do_callback(zend_fcall_info_cache *fcc, uint32_t argc,
sqlite
fake_argc = argc + is_agg;
+ if (in_callback) {
+ (*in_callback)++;
+ }
+
/* build up the params */
if (fake_argc) {
zargs = (zval *)safe_emalloc(fake_argc, sizeof(zval), 0);
@@ -801,7 +812,15 @@ static int sqlite3_do_callback(zend_fcall_info_cache *fcc, uint32_t argc,
sqlite
}
}
+ zend_try {
zend_call_known_fcc(fcc, &retval, fake_argc, zargs, /* named_params */ NULL);
+ } zend_catch {
+ bailout = true;
+ } zend_end_try();
+
+ if (in_callback) {
+ (*in_callback)--;
+ }
/* clean up the params */
if (is_agg) {
@@ -868,6 +887,9 @@ static int sqlite3_do_callback(zend_fcall_info_cache *fcc, uint32_t argc, sqlite
if (!Z_ISUNDEF(retval)) {
zval_ptr_dtor(&retval);
}
+ if (bailout) {
+ zend_bailout();
+ }
return ret;
}
/* }}}*/
@@ -908,6 +930,7 @@ static int php_sqlite3_callback_compare(void *coll, int a_len, const void *a, in
zval zargs[2];
zval retval;
int ret = 0;
+ bool bailout = false;
// Exception occurred on previous callback. Don't attempt to call function.
if (EG(exception)) {
@@ -917,10 +940,26 @@ static int php_sqlite3_callback_compare(void *coll, int a_len, const void *a,
in
ZVAL_STRINGL(&zargs[0], a, a_len);
ZVAL_STRINGL(&zargs[1], b, b_len);
+ if (collation->in_callback_ptr) {
+ (*collation->in_callback_ptr)++;
+ }
+
+ zend_try {
zend_call_known_fcc(&collation->cmp_func, &retval, /* argc */ 2, zargs, /* named_params
*/ NULL);
+ } zend_catch {
+ bailout = true;
+ } zend_end_try();
+
+ if (collation->in_callback_ptr) {
+ (*collation->in_callback_ptr)--;
+ }
zval_ptr_dtor(&zargs[0]);
zval_ptr_dtor(&zargs[1]);
+ if (bailout) {
+ zval_ptr_dtor(&retval);
+ zend_bailout();
+ }
if (EG(exception)) {
ret = 0;
@@ -966,6 +1005,7 @@ PHP_METHOD(SQLite3, createFunction)
}
func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
+ func->in_callback_ptr = &db_obj->in_callback;
if (sqlite3_create_function(db_obj->db, ZSTR_VAL(sql_func), sql_func_num_args, flags |
SQLITE_UTF8, func, php_sqlite3_callback_func, NULL, NULL) == SQLITE_OK) {
func->func_name = zend_string_copy(sql_func);
@@ -1014,6 +1054,7 @@ PHP_METHOD(SQLite3, createAggregate)
}
func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
+ func->in_callback_ptr = &db_obj->in_callback;
if (sqlite3_create_function(db_obj->db, ZSTR_VAL(sql_func), sql_func_num_args, SQLITE_UTF8,
func, NULL, php_sqlite3_callback_step, php_sqlite3_callback_final) == SQLITE_OK) {
func->func_name = zend_string_copy(sql_func);
@@ -1061,6 +1102,7 @@ PHP_METHOD(SQLite3, createCollation)
}
collation = (php_sqlite3_collation *)ecalloc(1, sizeof(*collation));
+ collation->in_callback_ptr = &db_obj->in_callback;
if (sqlite3_create_collation(db_obj->db, ZSTR_VAL(collation_name), SQLITE_UTF8, collation,
php_sqlite3_callback_compare) == SQLITE_OK) {
collation->collation_name = zend_string_copy(collation_name);
@@ -2222,8 +2264,15 @@ static int php_sqlite3_authorizer(void *autharg, int action, const char
*arg1, c
}
int authreturn = SQLITE_DENY;
+ bool bailout = false;
+ db_obj->in_callback++;
+ zend_try {
zend_call_known_fcc(&db_obj->authorizer_fcc, &retval, /* argc */ 5, argv, /*
named_params */ NULL);
+ } zend_catch {
+ bailout = true;
+ } zend_end_try();
+ db_obj->in_callback--;
if (Z_ISUNDEF(retval)) {
php_sqlite3_error(db_obj, 0, "An error occurred while invoking the authorizer
callback");
} else {
@@ -2246,6 +2295,9 @@ static int php_sqlite3_authorizer(void *autharg, int action, const char *arg1,
c
zval_ptr_dtor(&argv[3]);
zval_ptr_dtor(&argv[4]);
+ if (bailout) {
+ zend_bailout();
+ }
return authreturn;
}
/* }}} */
diff --git a/ext/sqlite3/tests/sqlite3_close_from_callback.phpt
b/ext/sqlite3/tests/sqlite3_close_from_callback.phpt
new file mode 100644
index 000000000000..0b5523717f76
--- /dev/null
+++ b/ext/sqlite3/tests/sqlite3_close_from_callback.phpt
@@ -0,0 +1,35 @@
+--TEST--
+SQLite3::close() from within a UDF callback must not corrupt active statement
+--EXTENSIONS--
+sqlite3
+--FILE--
+<?php
+$db = new SQLite3(':memory:');
+$db->createFunction('boom', function () use ($db) {
+ try {
+ var_dump($db->close());
+ } catch (Throwable $e) {
+ echo $e::class, ": ", $e->getMessage(), "\n";
+ }
+ return 1;
+});
+$stmt = $db->prepare('SELECT boom()');
+var_dump($stmt !== false);
+$res = $stmt->execute();
+var_dump($res !== false);
+var_dump($res->fetchArray(SQLITE3_NUM));
+$res->finalize();
+var_dump($db->close());
+echo "done\n";
+?>
+--EXPECT--
+bool(true)
+Error: Cannot close SQLite3 database while inside a callback
+bool(true)
+Error: Cannot close SQLite3 database while inside a callback
+array(1) {
+ [0]=>
+ int(1)
+}
+bool(true)
+done
diff --git a/ext/sqlite3/tests/sqlite3_close_from_callback_aggregate.phpt
b/ext/sqlite3/tests/sqlite3_close_from_callback_aggregate.phpt
new file mode 100644
index 000000000000..b5bdb9615a78
--- /dev/null
+++ b/ext/sqlite3/tests/sqlite3_close_from_callback_aggregate.phpt
@@ -0,0 +1,41 @@
+--TEST--
+SQLite3::close() from within a createAggregate() callback must not corrupt active statement
+--EXTENSIONS--
+sqlite3
+--FILE--
+<?php
+$db = new SQLite3(':memory:');
+$db->exec('CREATE TABLE t (a INTEGER)');
+$db->exec('INSERT INTO t VALUES (1)');
+$db->exec('INSERT INTO t VALUES (2)');
+
+$reported = false;
+$db->createAggregate('agg', function ($context, $rows, $a) use (&$db,
&$reported) {
+ if (!$reported) {
+ $reported = true;
+ try {
+ var_dump($db->close());
+ } catch (Throwable $e) {
+ echo $e::class, ": ", $e->getMessage(), "\n";
+ }
+ }
+ return (int) $context + $a;
+}, function ($context, $rows) {
+ return $context;
+}, 1);
+
+$stmt = $db->prepare('SELECT agg(a) FROM t');
+$res = $stmt->execute();
+var_dump($res->fetchArray(SQLITE3_NUM));
+$res->finalize();
+var_dump($db->close());
+echo "done\n";
+?>
+--EXPECT--
+Error: Cannot close SQLite3 database while inside a callback
+array(1) {
+ [0]=>
+ int(3)
+}
+bool(true)
+done
diff --git a/ext/sqlite3/tests/sqlite3_close_from_callback_authorizer.phpt
b/ext/sqlite3/tests/sqlite3_close_from_callback_authorizer.phpt
new file mode 100644
index 000000000000..1d5cf9e49227
--- /dev/null
+++ b/ext/sqlite3/tests/sqlite3_close_from_callback_authorizer.phpt
@@ -0,0 +1,38 @@
+--TEST--
+SQLite3::close() from within a setAuthorizer() callback must not finalize live statements
+--EXTENSIONS--
+sqlite3
+--FILE--
+<?php
+$db = new SQLite3(':memory:');
+$db->exec('CREATE TABLE t (a INTEGER)');
+$db->exec('INSERT INTO t VALUES (7)');
+$live = $db->prepare('SELECT a FROM t');
+
+$reported = false;
+$db->setAuthorizer(function ($action, $arg1, $arg2, $arg3, $arg4) use (&$db, &$reported)
{
+ if (!$reported) {
+ $reported = true;
+ try {
+ var_dump($db->close());
+ } catch (Throwable $e) {
+ echo $e::class, ": ", $e->getMessage(), "\n";
+ }
+ }
+ return SQLite3::OK;
+});
+
+$db->prepare('SELECT 1');
+
+$res = $live->execute();
+var_dump($res->fetchArray(SQLITE3_NUM));
+$res->finalize();
+echo "done\n";
+?>
+--EXPECT--
+Error: Cannot close SQLite3 database while inside a callback
+array(1) {
+ [0]=>
+ int(7)
+}
+done
diff --git a/ext/sqlite3/tests/sqlite3_close_from_callback_collation.phpt
b/ext/sqlite3/tests/sqlite3_close_from_callback_collation.phpt
new file mode 100644
index 000000000000..16906894161c
--- /dev/null
+++ b/ext/sqlite3/tests/sqlite3_close_from_callback_collation.phpt
@@ -0,0 +1,39 @@
+--TEST--
+SQLite3::close() from within a createCollation() callback must not corrupt active statement
+--EXTENSIONS--
+sqlite3
+--FILE--
+<?php
+$db = new SQLite3(':memory:');
+$db->exec('CREATE TABLE t (s TEXT)');
+$db->exec("INSERT INTO t VALUES ('b')");
+$db->exec("INSERT INTO t VALUES ('a')");
+
+$reported = false;
+$db->createCollation('CB', function ($x, $y) use (&$db, &$reported) {
+ if (!$reported) {
+ $reported = true;
+ try {
+ var_dump($db->close());
+ } catch (Throwable $e) {
+ echo $e::class, ": ", $e->getMessage(), "\n";
+ }
+ }
+ return strcmp($x, $y);
+});
+
+$stmt = $db->prepare('SELECT s FROM t ORDER BY s COLLATE CB');
+$res = $stmt->execute();
+while ($row = $res->fetchArray(SQLITE3_NUM)) {
+ echo $row[0], "\n";
+}
+$res->finalize();
+var_dump($db->close());
+echo "done\n";
+?>
+--EXPECT--
+Error: Cannot close SQLite3 database while inside a callback
+a
+b
+bool(true)
+done