Index: postgis/lwgeom_geos.c
===================================================================
--- postgis/lwgeom_geos.c	(revision 7276)
+++ postgis/lwgeom_geos.c	(working copy)
@@ -1358,13 +1358,29 @@
 	LWGEOM *lwgeom;
 	bool result;
 	GEOSGeom g1;
+#if POSTGIS_GEOS_VERSION < 33
+  BOX2DFLOAT4 box1;
+#endif
 
 	PROFSTART(PROF_QRUN);
 
 	geom1 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
 
+#if POSTGIS_GEOS_VERSION < 33
+  /* Short circuit and return FALSE if we have infinite coordinates */
+  /* GEOS 3.3+ is supposed to  handle this stuff OK */
+	if ( getbox2d_p(SERIALIZED_FORM(geom1), &box1) )	
+	{
+		if ( isinf(box1.xmax) || isinf(box1.ymax) || isinf(box1.xmin) || isinf(box1.ymin) || 
+		     isnan(box1.xmax) || isnan(box1.ymax) || isnan(box1.xmin) || isnan(box1.ymin)  )
+		{
+      lwnotice("Geometry contains an Inf or NaN coordinate");
+			PG_RETURN_BOOL(FALSE);
+		}
+	}
+#endif
+
 	initGEOS(lwnotice, lwnotice);
-
 	PROFSTART(PROF_P2G1);
 
 	lwgeom = lwgeom_deserialize(SERIALIZED_FORM(geom1));
@@ -1372,6 +1388,8 @@
 	{
 		lwerror("unable to deserialize input");
 	}
+	
+	
 	g1 = LWGEOM2GEOS(lwgeom);
 	if ( ! g1 )
 	{
@@ -1413,9 +1431,31 @@
 	int len = 0;
 	char *result = NULL;
 	const GEOSGeometry *g1 = NULL;
+#if POSTGIS_GEOS_VERSION < 33
+  BOX2DFLOAT4 box;
+#endif
 
 	geom = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
 
+#if POSTGIS_GEOS_VERSION < 33
+  /* Short circuit and return if we have infinite coordinates */
+  /* GEOS 3.3+ is supposed to  handle this stuff OK */
+	if ( getbox2d_p(SERIALIZED_FORM(geom), &box) )	
+	{
+		if ( isinf(box.xmax) || isinf(box.ymax) || isinf(box.xmin) || isinf(box.ymin) || 
+		     isnan(box.xmax) || isnan(box.ymax) || isnan(box.xmin) || isnan(box.ymin)  )
+		{
+			const char *rsn = "Geometry contains an Inf or NaN coordinate";
+    	len = strlen(rsn);
+    	result = palloc(VARHDRSZ + len);
+    	SET_VARSIZE(result, VARHDRSZ + len);
+    	memcpy(VARDATA(result), rsn, len);
+    	PG_FREE_IF_COPY(geom, 0);
+    	PG_RETURN_POINTER(result);			
+		}
+	}
+#endif
+
 	initGEOS(lwnotice, lwnotice);
 
 	g1 = (GEOSGeometry *)POSTGIS2GEOS(geom);
@@ -2899,6 +2939,13 @@
 
 		POSTGIS_DEBUGF(4, "Point: %g,%g,%g", p.x, p.y, p.z);
 
+#if POSTGIS_GEOS_VERSION < 33
+    /* Make sure we don't pass any infinite values down into GEOS */
+    /* GEOS 3.3+ is supposed to  handle this stuff OK */
+    if ( isinf(p.x) || isinf(p.y) || (dims == 3 && isinf(p.z)) )
+      lwerror("Infinite coordinate value found in geometry.");
+#endif
+
 		GEOSCoordSeq_setX(sq, i, p.x);
 		GEOSCoordSeq_setY(sq, i, p.y);
 		if ( dims == 3 ) GEOSCoordSeq_setZ(sq, i, p.z);
