|
| 1 | +--TEST-- |
| 2 | +GH-13952 (sqlite PDO::quote silently corrupts strings with null bytes) |
| 3 | +--EXTENSIONS-- |
| 4 | +pdo |
| 5 | +pdo_sqlite |
| 6 | +--FILE-- |
| 7 | +<?php |
| 8 | +$db = new \PDO('sqlite::memory:', null, null, array( |
| 9 | + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, |
| 10 | + \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, |
| 11 | + \PDO::ATTR_EMULATE_PREPARES => false, |
| 12 | +)); |
| 13 | + |
| 14 | +$test_cases = [ |
| 15 | + "", |
| 16 | + "x", |
| 17 | + "\x00", |
| 18 | + "a\x00b", |
| 19 | + "\x00\x00\x00", |
| 20 | + "foobar", |
| 21 | + "foo'''bar", |
| 22 | + "'foo'''bar'", |
| 23 | + "'foo'\x00'bar'", |
| 24 | + "foo\x00\x00\x00bar", |
| 25 | + "\x00foo\x00\x00\x00bar\x00", |
| 26 | + "\x00\x00\x00foo", |
| 27 | + "foo\x00\x00\x00", |
| 28 | +]; |
| 29 | + |
| 30 | +foreach($test_cases as $test){ |
| 31 | + $res = $db->query("SELECT " . $db->quote($test))->fetch($db::FETCH_NUM)[0] === $test; |
| 32 | + if(!$res){ |
| 33 | + throw new Exception("Failed for $test"); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +$db->exec('CREATE TABLE test (name TEXT)'); |
| 38 | + |
| 39 | +foreach ($test_cases as $test_case) { |
| 40 | + $quoted = $db->quote($test_case); |
| 41 | + echo trim(json_encode($test_case), '"'), " -> $quoted\n"; |
| 42 | + $db->exec("INSERT INTO test (name) VALUES (" . $quoted . ")"); |
| 43 | +} |
| 44 | + |
| 45 | +$stmt = $db->prepare('SELECT * from test'); |
| 46 | +$stmt->execute(); |
| 47 | +foreach ($stmt->fetchAll() as $result) { |
| 48 | + var_dump($result['name']); |
| 49 | +} |
| 50 | +?> |
| 51 | +--EXPECTF-- |
| 52 | +-> '' |
| 53 | +x -> 'x' |
| 54 | +\u0000 -> x'00' |
| 55 | +a\u0000b -> x'610062' |
| 56 | +\u0000\u0000\u0000 -> x'000000' |
| 57 | +foobar -> 'foobar' |
| 58 | +foo'''bar -> 'foo''''''bar' |
| 59 | +'foo'''bar' -> '''foo''''''bar''' |
| 60 | +'foo'\u0000'bar' -> x'27666F6F27002762617227' |
| 61 | +foo\u0000\u0000\u0000bar -> x'666F6F000000626172' |
| 62 | +\u0000foo\u0000\u0000\u0000bar\u0000 -> x'00666F6F00000062617200' |
| 63 | +\u0000\u0000\u0000foo -> x'000000666F6F' |
| 64 | +foo\u0000\u0000\u0000 -> x'666F6F000000' |
| 65 | +string(0) "" |
| 66 | +string(1) "x" |
| 67 | +string(1) "%0" |
| 68 | +string(3) "a%0b" |
| 69 | +string(3) "%0%0%0" |
| 70 | +string(6) "foobar" |
| 71 | +string(9) "foo'''bar" |
| 72 | +string(11) "'foo'''bar'" |
| 73 | +string(11) "'foo'%0'bar'" |
| 74 | +string(9) "foo%0%0%0bar" |
| 75 | +string(11) "%0foo%0%0%0bar%0" |
| 76 | +string(6) "%0%0%0foo" |
| 77 | +string(6) "foo%0%0%0" |
0 commit comments