|
| 1 | +--TEST-- |
| 2 | +GH-13952 (sqlite PDO::quote handles null bytes correctly) |
| 3 | +--EXTENSIONS-- |
| 4 | +pdo |
| 5 | +pdo_sqlite |
| 6 | +--FILE-- |
| 7 | +<?php |
| 8 | + |
| 9 | +$modes = [ |
| 10 | + 'exception' => PDO::ERRMODE_EXCEPTION, |
| 11 | + 'warning' => PDO::ERRMODE_WARNING, |
| 12 | + 'silent' => PDO::ERRMODE_SILENT, |
| 13 | +]; |
| 14 | + |
| 15 | +$test_cases = [ |
| 16 | + "", |
| 17 | + "x", |
| 18 | + "\x00", |
| 19 | + "a\x00b", |
| 20 | + "\x00\x00\x00", |
| 21 | + "foobar", |
| 22 | + "foo'''bar", |
| 23 | + "'foo'''bar'", |
| 24 | + "foo\x00bar", |
| 25 | + "'foo'\x00'bar'", |
| 26 | + "foo\x00\x00\x00bar", |
| 27 | + "\x00foo\x00\x00\x00bar\x00", |
| 28 | + "\x00\x00\x00foo", |
| 29 | + "foo\x00\x00\x00", |
| 30 | + "\x80", // << invalid UTF-8 |
| 31 | + "\x00\x80\x00", // << invalid UTF-8 with null bytes |
| 32 | +]; |
| 33 | + |
| 34 | +foreach ($modes as $mode_name => $mode) { |
| 35 | + echo "Testing error mode: $mode_name\n"; |
| 36 | + $db = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => $mode]); |
| 37 | + |
| 38 | + foreach ($test_cases as $test) { |
| 39 | + $contains_null = str_contains($test, "\x00"); |
| 40 | + |
| 41 | + if ($mode === PDO::ERRMODE_EXCEPTION && $contains_null) { |
| 42 | + set_error_handler(fn() => throw new PDOException(), E_WARNING); |
| 43 | + try { |
| 44 | + $db->quote($test); |
| 45 | + throw new LogicException("Expected exception not thrown."); |
| 46 | + } catch (PDOException) { |
| 47 | + // expected |
| 48 | + } finally { |
| 49 | + restore_error_handler(); |
| 50 | + } |
| 51 | + } else { |
| 52 | + set_error_handler(fn() => null, E_WARNING); |
| 53 | + $quoted = $db->quote($test); |
| 54 | + restore_error_handler(); |
| 55 | + |
| 56 | + if ($contains_null) { |
| 57 | + if ($quoted !== false) { |
| 58 | + throw new LogicException("Expected false, got: " . var_export($quoted, true)); |
| 59 | + } |
| 60 | + } else { |
| 61 | + if ($quoted === false) { |
| 62 | + throw new LogicException("Unexpected false from quote()."); |
| 63 | + } |
| 64 | + $fetched = $db->query("SELECT $quoted")->fetchColumn(); |
| 65 | + if ($fetched !== $test) { |
| 66 | + throw new LogicException("Data corrupted: expected " . var_export($test, true) . " got " . var_export($fetched, true)); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +echo "ok\n"; |
| 74 | +?> |
| 75 | +--EXPECT-- |
| 76 | +Testing error mode: exception |
| 77 | +Testing error mode: warning |
| 78 | +Testing error mode: silent |
| 79 | +ok |
0 commit comments