Skip to content

Commit 1044558

Browse files
committed
ext/pdo_sqlite: createCollation memory leaks fix.
coming from callback arguments when its return type is incorrect. close GH-18796
1 parent c772963 commit 1044558

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ PHP NEWS
3737
. Fixed bug #74796 (Requests through http proxy set peer name).
3838
(Jakub Zelenka)
3939

40+
- PDO Sqlite:
41+
. Fixed memory leak with Pdo_Sqlite::createCollation when the callback
42+
has an incorrect return type. (David Carlier)
43+
4044
- Phar:
4145
. Add missing filter cleanups on phar failure. (nielsdos)
4246
. Fixed bug GH-18642 (Signed integer overflow in ext/phar fseek). (nielsdos)

ext/pdo_sqlite/pdo_sqlite.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ static int php_sqlite_collation_callback(void *context, int string1_len, const v
346346

347347
zend_call_known_fcc(&collation->callback, &retval, /* argc */ 2, zargs, /* named_params */ NULL);
348348

349+
zval_ptr_dtor(&zargs[0]);
350+
zval_ptr_dtor(&zargs[1]);
351+
349352
if (!Z_ISUNDEF(retval)) {
350353
if (Z_TYPE(retval) != IS_LONG) {
351354
zend_string *func_name = get_active_function_or_method_name();
@@ -362,9 +365,6 @@ static int php_sqlite_collation_callback(void *context, int string1_len, const v
362365
}
363366
}
364367

365-
zval_ptr_dtor(&zargs[0]);
366-
zval_ptr_dtor(&zargs[1]);
367-
368368
return ret;
369369
}
370370

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Pdo\Sqlite::createCollation() memory leaks on wrong callback return type
3+
--EXTENSIONS--
4+
pdo_sqlite
5+
--FILE--
6+
<?php
7+
8+
declare(strict_types=1);
9+
10+
$db = new Pdo\Sqlite('sqlite::memory:');
11+
12+
$db->exec("CREATE TABLE test (c string)");
13+
$db->exec("INSERT INTO test VALUES('youwontseeme')");
14+
$db->exec("INSERT INTO test VALUES('neither')");
15+
$db->createCollation('NAT', function($a, $b): string { return $a . $b; });
16+
17+
try {
18+
$db->query("SELECT c FROM test ORDER BY c COLLATE NAT");
19+
} catch (\TypeError $e) {
20+
echo $e->getMessage(), PHP_EOL;
21+
}
22+
?>
23+
--EXPECT--
24+
PDO::query(): Return value of the callback must be of type int, string returned

0 commit comments

Comments
 (0)