forked from mirror/go-sqlite3
fix idxStr freeing issue (#898)
uses snippet suggested by @rittneje https://github.com/mattn/go-sqlite3/issues/897#issuecomment-752162125
This commit is contained in:
parent
98d34f9dc5
commit
2b780b4a7f
|
@ -472,10 +472,21 @@ func goVBestIndex(pVTab unsafe.Pointer, icp unsafe.Pointer) *C.char {
|
||||||
}
|
}
|
||||||
|
|
||||||
info.idxNum = C.int(res.IdxNum)
|
info.idxNum = C.int(res.IdxNum)
|
||||||
idxStr := C.CString(res.IdxStr)
|
info.idxStr = (*C.char)(C.sqlite3_malloc(C.int(len(res.IdxStr) + 1)))
|
||||||
defer C.free(unsafe.Pointer(idxStr))
|
if info.idxStr == nil {
|
||||||
info.idxStr = idxStr
|
// C.malloc and C.CString ordinarily do this for you. See https://golang.org/cmd/cgo/
|
||||||
info.needToFreeIdxStr = C.int(0)
|
panic("out of memory")
|
||||||
|
}
|
||||||
|
info.needToFreeIdxStr = C.int(1)
|
||||||
|
|
||||||
|
idxStr := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||||
|
Data: uintptr(unsafe.Pointer(info.idxStr)),
|
||||||
|
Len: len(res.IdxStr) + 1,
|
||||||
|
Cap: len(res.IdxStr) + 1,
|
||||||
|
}))
|
||||||
|
copy(idxStr, res.IdxStr)
|
||||||
|
idxStr[len(idxStr)-1] = 0 // null-terminated string
|
||||||
|
|
||||||
if res.AlreadyOrdered {
|
if res.AlreadyOrdered {
|
||||||
info.orderByConsumed = C.int(1)
|
info.orderByConsumed = C.int(1)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue