-
Notifications
You must be signed in to change notification settings - Fork 7.9k
SQLite pdo::quote use x'hex' syntax for null bytes #13962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
--TEST-- | ||
GH-13952 (sqlite PDO::quote silently corrupts strings with null bytes) | ||
--EXTENSIONS-- | ||
pdo | ||
pdo_sqlite | ||
--FILE-- | ||
<?php | ||
$db = new \PDO('sqlite::memory:', null, null, array( | ||
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, | ||
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, | ||
\PDO::ATTR_EMULATE_PREPARES => false, | ||
)); | ||
|
||
$test_cases = [ | ||
"", | ||
"x", | ||
"\x00", | ||
"a\x00b", | ||
"\x00\x00\x00", | ||
"foobar", | ||
"foo'''bar", | ||
"'foo'''bar'", | ||
"'foo'\x00'bar'", | ||
"foo\x00\x00\x00bar", | ||
"\x00foo\x00\x00\x00bar\x00", | ||
"\x00\x00\x00foo", | ||
"foo\x00\x00\x00", | ||
"\x80", // << invalid UTF8 | ||
"\x00\x80\x00", // << invalid UTF8 | ||
]; | ||
|
||
foreach($test_cases as $test){ | ||
$res = $db->query("SELECT " . $db->quote($test))->fetch($db::FETCH_NUM)[0] === $test; | ||
if(!$res){ | ||
throw new Exception("Failed for $test"); | ||
} | ||
} | ||
|
||
$db->exec('CREATE TABLE test (name TEXT)'); | ||
|
||
foreach ($test_cases as $test_case) { | ||
$quoted = $db->quote($test_case); | ||
echo trim(json_encode($test_case, JSON_PARTIAL_OUTPUT_ON_ERROR), '"'), " -> $quoted\n"; | ||
$db->exec("INSERT INTO test (name) VALUES (" . $quoted . ")"); | ||
} | ||
|
||
$stmt = $db->prepare('SELECT * from test'); | ||
$stmt->execute(); | ||
foreach ($stmt->fetchAll() as $result) { | ||
var_dump($result['name']); | ||
} | ||
?> | ||
--EXPECTF-- | ||
-> '' | ||
x -> 'x' | ||
\u0000 -> x'00' | ||
a\u0000b -> x'610062' | ||
\u0000\u0000\u0000 -> x'000000' | ||
foobar -> 'foobar' | ||
foo'''bar -> 'foo''''''bar' | ||
'foo'''bar' -> '''foo''''''bar''' | ||
'foo'\u0000'bar' -> x'27666F6F27002762617227' | ||
foo\u0000\u0000\u0000bar -> x'666F6F000000626172' | ||
\u0000foo\u0000\u0000\u0000bar\u0000 -> x'00666F6F00000062617200' | ||
\u0000\u0000\u0000foo -> x'000000666F6F' | ||
foo\u0000\u0000\u0000 -> x'666F6F000000' | ||
null -> '€' | ||
null -> x'008000' | ||
string(0) "" | ||
string(1) "x" | ||
string(1) "%0" | ||
string(3) "a%0b" | ||
string(3) "%0%0%0" | ||
string(6) "foobar" | ||
string(9) "foo'''bar" | ||
string(11) "'foo'''bar'" | ||
string(11) "'foo'%0'bar'" | ||
string(9) "foo%0%0%0bar" | ||
string(11) "%0foo%0%0%0bar%0" | ||
string(6) "%0%0%0foo" | ||
string(6) "foo%0%0%0" | ||
string(1) "€" | ||
string(3) "%0€%0" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quote function should not change type - https://dbfiddle.uk/ZodKR71s
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually think datatype blob is perfectly reasonable for binary strings, but okay, 3e573cf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One reason for "always text type" is as non-UTF-8 string like
0xff
is currently "text type" and not "blob type".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmmm see #13956 (comment)
there are both pros and cons with retaining the "text type" 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"nul byte" is supported in "text type" as long as you use it from php :)