Skip to content

Fix GH-11878: SQLite3 callback functions cause a memory leak with a callable array #11881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions ext/sqlite3/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -2194,6 +2194,42 @@ static void php_sqlite3_object_free_storage(zend_object *object) /* {{{ */
}
/* }}} */

static HashTable *php_sqlite3_get_gc(zend_object *object, zval **table, int *n)
{
php_sqlite3_db_object *intern = php_sqlite3_db_from_obj(object);

if (intern->funcs == NULL && intern->collations == NULL) {
/* Fast path without allocations */
*table = NULL;
*n = 0;
return zend_std_get_gc(object, table, n);
} else {
zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();

php_sqlite3_func *func = intern->funcs;
while (func != NULL) {
zend_get_gc_buffer_add_zval(gc_buffer, &func->func);
zend_get_gc_buffer_add_zval(gc_buffer, &func->step);
zend_get_gc_buffer_add_zval(gc_buffer, &func->fini);
func = func->next;
}

php_sqlite3_collation *collation = intern->collations;
while (collation != NULL) {
zend_get_gc_buffer_add_zval(gc_buffer, &collation->cmp_func);
collation = collation->next;
}

zend_get_gc_buffer_use(gc_buffer, table, n);

if (object->properties == NULL && object->ce->default_properties_count == 0) {
return NULL;
} else {
return zend_std_get_properties(object);
}
}
}

static void php_sqlite3_stmt_object_free_storage(zend_object *object) /* {{{ */
{
php_sqlite3_stmt *intern = php_sqlite3_stmt_from_obj(object);
Expand Down Expand Up @@ -2327,6 +2363,7 @@ PHP_MINIT_FUNCTION(sqlite3)
sqlite3_object_handlers.offset = XtOffsetOf(php_sqlite3_db_object, zo);
sqlite3_object_handlers.clone_obj = NULL;
sqlite3_object_handlers.free_obj = php_sqlite3_object_free_storage;
sqlite3_object_handlers.get_gc = php_sqlite3_get_gc;
php_sqlite3_sc_entry = register_class_SQLite3();
php_sqlite3_sc_entry->create_object = php_sqlite3_object_new;

Expand Down
31 changes: 31 additions & 0 deletions ext/sqlite3/tests/gh11878.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
GH-11878 (SQLite3 callback functions cause a memory leak with a callable array)
--EXTENSIONS--
sqlite3
--FILE--
<?php
class Foo {
public $sqlite;
public function __construct(bool $normalFunctions, bool $aggregates) {
$this->sqlite = new SQLite3(":memory:");
if ($aggregates) {
$this->sqlite->createAggregate("indexes", array($this, "SQLiteIndex"), array($this, "SQLiteFinal"), 0);
}
if ($normalFunctions) {
$this->sqlite->createFunction("func", array($this, "SQLiteIndex"), 0);
$this->sqlite->createCollation("collation", array($this, "SQLiteIndex"));
}
}
public function SQLiteIndex() {}
public function SQLiteFinal() {}
}

// Test different combinations to check for null pointer derefs
$x = new Foo(true, true);
$y = new Foo(false, true);
$z = new Foo(true, false);
$w = new Foo(false, false);
?>
Done
--EXPECT--
Done