Skip to content

Commit

Permalink
#411 use safer method when converting between byte and float slices
Browse files Browse the repository at this point in the history
  • Loading branch information
sameeraaperera committed May 13, 2022
1 parent 035a8b9 commit daa6923
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 15 deletions.
8 changes: 1 addition & 7 deletions geom/wkb_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/binary"
"fmt"
"math"
"reflect"
"unsafe"
)

Expand Down Expand Up @@ -87,10 +86,5 @@ func (m *wkbMarshaler) writeSequence(seq Sequence) {
// floatsAsBytes reinterprets the floats slice as a bytes slice in a similar
// manner to reinterpret_cast in C++.
func floatsAsBytes(floats []float64) []byte {
var byts []byte
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&byts))
hdr.Data = (*reflect.SliceHeader)(unsafe.Pointer(&floats)).Data
hdr.Len = 8 * len(floats)
hdr.Cap = 8 * cap(floats)
return byts
return unsafe.Slice((*byte)(unsafe.Pointer(&floats[0])), 8*len(floats))
}
8 changes: 1 addition & 7 deletions geom/wkb_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/binary"
"fmt"
"math"
"reflect"
"unsafe"
)

Expand Down Expand Up @@ -224,12 +223,7 @@ func (p *wkbParser) parseLineString(ctype CoordinatesType) (LineString, error) {
// bytesAsFloats reinterprets the bytes slice as a float64 slice in a similar
// manner to reinterpret_cast in C++.
func bytesAsFloats(byts []byte) []float64 {
var floats []float64
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&floats))
hdr.Data = (*reflect.SliceHeader)(unsafe.Pointer(&byts)).Data
hdr.Len = len(byts) / 8
hdr.Cap = cap(byts) / 8
return floats
return unsafe.Slice((*float64)(unsafe.Pointer(&byts[0])), len(byts)/8)
}

// flipEndianessStride8 flips the endianess of the input bytes, assuming that
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/peterstace/simplefeatures

go 1.14
go 1.17

require github.com/lib/pq v1.1.1

0 comments on commit daa6923

Please sign in to comment.