Skip to content

Commit 7324bfd

Browse files
committed
Fix NUL byte truncation in sqlite3 TEXT column handling
As a bonus, this should probably also be a tad faster.
1 parent b37a6e7 commit 7324bfd

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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)