From 7a02d699783ae1d70131144dc0796c5c202b479f Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Wed, 1 Oct 2014 14:36:41 +0000 Subject: [PATCH] Fix yet another memory leak in lwgeom_make_valid (#2947) The leak occurs on invalid single-component collection input. git-svn-id: http://svn.osgeo.org/postgis/trunk@13025 b70326c6-7e19-0410-871a-916f4a2858ee --- liblwgeom/liblwgeom_internal.h | 1 + liblwgeom/lwgeom.c | 2 +- liblwgeom/lwgeom_geos_clean.c | 19 +++++++++++++------ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/liblwgeom/liblwgeom_internal.h b/liblwgeom/liblwgeom_internal.h index f029cbb07..61a747cd5 100644 --- a/liblwgeom/liblwgeom_internal.h +++ b/liblwgeom/liblwgeom_internal.h @@ -403,5 +403,6 @@ int gbox_centroid(const GBOX* gbox, POINT2D* out); /* Utilities */ extern void trim_trailing_zeros(char *num); +extern uint8_t MULTITYPE[NUMTYPES]; #endif /* _LIBLWGEOM_INTERNAL_H */ diff --git a/liblwgeom/lwgeom.c b/liblwgeom/lwgeom.c index f57593c85..ad76eeee9 100644 --- a/liblwgeom/lwgeom.c +++ b/liblwgeom/lwgeom.c @@ -261,7 +261,7 @@ LWGEOM *lwpoint_as_lwgeom(const LWPOINT *obj) /** ** Look-up for the correct MULTI* type promotion for singleton types. */ -static uint8_t MULTITYPE[NUMTYPES] = +uint8_t MULTITYPE[NUMTYPES] = { 0, MULTIPOINTTYPE, /* 1 */ diff --git a/liblwgeom/lwgeom_geos_clean.c b/liblwgeom/lwgeom_geos_clean.c index 3e0b5af87..e7d916135 100644 --- a/liblwgeom/lwgeom_geos_clean.c +++ b/liblwgeom/lwgeom_geos_clean.c @@ -997,7 +997,7 @@ lwgeom_make_valid(LWGEOM* lwgeom_in) int is3d; GEOSGeom geosgeom; GEOSGeometry* geosout; - LWGEOM *lwgeom_out, *lwgeom_tmp; + LWGEOM *lwgeom_out; is3d = FLAGS_GET_Z(lwgeom_in->flags); @@ -1052,12 +1052,19 @@ lwgeom_make_valid(LWGEOM* lwgeom_in) GEOSGeom_destroy(geosout); if ( lwgeom_is_collection(lwgeom_in) && ! lwgeom_is_collection(lwgeom_out) ) - { + {{ + LWGEOM **ogeoms = lwalloc(sizeof(LWGEOM*)); + LWGEOM *ogeom; LWDEBUG(3, "lwgeom_make_valid: forcing multi"); - lwgeom_tmp = lwgeom_as_multi(lwgeom_out); - lwfree(lwgeom_out); /* note: only frees the wrapper, not the content */ - lwgeom_out = lwgeom_tmp; - } + /* NOTE: this is safe because lwgeom_out is surely not lwgeom_in or + * otherwise we couldn't have a collection and a non-collection */ + assert(lwgeom_in != lwgeom_out); + ogeoms[0] = lwgeom_out; + ogeom = (LWGEOM *)lwcollection_construct(MULTITYPE[lwgeom_out->type], + lwgeom_out->srid, lwgeom_out->bbox, 1, ogeoms); + lwgeom_out->bbox = NULL; + lwgeom_out = ogeom; + }} lwgeom_out->srid = lwgeom_in->srid; return lwgeom_out; -- 2.40.0