Skip to content

Commit

Permalink
ext/sqlite3: Use new F ZPP modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Girgias committed Oct 6, 2023
1 parent 87715c5 commit 47eadcf
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
75 changes: 75 additions & 0 deletions ext/sqlite3/tests/sqlite3_trampoline_create_aggregate_no_leak.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
--TEST--
SQLite3::createAggregate() trampoline callback
--EXTENSIONS--
sqlite3
--FILE--
<?php

require_once(__DIR__ . '/new_db.inc');

class TrampolineTest {
public function __call(string $name, array $arguments) {
echo 'Trampoline for ', $name, PHP_EOL;
$context = $arguments[0];
if ($name === 'finalize') {
return implode(',', $context['values']);
}
if (empty($context)) {
$context = ['total' => 0, 'values' => []];
}
$context['total'] += (int) $arguments[2];
$context['values'][] = $context['total'];
return $context;
}
}
$o = new TrampolineTest();
$step = [$o, 'step'];
$finalize = [$o, 'finalize'];

echo "Creating Table\n";
var_dump($db->exec('CREATE TABLE test (a INTEGER, b INTEGER)'));

echo "INSERT into table\n";
var_dump($db->exec("INSERT INTO test (a, b) VALUES (1, -1)"));
var_dump($db->exec("INSERT INTO test (a, b) VALUES (2, -2)"));
var_dump($db->exec("INSERT INTO test (a, b) VALUES (3, -3)"));
var_dump($db->exec("INSERT INTO test (a, b) VALUES (4, -4)"));
var_dump($db->exec("INSERT INTO test (a, b) VALUES (4, -4)"));

$db->createAggregate('S', $step, $finalize, 1);

print_r($db->querySingle("SELECT S(a), S(b) FROM test", true));

echo "Closing database\n";
var_dump($db->close());
echo "Done\n";
?>
--EXPECT--
Creating Table
bool(true)
INSERT into table
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
Trampoline for step
Trampoline for step
Trampoline for step
Trampoline for step
Trampoline for step
Trampoline for step
Trampoline for step
Trampoline for step
Trampoline for step
Trampoline for step
Trampoline for finalize
Trampoline for finalize
Array
(
[S(a)] => 1,3,6,10,14
[S(b)] => -1,-3,-6,-10,-14
)
Closing database
bool(true)
Done
33 changes: 33 additions & 0 deletions ext/sqlite3/tests/sqlite3_trampoline_createcollation_no_leak.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
SQLite3::createFunction use F ZPP for trampoline callback and does not leak
--EXTENSIONS--
sqlite3
--FILE--
<?php

require_once(__DIR__ . '/new_db.inc');

class TrampolineTest {
public function __call(string $name, array $arguments) {
echo 'Trampoline for ', $name, PHP_EOL;
return strtoupper($arguments[0]);
}
}
$o = new TrampolineTest();
$callback = [$o, 'strtoupper'];

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

try {
var_dump($db->createfunction('strtoupper', $callback, new stdClass()));
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}

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

?>
--EXPECT--
bool(false)
TypeError: SQLite3::createFunction(): Argument #3 ($argCount) must be of type int, stdClass given
bool(true)
33 changes: 33 additions & 0 deletions ext/sqlite3/tests/sqlite3_trampoline_createfunction_no_leak.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
SQLite3::createFunction use F ZPP for trampoline callback and does not leak
--EXTENSIONS--
sqlite3
--FILE--
<?php

require_once(__DIR__ . '/new_db.inc');

class TrampolineTest {
public function __call(string $name, array $arguments) {
echo 'Trampoline for ', $name, PHP_EOL;
return strtoupper($arguments[0]);
}
}
$o = new TrampolineTest();
$callback = [$o, 'strtoupper'];

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

try {
var_dump($db->createfunction('strtoupper', $callback, new stdClass()));
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}

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

?>
--EXPECT--
bool(false)
TypeError: SQLite3::createFunction(): Argument #3 ($argCount) must be of type int, stdClass given
bool(true)
41 changes: 41 additions & 0 deletions ext/sqlite3/tests/sqlite3_trampoline_setauthorizer_no_leak.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
SQLite3 user authorizer trampoline callback
--EXTENSIONS--
sqlite3
--FILE--
<?php

class TrampolineTest {
public function __call(string $name, array $arguments) {
echo 'Trampoline for ', $name, PHP_EOL;
if ($arguments[0] == SQLite3::SELECT) {
return SQLite3::OK;
}

return SQLite3::DENY;
}
}
$o = new TrampolineTest();
$callback = [$o, 'authorizer'];

$db = new SQLite3(':memory:');
$db->enableExceptions(true);

$db->setAuthorizer($callback);

// This query should be accepted
var_dump($db->querySingle('SELECT 1;'));

try {
// This one should fail
var_dump($db->querySingle('CREATE TABLE test (a, b);'));
} catch (\Exception $e) {
echo $e->getMessage() . "\n";
}

?>
--EXPECT--
Trampoline for authorizer
int(1)
Trampoline for authorizer
Unable to prepare statement: not authorized

0 comments on commit 47eadcf

Please sign in to comment.