Skip to content

Commit 0d7e535

Browse files
committed
Fix NUL byte truncation in sqlite3 TEXT column handling
As a bonus, this should probably also be a tad faster. Closes GH-20704.
1 parent 2ef60d0 commit 0d7e535

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ PHP NEWS
6060
. DirectoryIterator key can now work better with filesystem supporting larger
6161
directory indexing. (David Carlier)
6262

63+
- Sqlite3:
64+
. Fix NUL byte truncation in sqlite3 TEXT column handling. (ndossche)
65+
6366
- Standard:
6467
. Fixed bug GH-19926 (reset internal pointer earlier while splicing array
6568
while COW violation flag is still set). (alexandre-daubois)

ext/sqlite3/sqlite3.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ static void sqlite_value_to_zval(sqlite3_stmt *stmt, int column, zval *data) /*
648648
break;
649649

650650
case SQLITE3_TEXT:
651-
ZVAL_STRING(data, (char*)sqlite3_column_text(stmt, column));
651+
ZVAL_STRINGL(data, (const char *) sqlite3_column_text(stmt, column), sqlite3_column_bytes(stmt, column));
652652
break;
653653

654654
case SQLITE_BLOB:
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
Text column with NUL bytes
3+
--EXTENSIONS--
4+
sqlite3
5+
--FILE--
6+
<?php
7+
$db = new SQLite3(':memory:');
8+
9+
$db->exec(
10+
'CREATE TABLE messages (
11+
content TEXT
12+
)'
13+
);
14+
15+
$insert = $db->prepare(
16+
'INSERT INTO messages (content) VALUES (:content)'
17+
);
18+
19+
$insert->bindValue(':content', "with\0null", SQLITE3_TEXT);
20+
$insert->execute();
21+
$insert->bindValue(':content', "\0", SQLITE3_TEXT);
22+
$insert->execute();
23+
24+
$result = $db->query('SELECT * FROM messages');
25+
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
26+
var_dump($row);
27+
}
28+
29+
?>
30+
--EXPECTF--
31+
array(1) {
32+
["content"]=>
33+
string(9) "with%0null"
34+
}
35+
array(1) {
36+
["content"]=>
37+
string(1) "%0"
38+
}

0 commit comments

Comments
 (0)