Skip to content

Commit

Permalink
Merge pull request #454 from peterstace/411-use-unsafeslice-for-wkb-h…
Browse files Browse the repository at this point in the history
…andling

#411 use safer method when converting between byte and float slices
  • Loading branch information
peterstace committed May 13, 2022
2 parents 831b19b + 06cb0a3 commit 7d1ff1d
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 21 deletions.
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
4 changes: 2 additions & 2 deletions .ci/geos.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM golang:1.14
FROM golang:1.17-buster
RUN apt-get -y update && \
apt-get install -y libgeos-dev=3.7.1-1 && \
rm -rf /var/lib/apt/lists/*
ENV PATH=/usr/lib/go-1.15/bin:${PATH}
ENV PATH=/usr/lib/go-1.17/bin:${PATH}
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
- Add a new `DumpRings` method to the `Polygon` type, which gives the rings of
the polygon as a slice of `LineString`s.

- Uses `unsafe.Slice` for internal WKB conversions. This increases the minimum
Go version required to use simplefeatures from 1.14 to 1.17.

## v0.37.0

2022-03-29
Expand Down
11 changes: 4 additions & 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,8 @@ 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
if len(floats) == 0 {
return nil
}
return unsafe.Slice((*byte)(unsafe.Pointer(&floats[0])), 8*len(floats))
}
11 changes: 4 additions & 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,10 @@ 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
if len(byts) == 0 {
return nil
}
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 7d1ff1d

Please sign in to comment.