[postgis-tickets] r17406 - ST_Union: allocate correct size of memory in right context

Darafei komzpa at gmail.com
Mon Apr 22 03:54:29 PDT 2019


Author: komzpa
Date: 2019-04-22 03:54:29 -0700 (Mon, 22 Apr 2019)
New Revision: 17406

Modified:
   trunk/postgis/lwgeom_geos.c
Log:
ST_Union: allocate correct size of memory in right context

Thanks Raul for finding the cause.

References #4382
Closes https://github.com/postgis/postgis/pull/397


Modified: trunk/postgis/lwgeom_geos.c
===================================================================
--- trunk/postgis/lwgeom_geos.c	2019-04-21 15:52:00 UTC (rev 17405)
+++ trunk/postgis/lwgeom_geos.c	2019-04-22 10:54:29 UTC (rev 17406)
@@ -523,6 +523,7 @@
 Datum pgis_geometry_union_transfn(PG_FUNCTION_ARGS)
 {
 	MemoryContext aggcontext;
+	MemoryContext old;
 	UnionBuildState *state;
 	GSERIALIZED *gser_in;
 	uint32_t curgeom;
@@ -541,7 +542,7 @@
 	}
 	else
 	{
-		MemoryContext old = MemoryContextSwitchTo(aggcontext);
+		old = MemoryContextSwitchTo(aggcontext);
 		state = (UnionBuildState *)palloc(sizeof(UnionBuildState));
 
 		state->mcontext = aggcontext;
@@ -560,7 +561,9 @@
 	/* do we have geometry to push? */
 	if (!PG_ARGISNULL(1))
 	{
-		gser_in = PG_GETARG_GSERIALIZED_P(1);
+		old = MemoryContextSwitchTo(state->mcontext);
+		gser_in = PG_GETARG_GSERIALIZED_P_COPY(1);
+		MemoryContextSwitchTo(old);
 
 		if (state->ngeoms > 0)
 		{
@@ -572,7 +575,6 @@
 
 		if (!gserialized_is_empty(gser_in))
 		{
-			MemoryContext old = MemoryContextSwitchTo(aggcontext);
 			if (state->ngeoms == 0)
 			{
 				state->srid = gserialized_get_srid(gser_in);
@@ -579,7 +581,9 @@
 				state->is3d = gserialized_has_z(gser_in);
 			}
 
+			old = MemoryContextSwitchTo(state->mcontext);
 			g = POSTGIS2GEOS(gser_in);
+			MemoryContextSwitchTo(old);
 
 			if (!g)
 			{
@@ -593,12 +597,13 @@
 
 			if (state->ngeoms > state->alen)
 			{
+				old = MemoryContextSwitchTo(state->mcontext);
 				state->alen *= 2;
-				state->geoms = repalloc(state->geoms, state->alen);
+				state->geoms = repalloc(state->geoms, sizeof(GEOSGeometry *) * state->alen);
+				MemoryContextSwitchTo(old);
 			}
 
 			state->geoms[curgeom] = g;
-			MemoryContextSwitchTo(old);
 		}
 		else
 		{



More information about the postgis-tickets mailing list