Skip to content

Commit

Permalink
Provide SQLite with its own memory for deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
otoolep committed Jan 31, 2021
1 parent eeaa957 commit 645c9cb
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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)
}
Expand Down

0 comments on commit 645c9cb

Please sign in to comment.