Skip to content

Commit b76c610

Browse files
bind: pass &v[0] in direct call to C
In Go 1.6, the cgo checking rules are more precise when they see an address operation as an argument to the C function. When you pass &v[0] to a C function, the cgo check just verifies that v itself does not contain any pointers. When you write `p := &v[0]` and then pass p to the C function, the cgo check is conservative: it verifies that the entire memory block to which p points does not contain any pointers. When the bind function is called by code that passes a slice that is part of a larger struct, this means that the cgo check will look at the entire larger struct, not just the slice. This can cause a surprising run time failure. Avoid this problem by rewriting the code slightly to pass &v[0] in the call to the C function itself. In particular this fixes the tests of github.com/jmoiron/sqlx when using Go 1.6.
1 parent 0cc1174 commit b76c610

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

sqlite3.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -814,11 +814,11 @@ func (s *SQLiteStmt) bind(args []driver.Value) error {
814814
case float64:
815815
rv = C.sqlite3_bind_double(s.s, n, C.double(v))
816816
case []byte:
817-
var p *byte
818-
if len(v) > 0 {
819-
p = &v[0]
817+
if len(v) == 0 {
818+
rv = C._sqlite3_bind_blob(s.s, n, nil, 0)
819+
} else {
820+
rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(&v[0]), C.int(len(v)))
820821
}
821-
rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(p), C.int(len(v)))
822822
case time.Time:
823823
b := []byte(v.Format(SQLiteTimestampFormats[0]))
824824
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))

0 commit comments

Comments
 (0)