File size: 2,218 Bytes
1e94262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
--TEST--
GH-13952 (sqlite PDO::quote handles null bytes correctly)
--EXTENSIONS--
pdo
pdo_sqlite
--FILE--
<?php

$modes = [
    'exception' => PDO::ERRMODE_EXCEPTION,
    'warning'   => PDO::ERRMODE_WARNING,
    'silent'    => PDO::ERRMODE_SILENT,
];

$test_cases = [
    "",
    "x",
    "\x00",
    "a\x00b",
    "\x00\x00\x00",
    "foobar",
    "foo'''bar",
    "'foo'''bar'",
    "foo\x00bar",
    "'foo'\x00'bar'",
    "foo\x00\x00\x00bar",
    "\x00foo\x00\x00\x00bar\x00",
    "\x00\x00\x00foo",
    "foo\x00\x00\x00",
    "\x80", // << invalid UTF-8
    "\x00\x80\x00", // << invalid UTF-8 with null bytes
];

foreach ($modes as $mode_name => $mode) {
    echo "Testing error mode: $mode_name\n";
    $db = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => $mode]);

    foreach ($test_cases as $test) {
        $contains_null = str_contains($test, "\x00");

        if ($mode === PDO::ERRMODE_EXCEPTION && $contains_null) {
            set_error_handler(fn() => throw new PDOException(), E_WARNING);
            try {
                $db->quote($test);
                throw new LogicException("Expected exception not thrown.");
            } catch (PDOException) {
                // expected
            } finally {
                restore_error_handler();
            }
        } else {
            set_error_handler(fn() => null, E_WARNING);
            $quoted = $db->quote($test);
            restore_error_handler();

            if ($contains_null) {
                if ($quoted !== false) {
                    throw new LogicException("Expected false, got: " . var_export($quoted, true));
                }
            } else {
                if ($quoted === false) {
                    throw new LogicException("Unexpected false from quote().");
                }
                $fetched = $db->query("SELECT $quoted")->fetchColumn();
                if ($fetched !== $test) {
                    throw new LogicException("Data corrupted: expected " . var_export($test, true) . " got " . var_export($fetched, true));
                }
            }
        }
    }
}

echo "ok\n";
?>
--EXPECT--
Testing error mode: exception
Testing error mode: warning
Testing error mode: silent
ok