Skip to content

Commit

Permalink
use safer bytesToString on appengine (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechampine authored and cristaloleg committed Oct 15, 2018
1 parent 286b14e commit 0bde97d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
14 changes: 14 additions & 0 deletions bytes.go
Original file line number Diff line number Diff line change
@@ -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))
}
7 changes: 7 additions & 0 deletions bytes_appengine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build appengine

package bigcache

func bytesToString(b []byte) string {
return string(b)
}
8 changes: 0 additions & 8 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package bigcache

import (
"encoding/binary"
"reflect"
"unsafe"
)

const (
Expand Down Expand Up @@ -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:])
}
Expand Down

0 comments on commit 0bde97d

Please sign in to comment.