aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--sqlite3.go4
-rw-r--r--sqlite3_test.go39
2 files changed, 42 insertions, 1 deletions
diff --git a/sqlite3.go b/sqlite3.go
index c7a87b7..5620b88 100644
--- a/sqlite3.go
+++ b/sqlite3.go
@@ -643,7 +643,9 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
case C.SQLITE_TEXT:
var err error
var timeVal time.Time
- s := C.GoString((*C.char)(unsafe.Pointer(C.sqlite3_column_text(rc.s.s, C.int(i)))))
+
+ n := int(C.sqlite3_column_bytes(rc.s.s, C.int(i)))
+ s := C.GoStringN((*C.char)(unsafe.Pointer(C.sqlite3_column_text(rc.s.s, C.int(i)))), C.int(n))
switch rc.decltype[i] {
case "timestamp", "datetime", "date":
diff --git a/sqlite3_test.go b/sqlite3_test.go
index deb9706..74cafa8 100644
--- a/sqlite3_test.go
+++ b/sqlite3_test.go
@@ -979,3 +979,42 @@ func TestNumberNamedParams(t *testing.T) {
t.Error("Failed to db.QueryRow: not matched results")
}
}
+
+func TestStringContainingZero(t *testing.T) {
+ tempFilename := TempFilename()
+ db, err := sql.Open("sqlite3", tempFilename)
+ if err != nil {
+ t.Fatal("Failed to open database:", err)
+ }
+ defer os.Remove(tempFilename)
+ defer db.Close()
+
+ _, err = db.Exec(`
+ create table foo (id integer, name, extra text);
+ `)
+ if err != nil {
+ t.Error("Failed to call db.Query:", err)
+ }
+
+ const text = "foo\x00bar"
+
+ _, err = db.Exec(`insert into foo(id, name, extra) values($1, $2, $2)`, 1, text)
+ if err != nil {
+ t.Error("Failed to call db.Exec:", err)
+ }
+
+ row := db.QueryRow(`select id, extra from foo where id = $1 and extra = $2`, 1, text)
+ if row == nil {
+ t.Error("Failed to call db.QueryRow")
+ }
+
+ var id int
+ var extra string
+ err = row.Scan(&id, &extra)
+ if err != nil {
+ t.Error("Failed to db.Scan:", err)
+ }
+ if id != 1 || extra != text {
+ t.Error("Failed to db.QueryRow: not matched results")
+ }
+}