Skip to content

Commit 9377c69

Browse files
committed
Initialize FCI/FCC
1 parent e7dc782 commit 9377c69

5 files changed

+60
-17
lines changed

ext/sqlite3/sqlite3.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -943,8 +943,8 @@ PHP_METHOD(SQLite3, createFunction)
943943
php_sqlite3_func *func;
944944
char *sql_func;
945945
size_t sql_func_len;
946-
zend_fcall_info fci;
947-
zend_fcall_info_cache fcc;
946+
zend_fcall_info fci = empty_fcall_info;
947+
zend_fcall_info_cache fcc = empty_fcall_info_cache;
948948
zend_long sql_func_num_args = -1;
949949
zend_long flags = 0;
950950
db_obj = Z_SQLITE3_DB_P(object);
@@ -989,8 +989,10 @@ PHP_METHOD(SQLite3, createAggregate)
989989
php_sqlite3_func *func;
990990
char *sql_func;
991991
size_t sql_func_len;
992-
zend_fcall_info step_fci, fini_fci;
993-
zend_fcall_info_cache step_fcc, fini_fcc;
992+
zend_fcall_info step_fci = empty_fcall_info;
993+
zend_fcall_info_cache step_fcc = empty_fcall_info_cache;
994+
zend_fcall_info fini_fci = empty_fcall_info;
995+
zend_fcall_info_cache fini_fcc = empty_fcall_info_cache;
994996
zend_long sql_func_num_args = -1;
995997
db_obj = Z_SQLITE3_DB_P(object);
996998

@@ -1026,12 +1028,8 @@ PHP_METHOD(SQLite3, createAggregate)
10261028
efree(func);
10271029

10281030
error:
1029-
if (ZEND_FCC_INITIALIZED(step_fcc)) {
1030-
zend_release_fcall_info_cache(&step_fcc);
1031-
}
1032-
if (ZEND_FCC_INITIALIZED(fini_fcc)) {
1033-
zend_release_fcall_info_cache(&fini_fcc);
1034-
}
1031+
zend_release_fcall_info_cache(&step_fcc);
1032+
zend_release_fcall_info_cache(&fini_fcc);
10351033

10361034
RETURN_FALSE;
10371035
}
@@ -1045,8 +1043,8 @@ PHP_METHOD(SQLite3, createCollation)
10451043
php_sqlite3_collation *collation;
10461044
char *collation_name;
10471045
size_t collation_name_len;
1048-
zend_fcall_info fci;
1049-
zend_fcall_info_cache fcc;
1046+
zend_fcall_info fci = empty_fcall_info;
1047+
zend_fcall_info_cache fcc = empty_fcall_info_cache;
10501048
db_obj = Z_SQLITE3_DB_P(object);
10511049

10521050
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sF", &collation_name, &collation_name_len, &fci, &fcc) == FAILURE) {
@@ -1312,16 +1310,15 @@ PHP_METHOD(SQLite3, enableExceptions)
13121310
/* {{{ Register a callback function to be used as an authorizer by SQLite. The callback should return SQLite3::OK, SQLite3::IGNORE or SQLite3::DENY. */
13131311
PHP_METHOD(SQLite3, setAuthorizer)
13141312
{
1315-
php_sqlite3_db_object *db_obj;
1316-
zval *object = ZEND_THIS;
1317-
db_obj = Z_SQLITE3_DB_P(object);
1318-
zend_fcall_info fci;
1319-
zend_fcall_info_cache fcc;
1313+
zend_fcall_info fci = empty_fcall_info;
1314+
zend_fcall_info_cache fcc = empty_fcall_info_cache;
13201315

13211316
ZEND_PARSE_PARAMETERS_START(1, 1)
13221317
Z_PARAM_FUNC_NO_TRAMPOLINE_FREE_OR_NULL(fci, fcc)
13231318
ZEND_PARSE_PARAMETERS_END();
13241319

1320+
php_sqlite3_db_object *db_obj = Z_SQLITE3_DB_P(ZEND_THIS);
1321+
13251322
SQLITE3_CHECK_INITIALIZED_FREE_TRAMPOLINE(db_obj, db_obj->initialised, SQLite3, &fcc);
13261323

13271324
/* Clear previously set callback */

ext/sqlite3/tests/sqlite3_trampoline_create_aggregate_no_leak.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ $finalize = [$o, 'finalize'];
2828

2929
var_dump($db->createAggregate('', $step, $finalize, 1));
3030

31+
try {
32+
var_dump($db->createAggregate(new stdClass(), $step, $finalize, new stdClass()));
33+
} catch (\Throwable $e) {
34+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
35+
}
3136
try {
3237
var_dump($db->createAggregate('S', $step, $finalize, new stdClass()));
3338
} catch (\Throwable $e) {
@@ -68,6 +73,7 @@ echo "Done\n";
6873
?>
6974
--EXPECT--
7075
bool(false)
76+
TypeError: SQLite3::createAggregate(): Argument #1 ($name) must be of type string, stdClass given
7177
TypeError: SQLite3::createAggregate(): Argument #4 ($argCount) must be of type int, stdClass given
7278
TypeError: SQLite3::createAggregate(): Argument #3 ($finalCallback) must be a valid callback, function "no_func" not found or invalid function name
7379
TypeError: SQLite3::createAggregate(): Argument #2 ($stepCallback) must be a valid callback, function "no_func" not found or invalid function name

ext/sqlite3/tests/sqlite3_trampoline_createcollation_no_leak.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ $o = new TrampolineTest();
1717
$callback = [$o, 'NAT'];
1818

1919
var_dump($db->createCollation('', $callback));
20+
21+
try {
22+
var_dump($db->createCollation(new stdClass(), $callback));
23+
} catch (\Throwable $e) {
24+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
25+
}
2026
try {
2127
var_dump($db->createCollation('NAT', $callback, new stdClass()));
2228
} catch (\Throwable $e) {
@@ -38,6 +44,7 @@ var_dump($db->createCollation('NAT', $callback));
3844
?>
3945
--EXPECT--
4046
bool(false)
47+
TypeError: SQLite3::createCollation(): Argument #1 ($name) must be of type string, stdClass given
4148
ArgumentCountError: SQLite3::createCollation() expects exactly 2 arguments, 3 given
4249
Invalid SQLite3 object:
4350
Error: The SQLite3 object has not been correctly initialised or is already closed

ext/sqlite3/tests/sqlite3_trampoline_createfunction_no_leak.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ $callback = [$o, 'strtoupper'];
1818

1919
var_dump($db->createfunction('', $callback));
2020

21+
try {
22+
var_dump($db->createfunction(new stdClass(), $callback, new stdClass()));
23+
} catch (\Throwable $e) {
24+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
25+
}
26+
2127
try {
2228
var_dump($db->createfunction('strtoupper', $callback, new stdClass()));
2329
} catch (\Throwable $e) {
@@ -39,6 +45,7 @@ var_dump($db->createfunction('strtoupper', $callback));
3945
?>
4046
--EXPECT--
4147
bool(false)
48+
TypeError: SQLite3::createFunction(): Argument #1 ($name) must be of type string, stdClass given
4249
TypeError: SQLite3::createFunction(): Argument #3 ($argCount) must be of type int, stdClass given
4350
Invalid SQLite3 object:
4451
Error: The SQLite3 object has not been correctly initialised or is already closed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
SQLite3 user authorizer null
3+
--EXTENSIONS--
4+
sqlite3
5+
--FILE--
6+
<?php
7+
8+
$db = new SQLite3(':memory:');
9+
$db->enableExceptions(true);
10+
11+
$db->setAuthorizer(null);
12+
13+
// This query should be accepted
14+
var_dump($db->querySingle('SELECT 1;'));
15+
16+
try {
17+
// This one should fail
18+
var_dump($db->querySingle('CREATE TABLE test (a, b);'));
19+
} catch (\Exception $e) {
20+
echo $e->getMessage() . "\n";
21+
}
22+
23+
?>
24+
--EXPECT--
25+
int(1)
26+
NULL

0 commit comments

Comments
 (0)