Skip to content

Commit e7a63fe

Browse files
MB-54131: Corner cases when buffer not setup & formatting (#15)
* MB-54131: Corner cases when buffer not setup & formatting * No need to export sizeOfFloat64, sizeOfVertex
1 parent 045f1ed commit e7a63fe

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

s2/buffer_pool.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,24 @@ func NewGeoBufferPool(maxSize int, minSize int) *GeoBufferPool {
4646
}
4747

4848
func (b *GeoBufferPool) Get(size int) ([]byte) {
49+
if b == nil {
50+
// GeoBufferPool not setup
51+
return make([]byte, size)
52+
}
53+
4954
bufSize := b.minSize
5055

5156
for i := range b.buffers {
52-
if size <= bufSize || i == len(b.buffers) - 1{
57+
if size <= bufSize || i == len(b.buffers) - 1 {
5358
if b.buffers[i] == nil {
5459
b.buffers[i] = make([]byte, bufSize)
5560
}
5661

5762
return b.buffers[i]
58-
} else {
59-
bufSize = bufSize * 2
6063
}
64+
65+
bufSize = bufSize * 2
6166
}
6267

6368
return nil
64-
}
69+
}

s2/encode.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ func (d *decoder) readFloat64Array(size int, buf []byte) int {
231231
if size >= len(buf) {
232232
_, d.err = io.ReadFull(d.r, buf)
233233
return len(buf)
234-
} else {
235-
_, d.err = io.ReadFull(d.r, buf[0:size])
236-
return size
237234
}
235+
236+
_, d.err = io.ReadFull(d.r, buf[0:size])
237+
return size
238238
}

s2/loop.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ import (
1919
"fmt"
2020
"io"
2121
"math"
22+
"reflect"
2223

2324
"github.com/golang/geo/r1"
2425
"github.com/golang/geo/r3"
2526
"github.com/golang/geo/s1"
2627
)
2728

28-
const SizeOfFloat = 8
29-
const SizeOfVertex = 3 * SizeOfFloat
30-
3129
// Loop represents a simple spherical polygon. It consists of a sequence
3230
// of vertices where the first vertex is implicitly connected to the
3331
// last. All loops are defined to have a CCW orientation, i.e. the interior of
@@ -1265,6 +1263,15 @@ func (l Loop) encode(e *encoder) {
12651263
l.bound.encode(e)
12661264
}
12671265

1266+
func init() {
1267+
var f64 float64
1268+
sizeOfFloat64 = int(reflect.TypeOf(f64).Size())
1269+
sizeOfVertex = 3 * sizeOfFloat64
1270+
}
1271+
1272+
var sizeOfFloat64 int
1273+
var sizeOfVertex int
1274+
12681275
// Decode decodes a loop.
12691276
func (l *Loop) Decode(r io.Reader) error {
12701277
*l = Loop{}
@@ -1296,7 +1303,7 @@ func (l *Loop) decode(d *decoder) {
12961303
l.vertices = make([]Point, nvertices)
12971304

12981305
// Each vertex requires 24 bytes of storage
1299-
numBytesNeeded := int(nvertices) * SizeOfVertex
1306+
numBytesNeeded := int(nvertices) * sizeOfVertex
13001307

13011308
i := 0
13021309

@@ -1308,18 +1315,21 @@ func (l *Loop) decode(d *decoder) {
13081315
break
13091316
}
13101317

1311-
numBytesNeeded = numBytesNeeded - numBytesRead
1318+
numBytesNeeded -= numBytesRead
13121319

13131320
// Parsing one vertex at a time into the vertex array of the loop
1314-
// by going through the buffer in steps of SizeOfVertex and converting
1321+
// by going through the buffer in steps of sizeOfVertex and converting
13151322
// floatSize worth of bytes into the float values
1316-
for j := 0; j < int(numBytesRead/SizeOfVertex); j++ {
1317-
l.vertices[i+j].X = math.Float64frombits(binary.LittleEndian.Uint64(arr[SizeOfFloat*(j*3) : SizeOfFloat*(j*3+1)]))
1318-
l.vertices[i+j].Y = math.Float64frombits(binary.LittleEndian.Uint64(arr[SizeOfFloat*(j*3+1) : SizeOfFloat*(j*3+2)]))
1319-
l.vertices[i+j].Z = math.Float64frombits(binary.LittleEndian.Uint64(arr[SizeOfFloat*(j*3+2) : SizeOfFloat*(j*3+3)]))
1323+
for j := 0; j < int(numBytesRead/sizeOfVertex); j++ {
1324+
l.vertices[i+j].X = math.Float64frombits(
1325+
binary.LittleEndian.Uint64(arr[sizeOfFloat64*(j*3) : sizeOfFloat64*(j*3+1)]))
1326+
l.vertices[i+j].Y = math.Float64frombits(
1327+
binary.LittleEndian.Uint64(arr[sizeOfFloat64*(j*3+1) : sizeOfFloat64*(j*3+2)]))
1328+
l.vertices[i+j].Z = math.Float64frombits(
1329+
binary.LittleEndian.Uint64(arr[sizeOfFloat64*(j*3+2) : sizeOfFloat64*(j*3+3)]))
13201330
}
13211331

1322-
i = i + int(numBytesRead/SizeOfVertex)
1332+
i += int(numBytesRead/sizeOfVertex)
13231333
}
13241334

13251335
l.index = NewShapeIndex()

0 commit comments

Comments
 (0)