From eb99c7d3da996d1a1dabd18aea6781262648dadb Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Sun, 9 Feb 2020 12:02:37 +0200 Subject: prog: remove use of unsafe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Unsafe is, well, unsafe. Plus it fails under the new checkptr mode in go1.14. Remove use of unsafe. No statistically significant change in performance: name old time/op new time/op delta StoreLoadInt-8 21.2ns ± 5% 21.6ns ± 9% ~ (p=0.136 n=20+20) --- prog/mutation.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'prog/mutation.go') diff --git a/prog/mutation.go b/prog/mutation.go index f70a62cbe..b50f48803 100644 --- a/prog/mutation.go +++ b/prog/mutation.go @@ -4,11 +4,11 @@ package prog import ( + "encoding/binary" "fmt" "math" "math/rand" "sort" - "unsafe" ) // Maximum length of generated binary blobs inserted into the program. @@ -840,32 +840,30 @@ func swapInt(v uint64, size int) uint64 { } func loadInt(data []byte, size int) uint64 { - p := unsafe.Pointer(&data[0]) switch size { case 1: - return uint64(*(*uint8)(p)) + return uint64(data[0]) case 2: - return uint64(*(*uint16)(p)) + return uint64(binary.LittleEndian.Uint16(data)) case 4: - return uint64(*(*uint32)(p)) + return uint64(binary.LittleEndian.Uint32(data)) case 8: - return *(*uint64)(p) + return binary.LittleEndian.Uint64(data) default: panic(fmt.Sprintf("loadInt: bad size %v", size)) } } func storeInt(data []byte, v uint64, size int) { - p := unsafe.Pointer(&data[0]) switch size { case 1: - *(*uint8)(p) = uint8(v) + data[0] = uint8(v) case 2: - *(*uint16)(p) = uint16(v) + binary.LittleEndian.PutUint16(data, uint16(v)) case 4: - *(*uint32)(p) = uint32(v) + binary.LittleEndian.PutUint32(data, uint32(v)) case 8: - *(*uint64)(p) = v + binary.LittleEndian.PutUint64(data, v) default: panic(fmt.Sprintf("storeInt: bad size %v", size)) } -- cgit mrf-deployment