From 0bde97d3259d05932b6376e38370413559b5385d Mon Sep 17 00:00:00 2001 From: Luke Champine Date: Mon, 15 Oct 2018 15:27:32 -0400 Subject: [PATCH] use safer bytesToString on appengine (#98) --- bytes.go | 14 ++++++++++++++ bytes_appengine.go | 7 +++++++ encoding.go | 8 -------- 3 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 bytes.go create mode 100644 bytes_appengine.go diff --git a/bytes.go b/bytes.go new file mode 100644 index 00000000..3944bfe1 --- /dev/null +++ b/bytes.go @@ -0,0 +1,14 @@ +// +build !appengine + +package bigcache + +import ( + "reflect" + "unsafe" +) + +func bytesToString(b []byte) string { + bytesHeader := (*reflect.SliceHeader)(unsafe.Pointer(&b)) + strHeader := reflect.StringHeader{Data: bytesHeader.Data, Len: bytesHeader.Len} + return *(*string)(unsafe.Pointer(&strHeader)) +} diff --git a/bytes_appengine.go b/bytes_appengine.go new file mode 100644 index 00000000..3892f3b5 --- /dev/null +++ b/bytes_appengine.go @@ -0,0 +1,7 @@ +// +build appengine + +package bigcache + +func bytesToString(b []byte) string { + return string(b) +} diff --git a/encoding.go b/encoding.go index 5d90d71d..4d434e5d 100644 --- a/encoding.go +++ b/encoding.go @@ -2,8 +2,6 @@ package bigcache import ( "encoding/binary" - "reflect" - "unsafe" ) const ( @@ -55,12 +53,6 @@ func readKeyFromEntry(data []byte) string { return bytesToString(dst) } -func bytesToString(b []byte) string { - bytesHeader := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - strHeader := reflect.StringHeader{Data: bytesHeader.Data, Len: bytesHeader.Len} - return *(*string)(unsafe.Pointer(&strHeader)) -} - func readHashFromEntry(data []byte) uint64 { return binary.LittleEndian.Uint64(data[timestampSizeInBytes:]) }