Skip to content

Commit 7312f87

Browse files
committed
geomfn: prevent crash with st_snap on empty geometries
After we've upgraded to GEOS 3.12+, apparently we picked up a regression with the library where it could result in a crash when evaluating `Snap` on an empty geometry. In such a scenario, the input parameter should be returned unchanged, so we simply add a short-circuiting logic. Release note: None
1 parent 84856d9 commit 7312f87

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

pkg/geo/geomfn/snap.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import (
1414
// geometry. Tolerance is used to control where snapping is performed.
1515
// If no snapping occurs then the input geometry is returned unchanged.
1616
func Snap(input, target geo.Geometry, tolerance float64) (geo.Geometry, error) {
17+
if input.Empty() {
18+
// If input is empty, return it unchanged (snap has no effect on empty
19+
// geoms).
20+
return input, nil
21+
}
1722
snappedEWKB, err := geos.Snap(input.EWKB(), target.EWKB(), tolerance)
1823
if err != nil {
1924
return geo.Geometry{}, err

pkg/sql/logictest/testdata/logic_test/geospatial

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6378,3 +6378,10 @@ SELECT '{0101000020e6100000cdcccccccc4c1b40cdcccccccc8c4740:0101000020e610000033
63786378
{0101000020E6100000CDCCCCCCCC4C1B40CDCCCCCCCC8C4740:0101000020E6100000333333333333FD3FCDCCCCCCCC0C4640}
63796379

63806380
subtest end
6381+
6382+
# Regression test for crashing when evaluating st_snap on an empty geometry
6383+
# (#151103).
6384+
query T
6385+
SELECT st_snap('01010000C0000000000000F87F000000000000F87F000000000000F87F000000000000F87F'::GEOMETRY, '01010000C0000000000000F87F000000000000F87F000000000000F87F000000000000F87F'::GEOMETRY, 0.5::FLOAT8);
6386+
----
6387+
01010000C0000000000000F87F000000000000F87F000000000000F87F000000000000F87F

pkg/sql/tests/rsg_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,6 @@ func TestRandomSyntaxFunctions(t *testing.T) {
435435
"crdb_internal.fingerprint":
436436
// Skipped due to long execution time.
437437
continue
438-
case "st_snap":
439-
// TODO(#151103): unskip st_snap.
440-
continue
441438
}
442439
_, variations := builtinsregistry.GetBuiltinProperties(name)
443440
for _, builtin := range variations {
@@ -758,10 +755,6 @@ func TestRandomSyntaxSQLSmith(t *testing.T) {
758755
return err
759756
}, func(ctx context.Context, db *verifyFormatDB, r *rsg.RSG) error {
760757
s := smither.Generate()
761-
if strings.Contains(s, "st_snap(") {
762-
// TODO(#151103): unskip st_snap.
763-
return nil
764-
}
765758
err := db.exec(t, ctx, s)
766759
if c := (*crasher)(nil); errors.As(err, &c) {
767760
if err := db.exec(t, ctx, "USE defaultdb"); err != nil {

0 commit comments

Comments
 (0)