Skip to content

Commit 92b3172

Browse files
committed
use unsafe.Slice
1 parent 8366a00 commit 92b3172

File tree

1 file changed

+2
-14
lines changed

1 file changed

+2
-14
lines changed

sqlite3_opt_serialize.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import "C"
1717
import (
1818
"fmt"
1919
"math"
20-
"reflect"
2120
"unsafe"
2221
)
2322

@@ -43,14 +42,8 @@ func (c *SQLiteConn) Serialize(schema string) ([]byte, error) {
4342
return nil, fmt.Errorf("serialized database is too large (%d bytes)", sz)
4443
}
4544

46-
cBuf := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
47-
Data: uintptr(unsafe.Pointer(ptr)),
48-
Len: int(sz),
49-
Cap: int(sz),
50-
}))
51-
5245
res := make([]byte, int(sz))
53-
copy(res, cBuf)
46+
copy(res, unsafe.Slice((*byte)(unsafe.Pointer(ptr)), int(sz)))
5447
return res, nil
5548
}
5649

@@ -67,12 +60,7 @@ func (c *SQLiteConn) Deserialize(b []byte, schema string) error {
6760
defer C.free(unsafe.Pointer(zSchema))
6861

6962
tmpBuf := (*C.uchar)(C.sqlite3_malloc64(C.sqlite3_uint64(len(b))))
70-
cBuf := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
71-
Data: uintptr(unsafe.Pointer(tmpBuf)),
72-
Len: len(b),
73-
Cap: len(b),
74-
}))
75-
copy(cBuf, b)
63+
copy(unsafe.Slice((*byte)(unsafe.Pointer(tmpBuf)), len(b)), b)
7664

7765
rc := C.sqlite3_deserialize(c.db, zSchema, tmpBuf, C.sqlite3_int64(len(b)),
7866
C.sqlite3_int64(len(b)), C.SQLITE_DESERIALIZE_FREEONCLOSE)

0 commit comments

Comments
 (0)