Skip to content

Commit 26eb2dc

Browse files
craig[bot]yuzefovich
andcommitted
Merge #145283
145283: geo: fix index out of bounds for recently added builtin r=yuzefovich a=yuzefovich Fixes: #144282 Fixes: #144446 Release note: None Co-authored-by: Yahor Yuzefovich <[email protected]>
2 parents 65d8edd + 86fca13 commit 26eb2dc

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

pkg/geo/geomfn/unary_operators.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,18 @@ func CountVertices(t geom.T) int {
250250
// lineString does not have a z-coordinate.
251251
func length3DLineString(lineString *geom.LineString) (float64, error) {
252252
lineCoords := lineString.Coords()
253-
prevPoint := lineCoords[0]
254-
lineLength := float64(0)
255253
zIndex := lineString.Layout().ZIndex()
256254
if zIndex < 0 || zIndex >= lineString.Stride() {
257255
return 0, errors.AssertionFailedf("Z-Index for LINESTRING is out-of-bounds")
258256
}
259-
for i := 0; i < lineString.NumCoords(); i++ {
260-
curPoint := lineCoords[i]
257+
lineLength := float64(0)
258+
for i := 1; i < len(lineCoords); i++ {
259+
prevPoint, curPoint := lineCoords[i-1], lineCoords[i]
261260
deltaX := curPoint.X() - prevPoint.X()
262261
deltaY := curPoint.Y() - prevPoint.Y()
263262
deltaZ := curPoint[zIndex] - prevPoint[zIndex]
264263
distBetweenPoints := math.Sqrt(deltaX*deltaX + deltaY*deltaY + deltaZ*deltaZ)
265264
lineLength += distBetweenPoints
266-
prevPoint = curPoint
267265
}
268266

269267
return lineLength, nil

pkg/sql/logictest/testdata/logic_test/geospatial_zm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,16 @@ SELECT ST_3DLength('LINESTRING(743238 2967416 1,743238 2967450 1,743265 2967450
388388
----
389389
122.70471674145682
390390

391+
query R
392+
SELECT ST_3DLength('010200008000000000':::GEOMETRY);
393+
----
394+
0
395+
396+
query R
397+
SELECT ST_3DLength('01020000C000000000':::GEOMETRY);
398+
----
399+
0
400+
391401
query TTT nosort
392402
SELECT
393403
encode(ST_AsTWKB(t, 5), 'hex'),

0 commit comments

Comments
 (0)