[postgis-tickets] r16020 - Fix memory leak in lwgeom_grid_in_place

Paul Ramsey pramsey at cleverelephant.ca
Fri Oct 20 06:58:00 PDT 2017


Author: pramsey
Date: 2017-10-20 06:58:00 -0700 (Fri, 20 Oct 2017)
New Revision: 16020

Modified:
   trunk/liblwgeom/lwgeom.c
Log:
Fix memory leak in lwgeom_grid_in_place
>From Ra?\195?\186l Mar?\195?\173n Rodr?\195?\173guez
Closes #3910


Modified: trunk/liblwgeom/lwgeom.c
===================================================================
--- trunk/liblwgeom/lwgeom.c	2017-10-20 13:07:21 UTC (rev 16019)
+++ trunk/liblwgeom/lwgeom.c	2017-10-20 13:58:00 UTC (rev 16020)
@@ -2151,25 +2151,39 @@
 		case POLYGONTYPE:
 		{
 			LWPOLY *ply = (LWPOLY*)(geom);
-			int i, j = 0;
 			if (!ply->rings) return;
-			for (i = 0; i < ply->nrings; i++)
+
+			/* Check first the external ring */
+			int i = 0;
+			POINTARRAY *pa = ply->rings[0];
+			ptarray_grid_in_place(pa, grid);
+			if (pa->npoints < 4)
 			{
+				/* External ring collapsed: free everything */
+				for (i = 0; i < ply->nrings; i++)
+				{
+					ptarray_free(ply->rings[i]);
+				}
+				ply->nrings = 0;
+				return;
+			}
+
+			/* Check the other rings */
+			int j = 1;
+			for (i = 1; i < ply->nrings; i++)
+			{
 				POINTARRAY *pa = ply->rings[i];
 				ptarray_grid_in_place(pa, grid);
+
 				/* Skip bad rings */
-				if (pa->npoints < 4)
+				if (pa->npoints >= 4)
 				{
+					j++;
+				}
+				else
+				{
 					ptarray_free(pa);
-					/* When internal rings collapse, we free */
-					/* then and move on */
-					if (i) continue;
-					/* If external ring collapses, we free */
-					/* it and stop processing */
-					else break;
 				}
-				/* Fill in just the rings we are keeping */
-				ply->rings[j++] = pa;
 			}
 			/* Adjust ring count appropriately */
 			ply->nrings = j;



More information about the postgis-tickets mailing list