From bf3a4e6b0baa411beda7056db5ad01fa330fb0d4 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Fri, 3 Oct 2014 09:34:43 +0000 Subject: [PATCH] ST_ClipByBox2D: do not use gbox_overlaps with BOX2DF objects Fixes use of uninitialized memory (#2954) git-svn-id: http://svn.osgeo.org/postgis/trunk@13034 b70326c6-7e19-0410-871a-916f4a2858ee --- postgis/lwgeom_geos.c | 9 ++++++--- regress/clipbybox2d.sql | 2 ++ regress/clipbybox2d_expected | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/postgis/lwgeom_geos.c b/postgis/lwgeom_geos.c index ce3a5092f..3a81d9862 100644 --- a/postgis/lwgeom_geos.c +++ b/postgis/lwgeom_geos.c @@ -1665,10 +1665,13 @@ Datum ST_ClipByBox2d(PG_FUNCTION_ARGS) PG_RETURN_POINTER(geom1); } + /* WARNING: this is really a BOX2DF, use only xmin and ymin fields */ bbox2 = (GBOX *)PG_GETARG_POINTER(1); - /* If bbox1 outside of bbox, return empty */ - if ( LW_FALSE == gbox_overlaps_2d(bbox1, bbox2) ) { + /* If bbox1 outside of bbox2, return empty */ + if ( bbox1->xmin > bbox2->xmax || bbox1->xmax < bbox2->xmin || + bbox1->ymin > bbox2->ymax || bbox1->ymax < bbox2->ymin ) + { lwresult = lwgeom_construct_empty(lwgeom1->type, lwgeom1->srid, 0, 0); lwgeom_free(lwgeom1); PG_FREE_IF_COPY(geom1, 0); @@ -1677,7 +1680,7 @@ Datum ST_ClipByBox2d(PG_FUNCTION_ARGS) PG_RETURN_POINTER(result); } - /* if bbox1 is covered by bbox, return lwgeom1 */ + /* if bbox1 is covered by bbox2, return lwgeom1 */ if ( bbox1->xmax <= bbox2->xmax && bbox1->xmin >= bbox2->xmin && bbox1->ymax <= bbox2->ymax && bbox1->ymin >= bbox2->ymin ) { diff --git a/regress/clipbybox2d.sql b/regress/clipbybox2d.sql index 1c9a4a6f7..b82207d7d 100644 --- a/regress/clipbybox2d.sql +++ b/regress/clipbybox2d.sql @@ -21,3 +21,5 @@ CREATE TEMPORARY TABLE t AS SELECT 'SRID=3857;POLYGON((41 20,41 0,21 0,1 20,1 40,21 40,41 20))' ::geometry g; SELECT ST_AsEWKT(ST_ClipByBox2d(g, ST_MakeEnvelope(-20,-20,-10,-10))) FROM t; +-- See http://trac.osgeo.org/postgis/ticket/2954 +SELECT ST_AsEWKT(ST_ClipByBox2D('SRID=4326;POINT(0 0)','BOX3D(-1 -1,1 1)'::box3d::box2d)); diff --git a/regress/clipbybox2d_expected b/regress/clipbybox2d_expected index 8243e44fa..e58bf780c 100644 --- a/regress/clipbybox2d_expected +++ b/regress/clipbybox2d_expected @@ -6,3 +6,4 @@ POLYGON((2 2,8 2,2 8,8 8,2 2)) POLYGON((2.5 2,5 4,5 5,5 4,7.5 2,2.5 2)) POLYGON((2 2,2 5,5 5,5 2,2 2)) SRID=3857;POLYGON EMPTY +SRID=4326;POINT(0 0) -- 2.40.0