From 684390dbb1d23c12f137656842d9daab3f6d43e7 Mon Sep 17 00:00:00 2001
From: Sandro Santilli <strk@keybit.net>
Date: Fri, 25 Jun 2010 09:30:11 +0000
Subject: [PATCH] Properly handle GEOS exceptions on cascaded union code
 (failing since introduction of postponed exception for cleaning geoms)

git-svn-id: http://svn.osgeo.org/postgis/trunk@5707 b70326c6-7e19-0410-871a-916f4a2858ee
---
 postgis/lwgeom_geos.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/postgis/lwgeom_geos.c b/postgis/lwgeom_geos.c
index 040b669dd..6841f5bab 100644
--- a/postgis/lwgeom_geos.c
+++ b/postgis/lwgeom_geos.c
@@ -360,7 +360,7 @@ Datum pgis_union_geometry_array(PG_FUNCTION_ARGS)
 	int nelems, i;
 	PG_LWGEOM *result = NULL;
 	PG_LWGEOM *pgis_geom = NULL;
-	const GEOSGeometry * g1 = NULL;
+	GEOSGeometry * g1 = NULL;
 	GEOSGeometry * g2 = NULL;
 	GEOSGeometry * geos_result=NULL;
 	int SRID=-1;
@@ -550,12 +550,24 @@ Datum pgis_union_geometry_array(PG_FUNCTION_ARGS)
 		if (curgeom > 0)
 		{
 			g1 = GEOSGeom_createCollection(GEOS_MULTIPOLYGON, geoms, curgeom);
-			if ( g1 ) g2 = GEOSUnionCascaded(g1);
-			if ( g2 ) GEOSSetSRID(g2, SRID);
-			if ( g2 ) result = GEOS2POSTGIS(g2, is3d);
-			/* Clean up the mess. */
-			if ( g1 ) GEOSGeom_destroy((GEOSGeometry *)g1);
-			if ( g2 ) GEOSGeom_destroy(g2);
+			if ( ! g1 ) 
+			{
+				/* TODO: cleanup geoms memory */
+				lwerror("Could not create MULTIPOLYGON from geometry array: %s", lwgeom_geos_errmsg);
+				PG_RETURN_NULL();
+			}
+			g2 = GEOSUnionCascaded(g1);
+			GEOSGeom_destroy(g1);
+			if ( ! g2 )
+			{
+				lwerror("GEOSUnionCascaded: %s",
+					lwgeom_geos_errmsg);
+				PG_RETURN_NULL();
+			}
+
+			GEOSSetSRID(g2, SRID);
+			result = GEOS2POSTGIS(g2, is3d);
+			GEOSGeom_destroy(g2);
 		}
 		else
 		{
-- 
2.40.0