Skip to content

Commit 47eadcf

Browse files
committed
ext/sqlite3: Use new F ZPP modifier
1 parent 87715c5 commit 47eadcf

4 files changed

+182
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
--TEST--
2+
SQLite3::createAggregate() trampoline callback
3+
--EXTENSIONS--
4+
sqlite3
5+
--FILE--
6+
<?php
7+
8+
require_once(__DIR__ . '/new_db.inc');
9+
10+
class TrampolineTest {
11+
public function __call(string $name, array $arguments) {
12+
echo 'Trampoline for ', $name, PHP_EOL;
13+
$context = $arguments[0];
14+
if ($name === 'finalize') {
15+
return implode(',', $context['values']);
16+
}
17+
if (empty($context)) {
18+
$context = ['total' => 0, 'values' => []];
19+
}
20+
$context['total'] += (int) $arguments[2];
21+
$context['values'][] = $context['total'];
22+
return $context;
23+
}
24+
}
25+
$o = new TrampolineTest();
26+
$step = [$o, 'step'];
27+
$finalize = [$o, 'finalize'];
28+
29+
echo "Creating Table\n";
30+
var_dump($db->exec('CREATE TABLE test (a INTEGER, b INTEGER)'));
31+
32+
echo "INSERT into table\n";
33+
var_dump($db->exec("INSERT INTO test (a, b) VALUES (1, -1)"));
34+
var_dump($db->exec("INSERT INTO test (a, b) VALUES (2, -2)"));
35+
var_dump($db->exec("INSERT INTO test (a, b) VALUES (3, -3)"));
36+
var_dump($db->exec("INSERT INTO test (a, b) VALUES (4, -4)"));
37+
var_dump($db->exec("INSERT INTO test (a, b) VALUES (4, -4)"));
38+
39+
$db->createAggregate('S', $step, $finalize, 1);
40+
41+
print_r($db->querySingle("SELECT S(a), S(b) FROM test", true));
42+
43+
echo "Closing database\n";
44+
var_dump($db->close());
45+
echo "Done\n";
46+
?>
47+
--EXPECT--
48+
Creating Table
49+
bool(true)
50+
INSERT into table
51+
bool(true)
52+
bool(true)
53+
bool(true)
54+
bool(true)
55+
bool(true)
56+
Trampoline for step
57+
Trampoline for step
58+
Trampoline for step
59+
Trampoline for step
60+
Trampoline for step
61+
Trampoline for step
62+
Trampoline for step
63+
Trampoline for step
64+
Trampoline for step
65+
Trampoline for step
66+
Trampoline for finalize
67+
Trampoline for finalize
68+
Array
69+
(
70+
[S(a)] => 1,3,6,10,14
71+
[S(b)] => -1,-3,-6,-10,-14
72+
)
73+
Closing database
74+
bool(true)
75+
Done
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
SQLite3::createFunction use F ZPP for trampoline callback and does not leak
3+
--EXTENSIONS--
4+
sqlite3
5+
--FILE--
6+
<?php
7+
8+
require_once(__DIR__ . '/new_db.inc');
9+
10+
class TrampolineTest {
11+
public function __call(string $name, array $arguments) {
12+
echo 'Trampoline for ', $name, PHP_EOL;
13+
return strtoupper($arguments[0]);
14+
}
15+
}
16+
$o = new TrampolineTest();
17+
$callback = [$o, 'strtoupper'];
18+
19+
var_dump($db->createfunction('', $callback));
20+
21+
try {
22+
var_dump($db->createfunction('strtoupper', $callback, new stdClass()));
23+
} catch (\Throwable $e) {
24+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
25+
}
26+
27+
var_dump($db->createfunction('strtoupper', $callback));
28+
29+
?>
30+
--EXPECT--
31+
bool(false)
32+
TypeError: SQLite3::createFunction(): Argument #3 ($argCount) must be of type int, stdClass given
33+
bool(true)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
SQLite3::createFunction use F ZPP for trampoline callback and does not leak
3+
--EXTENSIONS--
4+
sqlite3
5+
--FILE--
6+
<?php
7+
8+
require_once(__DIR__ . '/new_db.inc');
9+
10+
class TrampolineTest {
11+
public function __call(string $name, array $arguments) {
12+
echo 'Trampoline for ', $name, PHP_EOL;
13+
return strtoupper($arguments[0]);
14+
}
15+
}
16+
$o = new TrampolineTest();
17+
$callback = [$o, 'strtoupper'];
18+
19+
var_dump($db->createfunction('', $callback));
20+
21+
try {
22+
var_dump($db->createfunction('strtoupper', $callback, new stdClass()));
23+
} catch (\Throwable $e) {
24+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
25+
}
26+
27+
var_dump($db->createfunction('strtoupper', $callback));
28+
29+
?>
30+
--EXPECT--
31+
bool(false)
32+
TypeError: SQLite3::createFunction(): Argument #3 ($argCount) must be of type int, stdClass given
33+
bool(true)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
SQLite3 user authorizer trampoline callback
3+
--EXTENSIONS--
4+
sqlite3
5+
--FILE--
6+
<?php
7+
8+
class TrampolineTest {
9+
public function __call(string $name, array $arguments) {
10+
echo 'Trampoline for ', $name, PHP_EOL;
11+
if ($arguments[0] == SQLite3::SELECT) {
12+
return SQLite3::OK;
13+
}
14+
15+
return SQLite3::DENY;
16+
}
17+
}
18+
$o = new TrampolineTest();
19+
$callback = [$o, 'authorizer'];
20+
21+
$db = new SQLite3(':memory:');
22+
$db->enableExceptions(true);
23+
24+
$db->setAuthorizer($callback);
25+
26+
// This query should be accepted
27+
var_dump($db->querySingle('SELECT 1;'));
28+
29+
try {
30+
// This one should fail
31+
var_dump($db->querySingle('CREATE TABLE test (a, b);'));
32+
} catch (\Exception $e) {
33+
echo $e->getMessage() . "\n";
34+
}
35+
36+
?>
37+
--EXPECT--
38+
Trampoline for authorizer
39+
int(1)
40+
Trampoline for authorizer
41+
Unable to prepare statement: not authorized

0 commit comments

Comments
 (0)