Skip to content

Commit 2b780b4

Browse files
fix idxStr freeing issue (#898)
uses snippet suggested by @rittneje #897 (comment)
1 parent 98d34f9 commit 2b780b4

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

sqlite3_opt_vtable.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,21 @@ func goVBestIndex(pVTab unsafe.Pointer, icp unsafe.Pointer) *C.char {
472472
}
473473

474474
info.idxNum = C.int(res.IdxNum)
475-
idxStr := C.CString(res.IdxStr)
476-
defer C.free(unsafe.Pointer(idxStr))
477-
info.idxStr = idxStr
478-
info.needToFreeIdxStr = C.int(0)
475+
info.idxStr = (*C.char)(C.sqlite3_malloc(C.int(len(res.IdxStr) + 1)))
476+
if info.idxStr == nil {
477+
// C.malloc and C.CString ordinarily do this for you. See https://golang.org/cmd/cgo/
478+
panic("out of memory")
479+
}
480+
info.needToFreeIdxStr = C.int(1)
481+
482+
idxStr := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
483+
Data: uintptr(unsafe.Pointer(info.idxStr)),
484+
Len: len(res.IdxStr) + 1,
485+
Cap: len(res.IdxStr) + 1,
486+
}))
487+
copy(idxStr, res.IdxStr)
488+
idxStr[len(idxStr)-1] = 0 // null-terminated string
489+
479490
if res.AlreadyOrdered {
480491
info.orderByConsumed = C.int(1)
481492
}

0 commit comments

Comments
 (0)