From 01566134d1cdfeb92b64df529aeccadd695cefcd Mon Sep 17 00:00:00 2001 From: Sven Engelhardt Date: Fri, 2 Dec 2011 23:32:38 +0100 Subject: [PATCH] allow to insert zero-length strings into database &b[0] fails with an index out-of-bounds error for a slice with len()==0 --- sqlite3.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/sqlite3.go b/sqlite3.go index f879718..364e8c1 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -154,8 +154,13 @@ func (s *SQLiteStmt) bind(args []interface{}) error { case nil: rv = C.sqlite3_bind_null(s.s, n) case string: - b := []byte(v) - rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b))) + if len(v) == 0 { + b := []byte{0} + rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(0)) + } else { + b := []byte(v) + rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b))) + } case int: rv = C.sqlite3_bind_int(s.s, n, C.int(v)) case int64: @@ -213,7 +218,7 @@ func (s *SQLiteStmt) Exec(args []interface{}) (driver.Result, error) { if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE { return nil, errors.New(C.GoString(C.sqlite3_errmsg(s.c.db))) } - return &SQLiteResult {s}, nil + return &SQLiteResult{s}, nil } type SQLiteRows struct {