Skip to content

Commit 07a9d2f

Browse files
committed
Fix GH-11878: SQLite3 callback functions cause a memory leak with a callable array
In this test file, the free_obj handler is called with a refcount of 2, caused by the fact we do a GC_ADDREF() to increase its refcount while its refcount is still 1 because the Foo object hasn't been destroyed yet (due to the cycle caused by the sqlite function callback). Solve this by introducing a get_gc handler. Closes GH-11881.
1 parent be0245c commit 07a9d2f

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ PHP NEWS
3737
. Fixed bug GH-11972 (RecursiveCallbackFilterIterator regression in 8.1.18).
3838
(nielsdos)
3939

40+
- SQLite3:
41+
. Fixed bug GH-11878 (SQLite3 callback functions cause a memory leak with
42+
a callable array). (nielsdos, arnaud-lb)
43+
4044
31 Aug 2023, PHP 8.1.23
4145

4246
- CLI:

ext/sqlite3/sqlite3.c

+37
Original file line numberDiff line numberDiff line change
@@ -2194,6 +2194,42 @@ static void php_sqlite3_object_free_storage(zend_object *object) /* {{{ */
21942194
}
21952195
/* }}} */
21962196

2197+
static HashTable *php_sqlite3_get_gc(zend_object *object, zval **table, int *n)
2198+
{
2199+
php_sqlite3_db_object *intern = php_sqlite3_db_from_obj(object);
2200+
2201+
if (intern->funcs == NULL && intern->collations == NULL) {
2202+
/* Fast path without allocations */
2203+
*table = NULL;
2204+
*n = 0;
2205+
return zend_std_get_gc(object, table, n);
2206+
} else {
2207+
zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
2208+
2209+
php_sqlite3_func *func = intern->funcs;
2210+
while (func != NULL) {
2211+
zend_get_gc_buffer_add_zval(gc_buffer, &func->func);
2212+
zend_get_gc_buffer_add_zval(gc_buffer, &func->step);
2213+
zend_get_gc_buffer_add_zval(gc_buffer, &func->fini);
2214+
func = func->next;
2215+
}
2216+
2217+
php_sqlite3_collation *collation = intern->collations;
2218+
while (collation != NULL) {
2219+
zend_get_gc_buffer_add_zval(gc_buffer, &collation->cmp_func);
2220+
collation = collation->next;
2221+
}
2222+
2223+
zend_get_gc_buffer_use(gc_buffer, table, n);
2224+
2225+
if (object->properties == NULL && object->ce->default_properties_count == 0) {
2226+
return NULL;
2227+
} else {
2228+
return zend_std_get_properties(object);
2229+
}
2230+
}
2231+
}
2232+
21972233
static void php_sqlite3_stmt_object_free_storage(zend_object *object) /* {{{ */
21982234
{
21992235
php_sqlite3_stmt *intern = php_sqlite3_stmt_from_obj(object);
@@ -2327,6 +2363,7 @@ PHP_MINIT_FUNCTION(sqlite3)
23272363
sqlite3_object_handlers.offset = XtOffsetOf(php_sqlite3_db_object, zo);
23282364
sqlite3_object_handlers.clone_obj = NULL;
23292365
sqlite3_object_handlers.free_obj = php_sqlite3_object_free_storage;
2366+
sqlite3_object_handlers.get_gc = php_sqlite3_get_gc;
23302367
php_sqlite3_sc_entry = register_class_SQLite3();
23312368
php_sqlite3_sc_entry->create_object = php_sqlite3_object_new;
23322369

ext/sqlite3/tests/gh11878.phpt

+31
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)