Skip to content

Commit 645c9cb

Browse files
committed
Provide SQLite with its own memory for deserialize
rqlite/rqlite#739
1 parent eeaa957 commit 645c9cb

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

sqlite3.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,10 @@ func (c *SQLiteConn) Serialize(schema string) []byte {
931931
// byte slice. If deserelization fails, error will contain the return code
932932
// of the underlying SQLite API call.
933933
//
934+
// Because the byte slice is in Go-controlled memory, this function
935+
// makes a copy of the data in C-controlled memory, before passing the
936+
// data to the SQLite library.
937+
//
934938
// See https://www.sqlite.org/c3ref/deserialize.html
935939
func (c *SQLiteConn) Deserialize(b []byte, schema string) error {
936940
if schema == "" {
@@ -941,8 +945,9 @@ func (c *SQLiteConn) Deserialize(b []byte, schema string) error {
941945
defer C.free(unsafe.Pointer(zSchema))
942946

943947
rc := C.sqlite3_deserialize(c.db, zSchema,
944-
(*C.uint8_t)(unsafe.Pointer(&b[0])),
945-
C.sqlite3_int64(len(b)), C.sqlite3_int64(len(b)), 0)
948+
(*C.uint8_t)(C.CBytes(b)),
949+
C.sqlite3_int64(len(b)), C.sqlite3_int64(len(b)),
950+
C.SQLITE_DESERIALIZE_FREEONCLOSE|C.SQLITE_DESERIALIZE_RESIZEABLE)
946951
if rc != 0 {
947952
return fmt.Errorf("deserialize failed with return %v", rc)
948953
}

0 commit comments

Comments
 (0)