Skip to content
Merged
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
4 changes: 3 additions & 1 deletion sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,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":
Expand Down
39 changes: 39 additions & 0 deletions sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -945,3 +945,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")
}
}