From 568d682daaf9c9eb2cd153afe7c28e92156f6b7a Mon Sep 17 00:00:00 2001 From: Frederick Akalin Date: Sat, 22 Sep 2018 11:20:12 -0700 Subject: [PATCH 1/3] Add failing test --- sqlite3_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/sqlite3_test.go b/sqlite3_test.go index bfed027..7b16342 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -1770,6 +1770,7 @@ var tests = []testing.InternalTest{ {Name: "TestResult", F: testResult}, {Name: "TestBlobs", F: testBlobs}, {Name: "TestMultiBlobs", F: testMultiBlobs}, + {Name: "TestNullZeroLengthBlobs", F: testNullZeroLengthBlobs}, {Name: "TestManyQueryRow", F: testManyQueryRow}, {Name: "TestTxQuery", F: testTxQuery}, {Name: "TestPreparedStmt", F: testPreparedStmt}, @@ -1975,6 +1976,34 @@ func testMultiBlobs(t *testing.T) { } } +// testBlobs tests that we distinguish between null and zero-length blobs +func testNullZeroLengthBlobs(t *testing.T) { + db.tearDown() + db.mustExec("create table foo (id integer primary key, bar " + db.blobType(16) + ")") + db.mustExec(db.q("insert into foo (id, bar) values(?,?)"), 0, nil) + db.mustExec(db.q("insert into foo (id, bar) values(?,?)"), 1, []byte{}) + + r0 := db.QueryRow(db.q("select bar from foo where id=0")) + var b0 []byte + err := r0.Scan(&b0) + if err != nil { + t.Fatal(err) + } + if b0 != nil { + t.Errorf("for id=0, got %x; want nil", b0) + } + + r1 := db.QueryRow(db.q("select bar from foo where id=1")) + var b1 []byte + err = r1.Scan(&b1) + if err != nil { + t.Fatal(err) + } + if b1 == nil || len(b1) > 0 { + t.Errorf("for id=1, got nil; want zero-length slice") + } +} + // testManyQueryRow is test for many query row func testManyQueryRow(t *testing.T) { if testing.Short() { From d61fdcd55b4fe42665f3b5c1cf8cd8a784da3716 Mon Sep 17 00:00:00 2001 From: Frederick Akalin Date: Sat, 22 Sep 2018 11:21:59 -0700 Subject: [PATCH 2/3] Clean up test --- sqlite3_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sqlite3_test.go b/sqlite3_test.go index 7b16342..309c2c3 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -1999,8 +1999,10 @@ func testNullZeroLengthBlobs(t *testing.T) { if err != nil { t.Fatal(err) } - if b1 == nil || len(b1) > 0 { - t.Errorf("for id=1, got nil; want zero-length slice") + if b1 == nil { + t.Error("for id=1, got nil; want zero-length slice") + } else if len(b1) > 0 { + t.Errorf("for id=1, got %x; want zero-length slice", b1) } } From ab4f1745f3ee0773f85605b6dfe45ced25d0274d Mon Sep 17 00:00:00 2001 From: Frederick Akalin Date: Sat, 22 Sep 2018 11:23:19 -0700 Subject: [PATCH 3/3] Fix bug --- sqlite3.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlite3.go b/sqlite3.go index b17e634..f29be13 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -1987,7 +1987,7 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error { case C.SQLITE_BLOB: p := C.sqlite3_column_blob(rc.s.s, C.int(i)) if p == nil { - dest[i] = nil + dest[i] = []byte{} continue } n := int(C.sqlite3_column_bytes(rc.s.s, C.int(i)))