Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#411 use safer method when converting between byte and float slices #454

Merged
merged 6 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ci/docker-compose-cmppg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
timeout: '1s'
retries: 50
tests:
image: golang:1.14
image: golang:1.17
working_dir: /go/src/github.com/peterstace/simplefeatures
entrypoint: go test -test.count=1 -test.timeout=30m -test.run=. ./internal/cmprefimpl/cmppg
volumes:
Expand Down
2 changes: 1 addition & 1 deletion .ci/docker-compose-pgscan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
timeout: '1s'
retries: 50
tests:
image: golang:1.14
image: golang:1.17
working_dir: /go/src/github.com/peterstace/simplefeatures
entrypoint: go test -test.count=1 -test.timeout=30m -test.run=. ./internal/pgscan
volumes:
Expand Down
2 changes: 1 addition & 1 deletion .ci/docker-compose-unit.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "2.1"
services:
tests:
image: golang:1.15
image: golang:1.17
working_dir: /go/src/github.com/peterstace/simplefeatures
entrypoint: go test -covermode=count -coverprofile=coverage.out -test.count=1 -test.run=. ./geom ./rtree
volumes:
Expand Down
2 changes: 1 addition & 1 deletion .ci/geos.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.14
FROM golang:1.17
RUN apt-get -y update && \
apt-get install -y libgeos-dev=3.7.1-1 && \
rm -rf /var/lib/apt/lists/*
Expand Down
2 changes: 1 addition & 1 deletion .ci/golint.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
FROM golang:1.14
FROM golang:1.17
RUN go get -u golang.org/x/lint/golint
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))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to be careful about the case where len(floats) is 0, otherwise floats[0] will panic. We could just have a guard if statement checking that condition first (and simply return nil in that case). That applies to bytesAsBytes too.

}
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