Skip to content

Commit 6851c7b

Browse files
committed
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1: Fix GH-11878: SQLite3 callback functions cause a memory leak with a callable array
2 parents 8fcffa6 + 07a9d2f commit 6851c7b

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ PHP NEWS
3939
. Fixed bug GH-11972 (RecursiveCallbackFilterIterator regression in 8.1.18).
4040
(nielsdos)
4141

42+
- SQLite3:
43+
. Fixed bug GH-11878 (SQLite3 callback functions cause a memory leak with
44+
a callable array). (nielsdos, arnaud-lb)
45+
4246
31 Aug 2023, PHP 8.2.10
4347

4448
- CLI:

ext/sqlite3/sqlite3.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,6 +2229,42 @@ static void php_sqlite3_object_free_storage(zend_object *object) /* {{{ */
22292229
}
22302230
/* }}} */
22312231

2232+
static HashTable *php_sqlite3_get_gc(zend_object *object, zval **table, int *n)
2233+
{
2234+
php_sqlite3_db_object *intern = php_sqlite3_db_from_obj(object);
2235+
2236+
if (intern->funcs == NULL && intern->collations == NULL) {
2237+
/* Fast path without allocations */
2238+
*table = NULL;
2239+
*n = 0;
2240+
return zend_std_get_gc(object, table, n);
2241+
} else {
2242+
zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
2243+
2244+
php_sqlite3_func *func = intern->funcs;
2245+
while (func != NULL) {
2246+
zend_get_gc_buffer_add_zval(gc_buffer, &func->func);
2247+
zend_get_gc_buffer_add_zval(gc_buffer, &func->step);
2248+
zend_get_gc_buffer_add_zval(gc_buffer, &func->fini);
2249+
func = func->next;
2250+
}
2251+
2252+
php_sqlite3_collation *collation = intern->collations;
2253+
while (collation != NULL) {
2254+
zend_get_gc_buffer_add_zval(gc_buffer, &collation->cmp_func);
2255+
collation = collation->next;
2256+
}
2257+
2258+
zend_get_gc_buffer_use(gc_buffer, table, n);
2259+
2260+
if (object->properties == NULL && object->ce->default_properties_count == 0) {
2261+
return NULL;
2262+
} else {
2263+
return zend_std_get_properties(object);
2264+
}
2265+
}
2266+
}
2267+
22322268
static void php_sqlite3_stmt_object_free_storage(zend_object *object) /* {{{ */
22332269
{
22342270
php_sqlite3_stmt *intern = php_sqlite3_stmt_from_obj(object);
@@ -2364,6 +2400,7 @@ PHP_MINIT_FUNCTION(sqlite3)
23642400
sqlite3_object_handlers.offset = XtOffsetOf(php_sqlite3_db_object, zo);
23652401
sqlite3_object_handlers.clone_obj = NULL;
23662402
sqlite3_object_handlers.free_obj = php_sqlite3_object_free_storage;
2403+
sqlite3_object_handlers.get_gc = php_sqlite3_get_gc;
23672404
php_sqlite3_sc_entry = register_class_SQLite3();
23682405
php_sqlite3_sc_entry->create_object = php_sqlite3_object_new;
23692406

ext/sqlite3/tests/gh11878.phpt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
GH-11878 (SQLite3 callback functions cause a memory leak with a callable array)
3+
--EXTENSIONS--
4+
sqlite3
5+
--FILE--
6+
<?php
7+
class Foo {
8+
public $sqlite;
9+
public function __construct(bool $normalFunctions, bool $aggregates) {
10+
$this->sqlite = new SQLite3(":memory:");
11+
if ($aggregates) {
12+
$this->sqlite->createAggregate("indexes", array($this, "SQLiteIndex"), array($this, "SQLiteFinal"), 0);
13+
}
14+
if ($normalFunctions) {
15+
$this->sqlite->createFunction("func", array($this, "SQLiteIndex"), 0);
16+
$this->sqlite->createCollation("collation", array($this, "SQLiteIndex"));
17+
}
18+
}
19+
public function SQLiteIndex() {}
20+
public function SQLiteFinal() {}
21+
}
22+
23+
// Test different combinations to check for null pointer derefs
24+
$x = new Foo(true, true);
25+
$y = new Foo(false, true);
26+
$z = new Foo(true, false);
27+
$w = new Foo(false, false);
28+
?>
29+
Done
30+
--EXPECT--
31+
Done

0 commit comments

Comments
 (0)