Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ext/sqlite3/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,11 @@ PHP_METHOD(SQLite3, escapeString)
RETURN_THROWS();
}

if (UNEXPECTED(zend_str_has_nul_byte(sql))) {
zend_argument_value_error(1, "must not contain null bytes");
RETURN_THROWS();
}

if (ZSTR_LEN(sql)) {
ret = sqlite3_mprintf("%q", ZSTR_VAL(sql));
if (ret) {
Expand Down
35 changes: 35 additions & 0 deletions ext/sqlite3/tests/gh_sqlite3_escapestring_nul.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
SQLite3::escapeString() rejects strings with embedded NUL bytes
--EXTENSIONS--
sqlite3
--FILE--
<?php

$cases = [
"\0",
"a\0b",
"secret\0x",
"foo\0\0bar",
];

foreach ($cases as $in) {
try {
SQLite3::escapeString($in);
echo "FAIL: no exception for ", bin2hex($in), "\n";
} catch (ValueError $e) {
echo "ValueError: ", $e->getMessage(), "\n";
}
}

echo "ok: ", SQLite3::escapeString("ok"), "\n";
echo "quote: ", SQLite3::escapeString("test''%"), "\n";
echo "empty: ", var_export(SQLite3::escapeString(""), true), "\n";
?>
--EXPECT--
ValueError: SQLite3::escapeString(): Argument #1 ($string) must not contain null bytes
ValueError: SQLite3::escapeString(): Argument #1 ($string) must not contain null bytes
ValueError: SQLite3::escapeString(): Argument #1 ($string) must not contain null bytes
ValueError: SQLite3::escapeString(): Argument #1 ($string) must not contain null bytes
ok: ok
quote: test''''%
empty: ''
Loading