From e003325935e385a7416be2cb4e1fb89728e4bfdd Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Mon, 4 Jan 2010 17:55:14 +0000 Subject: [PATCH] Fixed up de-serialization routines to avoid *geoms = malloc(0) cases which were causing free to fail later on down the line (#370) git-svn-id: http://svn.osgeo.org/postgis/trunk@5099 b70326c6-7e19-0410-871a-916f4a2858ee --- liblwgeom/cunit/cu_libgeom.c | 29 ++++++++++++++++++++++++++++- liblwgeom/cunit/cu_libgeom.h | 3 +-- liblwgeom/lwcollection.c | 9 ++++++++- liblwgeom/lwmcurve.c | 10 +++++++++- liblwgeom/lwmline.c | 10 +++++++++- liblwgeom/lwmpoint.c | 15 +++++++++++++-- liblwgeom/lwmpoly.c | 10 +++++++++- liblwgeom/lwmsurface.c | 10 +++++++++- liblwgeom/lwpoly.c | 9 ++++++++- 9 files changed, 94 insertions(+), 11 deletions(-) diff --git a/liblwgeom/cunit/cu_libgeom.c b/liblwgeom/cunit/cu_libgeom.c index a0e1aa48b..5d71152ba 100644 --- a/liblwgeom/cunit/cu_libgeom.c +++ b/liblwgeom/cunit/cu_libgeom.c @@ -35,7 +35,8 @@ CU_pSuite register_libgeom_suite(void) (NULL == CU_add_test(pSuite, "test_lwgeom_count_vertices()", test_lwgeom_count_vertices)) || (NULL == CU_add_test(pSuite, "test_on_gser_lwgeom_count_vertices()", test_on_gser_lwgeom_count_vertices)) || (NULL == CU_add_test(pSuite, "test_geometry_type_from_string()", test_geometry_type_from_string)) || - (NULL == CU_add_test(pSuite, "test_lwcollection_extract()", test_lwcollection_extract)) + (NULL == CU_add_test(pSuite, "test_lwcollection_extract()", test_lwcollection_extract)) || + (NULL == CU_add_test(pSuite, "test_lwgeom_free()", test_lwgeom_free)) ) { @@ -343,3 +344,29 @@ void test_lwcollection_extract(void) lwgeom_free(geom); } + +void test_lwgeom_free(void) +{ + LWGEOM *geom; + + /* Empty geometries don't seem to free properly (#370) */ + geom = lwgeom_from_ewkt("GEOMETRYCOLLECTION EMPTY", PARSER_CHECK_NONE); + CU_ASSERT_EQUAL(TYPE_GETTYPE(geom->type), COLLECTIONTYPE); + lwgeom_free(geom); + + /* Empty geometries don't seem to free properly (#370) */ + geom = lwgeom_from_ewkt("POLYGON EMPTY", PARSER_CHECK_NONE); + CU_ASSERT_EQUAL(TYPE_GETTYPE(geom->type), COLLECTIONTYPE); + lwgeom_free(geom); + + /* Empty geometries don't seem to free properly (#370) */ + geom = lwgeom_from_ewkt("LINESTRING EMPTY", PARSER_CHECK_NONE); + CU_ASSERT_EQUAL(TYPE_GETTYPE(geom->type), COLLECTIONTYPE); + lwgeom_free(geom); + + /* Empty geometries don't seem to free properly (#370) */ + geom = lwgeom_from_ewkt("POINT EMPTY", PARSER_CHECK_NONE); + CU_ASSERT_EQUAL(TYPE_GETTYPE(geom->type), COLLECTIONTYPE); + lwgeom_free(geom); + +} diff --git a/liblwgeom/cunit/cu_libgeom.h b/liblwgeom/cunit/cu_libgeom.h index fca263e00..c319384ec 100644 --- a/liblwgeom/cunit/cu_libgeom.h +++ b/liblwgeom/cunit/cu_libgeom.h @@ -23,7 +23,6 @@ */ - /* Test functions */ void test_typmod_macros(void); void test_flags_macros(void); @@ -35,4 +34,4 @@ void test_gbox_serialized_size(void); void test_lwcollection_extract(void); void test_lwgeom_count_vertices(void); void test_on_gser_lwgeom_count_vertices(void); - +void test_lwgeom_free(void); diff --git a/liblwgeom/lwcollection.c b/liblwgeom/lwcollection.c index 87fe9641d..a268fc2c0 100644 --- a/liblwgeom/lwcollection.c +++ b/liblwgeom/lwcollection.c @@ -115,7 +115,10 @@ lwcollection_deserialize(uchar *srl) result->bbox = lwalloc(sizeof(BOX2DFLOAT4)); memcpy(result->bbox, srl+1, sizeof(BOX2DFLOAT4)); } - else result->bbox = NULL; + else + { + result->bbox = NULL; + } if ( insp->ngeometries ) @@ -126,6 +129,10 @@ lwcollection_deserialize(uchar *srl) result->geoms[i] = lwgeom_deserialize(insp->sub_geoms[i]); } } + else + { + result->geoms = NULL; + } return result; } diff --git a/liblwgeom/lwmcurve.c b/liblwgeom/lwmcurve.c index 5b33fbfbe..bacf15c59 100644 --- a/liblwgeom/lwmcurve.c +++ b/liblwgeom/lwmcurve.c @@ -36,7 +36,15 @@ lwmcurve_deserialize(uchar *srl) result->type = insp->type; result->SRID = insp->SRID; result->ngeoms = insp->ngeometries; - result->geoms = lwalloc(sizeof(LWGEOM *)*insp->ngeometries); + + if( insp->ngeometries ) + { + result->geoms = lwalloc(sizeof(LWGEOM *)*insp->ngeometries); + } + else + { + result->geoms = NULL; + } if (lwgeom_hasBBOX(srl[0])) { diff --git a/liblwgeom/lwmline.c b/liblwgeom/lwmline.c index 10ef57c1f..f3e82b189 100644 --- a/liblwgeom/lwmline.c +++ b/liblwgeom/lwmline.c @@ -42,7 +42,15 @@ lwmline_deserialize(uchar *srl) result->type = insp->type; result->SRID = insp->SRID; result->ngeoms = insp->ngeometries; - result->geoms = lwalloc(sizeof(LWLINE *)*insp->ngeometries); + + if( insp->ngeometries ) + { + result->geoms = lwalloc(sizeof(LWLINE *)*insp->ngeometries); + } + else + { + result->geoms = NULL; + } if (lwgeom_hasBBOX(srl[0])) { diff --git a/liblwgeom/lwmpoint.c b/liblwgeom/lwmpoint.c index 99e762312..fc23b6ef3 100644 --- a/liblwgeom/lwmpoint.c +++ b/liblwgeom/lwmpoint.c @@ -43,14 +43,25 @@ lwmpoint_deserialize(uchar *srl) result->type = insp->type; result->SRID = insp->SRID; result->ngeoms = insp->ngeometries; - result->geoms = lwalloc(sizeof(LWPOINT *)*result->ngeoms); + + if ( insp->ngeometries ) + { + result->geoms = lwalloc(sizeof(LWPOINT *)*insp->ngeometries); + } + else + { + result->geoms = NULL; + } if (lwgeom_hasBBOX(srl[0])) { result->bbox = lwalloc(sizeof(BOX2DFLOAT4)); memcpy(result->bbox, srl+1, sizeof(BOX2DFLOAT4)); } - else result->bbox = NULL; + else + { + result->bbox = NULL; + } for (i=0; ingeometries; i++) { diff --git a/liblwgeom/lwmpoly.c b/liblwgeom/lwmpoly.c index 4303b151e..4c328e94f 100644 --- a/liblwgeom/lwmpoly.c +++ b/liblwgeom/lwmpoly.c @@ -46,7 +46,15 @@ lwmpoly_deserialize(uchar *srl) result->type = insp->type; result->SRID = insp->SRID; result->ngeoms = insp->ngeometries; - result->geoms = lwalloc(sizeof(LWPOLY *)*insp->ngeometries); + + if( insp->ngeometries ) + { + result->geoms = lwalloc(sizeof(LWPOLY *)*insp->ngeometries); + } + else + { + result->geoms = NULL; + } if (lwgeom_hasBBOX(srl[0])) { diff --git a/liblwgeom/lwmsurface.c b/liblwgeom/lwmsurface.c index e21fd1cde..2bce28986 100644 --- a/liblwgeom/lwmsurface.c +++ b/liblwgeom/lwmsurface.c @@ -39,7 +39,15 @@ lwmsurface_deserialize(uchar *srl) result->type = insp->type; result->SRID = insp->SRID; result->ngeoms = insp->ngeometries; - result->geoms = lwalloc(sizeof(LWPOLY *)*insp->ngeometries); + + if( insp->ngeometries ) + { + result->geoms = lwalloc(sizeof(LWPOLY *)*insp->ngeometries); + } + else + { + result->geoms = NULL; + } if (lwgeom_hasBBOX(srl[0])) { diff --git a/liblwgeom/lwpoly.c b/liblwgeom/lwpoly.c index 794c39a6c..77602c64f 100644 --- a/liblwgeom/lwpoly.c +++ b/liblwgeom/lwpoly.c @@ -128,7 +128,14 @@ lwpoly_deserialize(uchar *serialized_form) nrings = lw_get_uint32(loc); result->nrings = nrings; loc +=4; - result->rings = (POINTARRAY**) lwalloc(nrings* sizeof(POINTARRAY*)); + if( nrings ) + { + result->rings = (POINTARRAY**) lwalloc(nrings* sizeof(POINTARRAY*)); + } + else + { + result->rings = NULL; + } for (t =0;t