diff --git a/system/Database/SQLite3/Connection.php b/system/Database/SQLite3/Connection.php index d134f6eec8dd..4e2e5ffd70fe 100644 --- a/system/Database/SQLite3/Connection.php +++ b/system/Database/SQLite3/Connection.php @@ -190,7 +190,10 @@ protected function _escapeString(string $str): string $this->initialize(); } - return $this->connID->escapeString($str); + // SQLite3::escapeString() cannot handle NUL bytes: it silently + // truncated the string before PHP 8.6 and throws a ValueError since + // PHP 8.6. Escape the quotes ourselves so binary data is preserved. + return str_replace("'", "''", $str); } protected function _listTables(bool $prefixLimit = false, ?string $tableName = null): string diff --git a/tests/system/Database/Live/EscapeTest.php b/tests/system/Database/Live/EscapeTest.php index b3c27601ac42..df617a09dd1d 100644 --- a/tests/system/Database/Live/EscapeTest.php +++ b/tests/system/Database/Live/EscapeTest.php @@ -129,4 +129,14 @@ public function testEscapeStringArray(): void $this->assertSame('NULL', $escapedString[3]); } + + public function testNullBytesAreKept(): void + { + if ($this->db->DBDriver !== 'SQLite3') { + $this->markTestSkipped('Only the SQLite3 driver is affected.'); + } + + $this->assertSame("a\0b", $this->db->escapeString("a\0b")); + $this->assertSame("a''b\0c", $this->db->escapeString("a'b\0c")); + } }