]> granicus.if.org Git - postgis/commitdiff
#2775 lwline_from_lwmpoint leaks memory
authorPaul Ramsey <pramsey@cleverelephant.ca>
Tue, 24 Jun 2014 22:45:39 +0000 (22:45 +0000)
committerPaul Ramsey <pramsey@cleverelephant.ca>
Tue, 24 Jun 2014 22:45:39 +0000 (22:45 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@12651 b70326c6-7e19-0410-871a-916f4a2858ee

liblwgeom/cunit/cu_libgeom.c
liblwgeom/lwline.c

index 32b3c583b0b34b1ee4dd7f0c3df1c645e3296cae..cd89db01853511d575fa96bcd70bf819febe1308 100644 (file)
@@ -946,6 +946,23 @@ static void test_lwgeom_as_curve(void)
 
 }
 
+static void test_lwline_from_lwmpoint(void)
+{
+       LWLINE *line;
+       LWMPOINT *mpoint;
+
+//     LWLINE *
+//     lwline_from_lwmpoint(int srid, LWMPOINT *mpoint)
+
+       mpoint = (LWMPOINT*)lwgeom_from_wkt("MULTIPOINT(0 0, 0 1, 1 1, 1 2, 2 2)", LW_PARSER_CHECK_NONE);
+       line = lwline_from_lwmpoint(SRID_DEFAULT, mpoint);
+       CU_ASSERT_EQUAL(line->points->npoints, mpoint->ngeoms);
+       CU_ASSERT_DOUBLE_EQUAL(lwline_length_2d(line), 4.0, 0.000001);
+       
+       lwline_free(line);
+       lwmpoint_free(mpoint);
+}
+
 /*
 ** Used by test harness to register the tests in this file.
 */
@@ -969,6 +986,7 @@ CU_TestInfo libgeom_tests[] =
        PG_TEST(test_lwgeom_calculate_gbox),
        PG_TEST(test_lwgeom_is_empty),
        PG_TEST(test_lwgeom_same),
+       PG_TEST(test_lwline_from_lwmpoint),
        PG_TEST(test_lwgeom_as_curve),
        CU_TEST_INFO_NULL
 };
index 93e23e3ad54c9393936fddc0c816e1ff410b42a5..e5084d3972e5da861b536da1653c15479a900351 100644 (file)
@@ -260,32 +260,28 @@ LWLINE *
 lwline_from_lwmpoint(int srid, LWMPOINT *mpoint)
 {
        uint32_t i;
-       POINTARRAY *pa;
-       char zmflag = FLAGS_GET_ZM(mpoint->flags);
-       size_t ptsize, size;
-       uint8_t *newpoints, *ptr;
-
-       if ( zmflag == 0 ) ptsize=2*sizeof(double);
-       else if ( zmflag == 3 ) ptsize=4*sizeof(double);
-       else ptsize=3*sizeof(double);
+       POINTARRAY *pa = NULL;
+       LWGEOM *lwgeom = (LWGEOM*)mpoint;
+       POINT4D pt;
 
-       /* Allocate space for output points */
-       size = ptsize*mpoint->ngeoms;
-       newpoints = lwalloc(size);
-       memset(newpoints, 0, size);
+       char hasz = lwgeom_has_z(lwgeom);
+       char hasm = lwgeom_has_m(lwgeom);
+       uint32_t npoints = mpoint->ngeoms;
 
-       ptr=newpoints;
-       for (i=0; i<mpoint->ngeoms; i++)
+       if ( lwgeom_is_empty(lwgeom) ) 
        {
-               memcpy(ptr,
-                      getPoint_internal(mpoint->geoms[i]->point, 0),
-                      ptsize);
-               ptr+=ptsize;
+               return lwline_construct_empty(srid, hasz, hasm);
        }
 
-       pa = ptarray_construct_reference_data(zmflag&2, zmflag&1, mpoint->ngeoms, newpoints);
+       pa = ptarray_construct(hasz, hasm, npoints);
+
+       for (i=0; i < npoints; i++)
+       {
+               getPoint4d_p(mpoint->geoms[i]->point, 0, &pt);
+               ptarray_set_point4d(pa, i, &pt);
+       }
        
-       LWDEBUGF(3, "lwline_from_lwmpoint: constructed pointarray for %d points, %d zmflag", mpoint->ngeoms, zmflag);
+       LWDEBUGF(3, "lwline_from_lwmpoint: constructed pointarray for %d points", mpoint->ngeoms);
 
        return lwline_construct(srid, NULL, pa);
 }