From 645c9cbedaa190fc109e751ad6be69d580d54a92 Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Sun, 31 Jan 2021 14:13:44 -0500 Subject: [PATCH] Provide SQLite with its own memory for deserialize https://github.com/rqlite/rqlite/pull/739 --- sqlite3.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/sqlite3.go b/sqlite3.go index 61a381ae..0dadc146 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -931,6 +931,10 @@ func (c *SQLiteConn) Serialize(schema string) []byte { // byte slice. If deserelization fails, error will contain the return code // of the underlying SQLite API call. // +// Because the byte slice is in Go-controlled memory, this function +// makes a copy of the data in C-controlled memory, before passing the +// data to the SQLite library. +// // See https://www.sqlite.org/c3ref/deserialize.html func (c *SQLiteConn) Deserialize(b []byte, schema string) error { if schema == "" { @@ -941,8 +945,9 @@ func (c *SQLiteConn) Deserialize(b []byte, schema string) error { defer C.free(unsafe.Pointer(zSchema)) rc := C.sqlite3_deserialize(c.db, zSchema, - (*C.uint8_t)(unsafe.Pointer(&b[0])), - C.sqlite3_int64(len(b)), C.sqlite3_int64(len(b)), 0) + (*C.uint8_t)(C.CBytes(b)), + C.sqlite3_int64(len(b)), C.sqlite3_int64(len(b)), + C.SQLITE_DESERIALIZE_FREEONCLOSE|C.SQLITE_DESERIALIZE_RESIZEABLE) if rc != 0 { return fmt.Errorf("deserialize failed with return %v", rc) }