[postgis-tickets] [SCM] PostGIS branch stable-3.1 updated. 3.1.8-4-g11335bb4b

git at osgeo.org git at osgeo.org
Thu Feb 16 21:56:48 PST 2023


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "PostGIS".

The branch, stable-3.1 has been updated
       via  11335bb4b9ab654e4d0a525800dd28e7484018e3 (commit)
      from  c0a84b9163630aef92b483063ee438cc5c617fc4 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 11335bb4b9ab654e4d0a525800dd28e7484018e3
Author: Regina Obe <lr at pcorp.us>
Date:   Fri Feb 17 00:55:16 2023 -0500

    Prefilter to check for non-finite coordinates before feeding
    ST_SimplifyPreserveTopology, ST_Buffer, ST_SetPoint,
    ST_MinimumInscribedCircle
    to avoid crash/hang.
    References #5320
    References #5315
    References #5318
    References #5319
    for PostGIS 3.1.9

diff --git a/NEWS b/NEWS
index 9e6aa53ce..1588cbafc 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,8 @@ xxxx/xx/xx
 * Bug Fixes
   - #5338, Dump/Restore of raster table fails on
            enforce_coverage_tile_rast constraint (Regina Obe)
+  - #5315, #5318, #5319, #5320 crashes on infinite coordinates
+          (Regina Obe, Paul Ramsey)
 
 PostGIS 3.1.8
 2022/11/12
diff --git a/liblwgeom/liblwgeom.h.in b/liblwgeom/liblwgeom.h.in
index 805ea7c00..2320232aa 100644
--- a/liblwgeom/liblwgeom.h.in
+++ b/liblwgeom/liblwgeom.h.in
@@ -912,6 +912,12 @@ extern const GBOX *lwgeom_get_bbox(const LWGEOM *lwgeom);
 */
 extern int lwgeom_is_collection(const LWGEOM *lwgeom);
 
+/**
+* Check if a LWGEOM has any non-finite (NaN or Inf) coordinates.
+*/
+extern int lwgeom_isfinite(const LWGEOM *lwgeom);
+
+
 /******************************************************************/
 /* Functions that work on type numbers */
 
diff --git a/liblwgeom/lwgeom.c b/liblwgeom/lwgeom.c
index a3a39a698..f3ccfaf31 100644
--- a/liblwgeom/lwgeom.c
+++ b/liblwgeom/lwgeom.c
@@ -2535,3 +2535,29 @@ void lwgeom_trim_bits_in_place(LWGEOM* geom, int32_t prec_x, int32_t prec_y, int
 
 	lwpointiterator_destroy(it);
 }
+
+int
+lwgeom_isfinite(const LWGEOM *lwgeom)
+{
+	LWPOINTITERATOR* it = lwpointiterator_create(lwgeom);
+	int hasz = lwgeom_has_z(lwgeom);
+	int hasm = lwgeom_has_m(lwgeom);
+
+	while (lwpointiterator_has_next(it))
+	{
+		POINT4D p;
+		lwpointiterator_next(it, &p);
+		int finite = isfinite(p.x) &&
+			isfinite(p.y) &&
+			(hasz ? isfinite(p.z) : 1) &&
+			(hasm ? isfinite(p.m) : 1);
+
+		if (!finite)
+		{
+			lwpointiterator_destroy(it);
+			return LW_FALSE;
+		}
+	}
+	lwpointiterator_destroy(it);
+	return LW_TRUE;
+}
diff --git a/postgis/lwgeom_functions_basic.c b/postgis/lwgeom_functions_basic.c
index 0a629912b..1300f85fc 100644
--- a/postgis/lwgeom_functions_basic.c
+++ b/postgis/lwgeom_functions_basic.c
@@ -2334,15 +2334,24 @@ Datum LWGEOM_setpoint_linestring(PG_FUNCTION_ARGS)
 
 	lwg = lwgeom_from_gserialized(pglwg1);
 	line = lwgeom_as_lwline(lwg);
+
 	if (!line)
 	{
 		elog(ERROR, "First argument must be a LINESTRING");
 		PG_RETURN_NULL();
 	}
+
 	if ( line->points->npoints < 1 ) 	{
 		elog(ERROR, "Line has no points");
 		PG_RETURN_NULL();
 	}
+
+	if (!lwgeom_isfinite(lwg))
+	{
+		elog(ERROR, "Geometry contains invalid coordinate");
+		PG_RETURN_NULL();
+	}
+
 	if (which < 0)
 	{
 		/* Use backward indexing for negative values */
diff --git a/postgis/lwgeom_geos.c b/postgis/lwgeom_geos.c
index 964321dd5..932732528 100644
--- a/postgis/lwgeom_geos.c
+++ b/postgis/lwgeom_geos.c
@@ -386,6 +386,14 @@ Datum ST_MaximumInscribedCircle(PG_FUNCTION_ARGS)
 		double width, height, size, tolerance;
 		GBOX gbox;
 		int gtype;
+		LWGEOM *lwg;
+		lwg = lwgeom_from_gserialized(geom);
+		if (!lwgeom_isfinite(lwg))
+		{
+			lwpgerror("Geometry contains invalid coordinates");
+			PG_RETURN_NULL();
+		}
+		lwgeom_free(lwg);
 
 		if (!gserialized_get_gbox_p(geom, &gbox))
 			PG_RETURN_NULL();
@@ -964,23 +972,32 @@ Datum convexhull(PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1(topologypreservesimplify);
 Datum topologypreservesimplify(PG_FUNCTION_ARGS)
 {
-	GSERIALIZED	*geom1;
+	GSERIALIZED	*gs1;
+	LWGEOM *lwg1;
 	double	tolerance;
 	GEOSGeometry *g1, *g3;
 	GSERIALIZED *result;
 	uint32_t type;
 
-	geom1 = PG_GETARG_GSERIALIZED_P(0);
+	gs1 = PG_GETARG_GSERIALIZED_P(0);
 	tolerance = PG_GETARG_FLOAT8(1);
+	lwg1 = lwgeom_from_gserialized(gs1);
 
 	/* Empty.Simplify() == Empty */
-	type = gserialized_get_type(geom1);
-	if ( gserialized_is_empty(geom1) || type == TINTYPE || type == TRIANGLETYPE )
-		PG_RETURN_POINTER(geom1);
+	type = lwgeom_get_type(lwg1);
+	if (lwgeom_is_empty(lwg1) || type == TINTYPE || type == TRIANGLETYPE)
+		PG_RETURN_POINTER(gs1);
+
+	if (!lwgeom_isfinite(lwg1))
+	{
+		lwpgerror("Geometry contains invalid coordinates");
+		PG_RETURN_NULL();
+	}
 
 	initGEOS(lwpgnotice, lwgeom_geos_error);
 
-	g1 = POSTGIS2GEOS(geom1);
+	g1 = LWGEOM2GEOS(lwg1, LW_TRUE);
+	lwgeom_free(lwg1);
 	if (!g1)
 		HANDLE_GEOS_ERROR("First argument geometry could not be converted to GEOS");
 
@@ -991,9 +1008,9 @@ Datum topologypreservesimplify(PG_FUNCTION_ARGS)
 
 	POSTGIS_DEBUGF(3, "result: %s", GEOSGeomToWKT(g3));
 
-	GEOSSetSRID(g3, gserialized_get_srid(geom1));
+	GEOSSetSRID(g3, gserialized_get_srid(gs1));
 
-	result = GEOS2POSTGIS(g3, gserialized_has_z(geom1));
+	result = GEOS2POSTGIS(g3, gserialized_has_z(gs1));
 	GEOSGeom_destroy(g3);
 
 	if (!result)
@@ -1002,7 +1019,7 @@ Datum topologypreservesimplify(PG_FUNCTION_ARGS)
 		PG_RETURN_NULL(); /* never get here */
 	}
 
-	PG_FREE_IF_COPY(geom1, 0);
+	PG_FREE_IF_COPY(gs1, 0);
 	PG_RETURN_POINTER(result);
 }
 
@@ -1056,6 +1073,15 @@ Datum buffer(PG_FUNCTION_ARGS)
 		PG_RETURN_POINTER(geometry_serialize(lwg));
 	}
 
+	lwg = lwgeom_from_gserialized(geom1);
+
+	if (!lwgeom_isfinite(lwg))
+	{
+		lwpgerror("Geometry contains invalid coordinates");
+		PG_RETURN_NULL();
+	}
+	lwgeom_free(lwg);
+
 	initGEOS(lwpgnotice, lwgeom_geos_error);
 
 	g1 = POSTGIS2GEOS(geom1);
diff --git a/regress/core/geos39.sql b/regress/core/geos39.sql
index 8885fdd85..4a444a418 100644
--- a/regress/core/geos39.sql
+++ b/regress/core/geos39.sql
@@ -42,3 +42,6 @@ SELECT 'mic-cte' AS name, ST_AsText(st_snaptogrid((ST_MaximumInscribedCircle(ply
 SELECT 'rp-1', ST_AsText(ST_ReducePrecision('POINT(1.412 19.323)', 0.1));
 SELECT 'rp-2', ST_AsText(ST_ReducePrecision('POINT(1.412 19.323)', 1.0));
 SELECT 'rp-3', ST_AsText(ST_ReducePrecision('POINT(1.412 19.323)', 10));
+
+-- ST_MaximumInscribedCircle infinite check https://trac.osgeo.org/postgis/ticket/5318
+SELECT '#5318', ST_MaximumInscribedCircle('0106000020E61000000100000001030000000100000005000000000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F'::geometry) As result;
diff --git a/regress/core/geos39_expected b/regress/core/geos39_expected
index 06ebbd417..ec9be2b85 100644
--- a/regress/core/geos39_expected
+++ b/regress/core/geos39_expected
@@ -8,3 +8,4 @@ mic-cte|POINT(49.512 49.512)
 rp-1|POINT(1.4 19.3)
 rp-2|POINT(1 19)
 rp-3|POINT(0 20)
+ERROR:  Geometry contains invalid coordinates
diff --git a/regress/core/setpoint.sql b/regress/core/setpoint.sql
index 51519ac54..553e50309 100644
--- a/regress/core/setpoint.sql
+++ b/regress/core/setpoint.sql
@@ -26,3 +26,6 @@ SELECT ST_asewkt(ST_SetPoint('LINESTRING(0 0 0 0, 1 1 1 1, 2 2 2 2, 4 4 4 4)', 2
 SELECT ST_asewkt(ST_SetPoint('LINESTRING(0 0 0 0, 1 1 1 1, 2 2 2 2, 4 4 4 4)', 1, 'POINTM(90 91 92)'));
 SELECT ST_asewkt(ST_SetPoint('LINESTRING(0 0 0 0, 1 1 1 1, 2 2 2 2, 4 4 4 4)', 0, 'POINT(90 91 92 93)'));
 
+-- https://trac.osgeo.org/postgis/ticket/5319
+SELECT '#5319', ST_SetPoint('0102000020E610000005000000000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F'::geometry, 1,ST_GeomFromText('POINT(0 0)',4326)  )  As result;
+
diff --git a/regress/core/setpoint_expected b/regress/core/setpoint_expected
index 210195c40..cbc622b07 100644
--- a/regress/core/setpoint_expected
+++ b/regress/core/setpoint_expected
@@ -11,3 +11,4 @@ LINESTRING(0 0 0 0,1 1 1 1,2 2 2 2,90 91 0 0)
 LINESTRING(0 0 0 0,1 1 1 1,90 91 92 0,4 4 4 4)
 LINESTRING(0 0 0 0,90 91 0 92,2 2 2 2,4 4 4 4)
 LINESTRING(90 91 92 93,1 1 1 1,2 2 2 2,4 4 4 4)
+ERROR:  Geometry contains invalid coordinate
diff --git a/regress/core/tickets.sql b/regress/core/tickets.sql
index a178b4b68..b1c83c8e8 100644
--- a/regress/core/tickets.sql
+++ b/regress/core/tickets.sql
@@ -1453,3 +1453,7 @@ reset max_parallel_workers_per_gather;
 reset force_parallel_mode;
 
 SET client_min_messages TO NOTICE;
+-- https://trac.osgeo.org/postgis/ticket/5315
+SELECT '#5315', ST_Buffer('0106000020E86400000100000001030000000100000005000000000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F'::geometry, 1);
+
+SELECT '#5320', ST_SimplifyPreserveTopology('0106000020E86400000100000001030000000100000005000000000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F'::geometry, 1);
diff --git a/regress/core/tickets_expected b/regress/core/tickets_expected
index 7af53ff4b..e7ca0aab7 100644
--- a/regress/core/tickets_expected
+++ b/regress/core/tickets_expected
@@ -463,3 +463,5 @@ ERROR:  LWGEOM_addpoint: Invalid offset
 ERROR:  Line has no points
 #5139|100000
 #5139|1000000
+ERROR:  Geometry contains invalid coordinates
+ERROR:  Geometry contains invalid coordinates

-----------------------------------------------------------------------

Summary of changes:
 NEWS                             |  2 ++
 liblwgeom/liblwgeom.h.in         |  6 ++++++
 liblwgeom/lwgeom.c               | 26 ++++++++++++++++++++++++
 postgis/lwgeom_functions_basic.c |  9 ++++++++
 postgis/lwgeom_geos.c            | 44 ++++++++++++++++++++++++++++++++--------
 regress/core/geos39.sql          |  3 +++
 regress/core/geos39_expected     |  1 +
 regress/core/setpoint.sql        |  3 +++
 regress/core/setpoint_expected   |  1 +
 regress/core/tickets.sql         |  4 ++++
 regress/core/tickets_expected    |  2 ++
 11 files changed, 92 insertions(+), 9 deletions(-)


hooks/post-receive
-- 
PostGIS


More information about the postgis-tickets mailing list