Skip to content

Commit 59fabb0

Browse files
authored
Merge pull request #10 from jovakaloom/prevent-empty-string-panic
prevent empty string panic
2 parents fd48376 + 10b921f commit 59fabb0

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

nuodb.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,15 @@ func (stmt *Stmt) bind(args []driver.Value) error {
250250
b := []byte(v)
251251
args[i] = b // ensure the b is not GC'ed before the _bind
252252
i32 = C.int32_t(len(v))
253-
i64 = C.int64_t(uintptr(unsafe.Pointer(&b[0])))
253+
if len(b) > 0 {
254+
i64 = C.int64_t(uintptr(unsafe.Pointer(&b[0])))
255+
}
254256
case []byte:
255257
vt = C.NUODB_TYPE_BYTES
256258
i32 = C.int32_t(len(v))
257-
i64 = C.int64_t(uintptr(unsafe.Pointer(&v[0])))
259+
if len(v) > 0 {
260+
i64 = C.int64_t(uintptr(unsafe.Pointer(&v[0])))
261+
}
258262
case time.Time:
259263
vt = C.NUODB_TYPE_TIME
260264
i32 = C.int32_t(v.Nanosecond())

nuodb_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,39 @@ func TestExecAndQuery(t *testing.T) {
213213
}
214214
}
215215

216+
func TestEmptyStringAsStatementValue(t *testing.T) {
217+
db := testConn(t)
218+
defer db.Close()
219+
220+
id, ra := exec(t, db, "CREATE TABLE FooBar (id string)")
221+
if id|ra != 0 {
222+
t.Fatal(id, ra)
223+
}
224+
225+
// Insert an empty string by value alongside a control value.
226+
id, ra = exec(t, db, "INSERT INTO FooBar (id) VALUES (?),(?)", "", "control")
227+
if id|ra == 0 {
228+
t.Fatal(id, ra)
229+
}
230+
231+
// Fetch back only empty string value.
232+
rows := query(t, db, "SELECT * FROM FooBar where id = ?", "")
233+
if !rows.Next() {
234+
t.Fatal("Should have rows", rows)
235+
}
236+
values := [1]string{}
237+
if err := rows.Scan(&values[0]); err != nil {
238+
t.Fatal("Unable to scan:", err)
239+
}
240+
241+
if values != [1]string{""} {
242+
t.Fatal("Unexpected:", values)
243+
}
244+
if rows.Next() {
245+
log.Fatal("Should only have one row", rows)
246+
}
247+
}
248+
216249
func TestExecAndQueryContext(t *testing.T) {
217250
db := testConn(t)
218251
defer db.Close()

0 commit comments

Comments
 (0)