Skip to content

Commit

Permalink
builtins: fix panic for ST_MinimumBoundingCircle with num_segments
Browse files Browse the repository at this point in the history
Release note (bug fix): Fix a bug where ST_MinimumBoundingCircle would
panic with infinite coordinates and a num_segments argument.
  • Loading branch information
otan committed Apr 19, 2022
1 parent 21738e1 commit b856781
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
3 changes: 3 additions & 0 deletions pkg/geo/geomfn/linestring.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func LineMerge(g geo.Geometry) (geo.Geometry, error) {
if g.Empty() {
return g, nil
}
if BoundingBoxHasInfiniteCoordinates(g) {
return geo.Geometry{}, pgerror.Newf(pgcode.InvalidParameterValue, "value out of range: overflow")
}
ret, err := geos.LineMerge(g.EWKB())
if err != nil {
return geo.Geometry{}, err
Expand Down
6 changes: 6 additions & 0 deletions pkg/geo/geomfn/topology_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geopb"
"github.com/cockroachdb/cockroach/pkg/geo/geos"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
)

// Boundary returns the boundary of a given Geometry.
Expand All @@ -42,6 +44,10 @@ func Centroid(g geo.Geometry) (geo.Geometry, error) {

// MinimumBoundingCircle returns minimum bounding circle of an EWKB
func MinimumBoundingCircle(g geo.Geometry) (geo.Geometry, geo.Geometry, float64, error) {
if BoundingBoxHasInfiniteCoordinates(g) {
return geo.Geometry{}, geo.Geometry{}, 0, pgerror.Newf(pgcode.InvalidParameterValue, "value out of range: overflow")
}

polygonEWKB, centroidEWKB, radius, err := geos.MinimumBoundingCircle(g.EWKB())
if err != nil {
return geo.Geometry{}, geo.Geometry{}, 0, err
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/geospatial
Original file line number Diff line number Diff line change
Expand Up @@ -5670,6 +5670,9 @@ true
statement error st_minimumboundingcircle\(\): value out of range: overflow
select st_minimumboundingcircle(st_makepoint(((-0.27013513189303495):::FLOAT8::FLOAT8 // 5e-324:::FLOAT8::FLOAT8)::FLOAT8::FLOAT8, (-0.4968052087960828):::FLOAT8::FLOAT8)::GEOMETRY::GEOMETRY)::GEOMETRY

statement error st_minimumboundingcircle\(\): value out of range: overflow
select st_minimumboundingcircle(st_makepoint(((-0.27013513189303495):::FLOAT8::FLOAT8 // 5e-324:::FLOAT8::FLOAT8)::FLOAT8::FLOAT8, (-0.4968052087960828):::FLOAT8::FLOAT8)::GEOMETRY::GEOMETRY, 10)::GEOMETRY

subtest st_unaryunion

query T
Expand Down
6 changes: 0 additions & 6 deletions pkg/sql/sem/builtins/geo_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -3181,9 +3181,6 @@ Note If the result has zero or one points, it will be returned as a POINT. If it
defProps(),
geometryOverload1(
func(ctx *tree.EvalContext, g *tree.DGeometry) (tree.Datum, error) {
if geomfn.BoundingBoxHasInfiniteCoordinates(g.Geometry) {
return nil, pgerror.Newf(pgcode.InvalidParameterValue, "value out of range: overflow")
}
line, err := geomfn.LineMerge(g.Geometry)
if err != nil {
return nil, err
Expand Down Expand Up @@ -6471,9 +6468,6 @@ The parent_only boolean is always ignored.`,
"st_minimumboundingcircle": makeBuiltin(defProps(),
geometryOverload1(
func(evalContext *tree.EvalContext, g *tree.DGeometry) (tree.Datum, error) {
if geomfn.BoundingBoxHasInfiniteCoordinates(g.Geometry) {
return nil, pgerror.Newf(pgcode.InvalidParameterValue, "value out of range: overflow")
}
polygon, _, _, err := geomfn.MinimumBoundingCircle(g.Geometry)
if err != nil {
return nil, err
Expand Down

0 comments on commit b856781

Please sign in to comment.