]> granicus.if.org Git - postgis/commitdiff
Fix small memory leak in lwline_split_by_line (#2528)
authorSandro Santilli <strk@keybit.net>
Wed, 6 Nov 2013 09:39:29 +0000 (09:39 +0000)
committerSandro Santilli <strk@keybit.net>
Wed, 6 Nov 2013 09:39:29 +0000 (09:39 +0000)
Thanks Alessandro Furieri for the report and test

git-svn-id: http://svn.osgeo.org/postgis/trunk@12094 b70326c6-7e19-0410-871a-916f4a2858ee

liblwgeom/cunit/cu_split.c
liblwgeom/lwgeom_geos_split.c

index a7646c31100060dec8fb6c7f8c04708174a8fdd2..fa963426691dc023055d482c59de5e2ff3624055 100644 (file)
@@ -122,6 +122,38 @@ static void test_lwgeom_split(void)
        lwgeom_free(ret);
        lwgeom_free(geom);
        lwgeom_free(blade);
+
+  /* See #2528 (1) -- memory leak test, needs valgrind to check */
+  geom = lwgeom_from_wkt("SRID=1;LINESTRING(0 1,10 1)", LW_PARSER_CHECK_NONE);
+  CU_ASSERT(geom != NULL);
+  blade = lwgeom_from_wkt("LINESTRING(7 0,7 3)", LW_PARSER_CHECK_NONE);
+  ret = lwgeom_split(geom, blade);
+  CU_ASSERT(ret != NULL);
+  wkt = lwgeom_to_ewkt(ret);
+       in_wkt = "SRID=1;GEOMETRYCOLLECTION(LINESTRING(0 1,7 1),LINESTRING(7 1,10 1))";
+  if (strcmp(in_wkt, wkt))
+                fprintf(stderr, "\nExp:  %s\nObt:  %s\n", in_wkt, wkt);
+  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
+  lwfree(wkt);
+       lwgeom_free(ret);
+       lwgeom_free(geom);
+       lwgeom_free(blade);
+
+  /* See #2528 (2) -- memory leak test, needs valgrind to check */
+  geom = lwgeom_from_wkt("SRID=1;POLYGON((0 1, 10 1, 10 10, 0 10, 0 1))", LW_PARSER_CHECK_NONE);
+  CU_ASSERT(geom != NULL);
+  blade = lwgeom_from_wkt("LINESTRING(7 0,7 20)", LW_PARSER_CHECK_NONE);
+  ret = lwgeom_split(geom, blade);
+  CU_ASSERT(ret != NULL);
+  wkt = lwgeom_to_ewkt(ret);
+       in_wkt = "SRID=1;GEOMETRYCOLLECTION(POLYGON((7 1,0 1,0 10,7 10,7 1)),POLYGON((7 10,10 10,10 1,7 1,7 10)))";
+  if (strcmp(in_wkt, wkt))
+                fprintf(stderr, "\nExp:  %s\nObt:  %s\n", in_wkt, wkt);
+  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
+  lwfree(wkt);
+       lwgeom_free(ret);
+       lwgeom_free(geom);
+       lwgeom_free(blade);
 }
 
 
index 7bf9a153af7b3a6b8f5d8da5496a25b0e0b7958c..fa8519040e65fb4699583b39266b581f6af72d4a 100644 (file)
@@ -118,7 +118,8 @@ lwline_split_by_line(const LWLINE* lwline_in, const LWLINE* blade_in)
                return NULL;
        }
 
-       if ( ! lwtype_is_collection(diff->type) )
+       out = lwgeom_as_lwcollection(diff);
+       if ( ! out )
        {
                components = lwalloc(sizeof(LWGEOM*)*1);
                components[0] = diff;
@@ -127,9 +128,10 @@ lwline_split_by_line(const LWLINE* lwline_in, const LWLINE* blade_in)
        }
        else
        {
-               out = lwcollection_construct(COLLECTIONTYPE, lwline_in->srid,
-                                            NULL, ((LWCOLLECTION*)diff)->ngeoms,
-                                            ((LWCOLLECTION*)diff)->geoms);
+         /* Set SRID */
+               lwgeom_set_srid((LWGEOM*)out, lwline_in->srid);
+         /* Force collection type */
+         out->type = COLLECTIONTYPE;
        }
 
 
@@ -337,7 +339,7 @@ lwpoly_split_by_line(const LWPOLY* lwpoly_in, const LWLINE* blade_in)
        out = lwcollection_construct_empty(COLLECTIONTYPE, lwpoly_in->srid,
                                     hasZ, 0);
        /* Allocate space for all polys */
-       out->geoms = lwalloc(sizeof(LWGEOM*)*n);
+       out->geoms = lwrealloc(out->geoms, sizeof(LWGEOM*)*n);
        assert(0 == out->ngeoms);
        for (i=0; i<n; ++i)
        {