From: Darafei Praliaskouski Date: Sun, 14 Oct 2018 23:13:39 +0000 (+0000) Subject: Match memory management functions, so that lwalloc is not freed with pfree. X-Git-Tag: 3.0.0alpha1~348 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e30b1bcf33e5e75ff0a0ef1a2473f32a214eb6f3;p=postgis Match memory management functions, so that lwalloc is not freed with pfree. Currently swapping lwalloc to be malloc instead of palloc results in non-functional build. git-svn-id: http://svn.osgeo.org/postgis/trunk@16897 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/liblwgeom/g_box.c b/liblwgeom/g_box.c index 33914bee4..5b6652ceb 100644 --- a/liblwgeom/g_box.c +++ b/liblwgeom/g_box.c @@ -440,6 +440,7 @@ GBOX* gbox_copy(const GBOX *box) void gbox_duplicate(const GBOX *original, GBOX *duplicate) { assert(duplicate); + assert(original); memcpy(duplicate, original, sizeof(GBOX)); } diff --git a/liblwgeom/lwgeom_api.c b/liblwgeom/lwgeom_api.c index 67c12071f..27515ee5d 100644 --- a/liblwgeom/lwgeom_api.c +++ b/liblwgeom/lwgeom_api.c @@ -405,7 +405,7 @@ ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d) { uint8_t *ptr; assert(n < pa->npoints); - ptr=getPoint_internal(pa, n); + ptr = getPoint_internal(pa, n); switch ( FLAGS_GET_ZM(pa->flags) ) { case 3: diff --git a/libpgcommon/lwgeom_pg.c b/libpgcommon/lwgeom_pg.c index d4836db47..1ee41f0cb 100644 --- a/libpgcommon/lwgeom_pg.c +++ b/libpgcommon/lwgeom_pg.c @@ -73,7 +73,6 @@ pg_unparser_errhint(LWGEOM_UNPARSER_RESULT *lwg_unparser_result) elog(ERROR, "%s", lwg_unparser_result->message); } - static void * pg_alloc(size_t size) { @@ -169,14 +168,13 @@ pg_install_lwgeom_handlers(void) { /* install PostgreSQL handlers */ lwgeom_set_handlers(pg_alloc, pg_realloc, pg_free, pg_error, pg_notice); + /* + If you want to try with malloc: + lwgeom_set_handlers(NULL, NULL, NULL, pg_error, pg_notice); + */ lwgeom_set_debuglogger(pg_debug); } -/** -* Utility method to call the serialization and then set the -* PgSQL varsize header appropriately with the serialized size. -*/ - /** * Utility method to call the serialization and then set the * PgSQL varsize header appropriately with the serialized size. diff --git a/postgis/lwgeom_functions_basic.c b/postgis/lwgeom_functions_basic.c index 6a8c265e0..98ce3ed4b 100644 --- a/postgis/lwgeom_functions_basic.c +++ b/postgis/lwgeom_functions_basic.c @@ -143,7 +143,7 @@ Datum LWGEOM_summary(PG_FUNCTION_ARGS) /* create a text obj to return */ mytext = cstring_to_text(result); - pfree(result); + lwfree(result); PG_FREE_IF_COPY(geom,0); PG_RETURN_TEXT_P(mytext); @@ -2348,7 +2348,7 @@ Datum LWGEOM_asEWKT(PG_FUNCTION_ARGS) /* Write to text and free the WKT */ result = cstring_to_text(wkt); - pfree(wkt); + lwfree(wkt); /* Return the text */ PG_FREE_IF_COPY(geom, 0); diff --git a/postgis/lwgeom_geos.c b/postgis/lwgeom_geos.c index 2219f89b5..444d4c308 100644 --- a/postgis/lwgeom_geos.c +++ b/postgis/lwgeom_geos.c @@ -2936,7 +2936,7 @@ Datum clusterintersecting_garray(PG_FUNCTION_ARGS) result_array_data[i] = PointerGetDatum(GEOS2POSTGIS(geos_results[i], is3d)); GEOSGeom_destroy(geos_results[i]); } - pfree(geos_results); + lwfree(geos_results); get_typlenbyvalalign(array->elemtype, &elmlen, &elmbyval, &elmalign); result = construct_array(result_array_data, nclusters, array->elemtype, elmlen, elmbyval, elmalign); @@ -3012,7 +3012,7 @@ Datum cluster_within_distance_garray(PG_FUNCTION_ARGS) result_array_data[i] = PointerGetDatum(gserialized_from_lwgeom(lw_results[i], NULL)); lwgeom_free(lw_results[i]); } - pfree(lw_results); + lwfree(lw_results); get_typlenbyvalalign(array->elemtype, &elmlen, &elmbyval, &elmalign); result = construct_array(result_array_data, nclusters, array->elemtype, elmlen, elmbyval, elmalign); diff --git a/postgis/lwgeom_in_gml.c b/postgis/lwgeom_in_gml.c index 0e7f43aea..6603a177c 100644 --- a/postgis/lwgeom_in_gml.c +++ b/postgis/lwgeom_in_gml.c @@ -63,7 +63,7 @@ Datum geom_from_gml(PG_FUNCTION_ARGS); -static LWGEOM* lwgeom_from_gml(const char *wkt); +static LWGEOM *lwgeom_from_gml(const char *wkt, int xml_size); static LWGEOM* parse_gml(xmlNodePtr xnode, bool *hasz, int *root_srid); typedef struct struct_gmlSrs @@ -102,17 +102,18 @@ Datum geom_from_gml(PG_FUNCTION_ARGS) LWGEOM *lwgeom; char *xml; int root_srid=SRID_UNKNOWN; - + int xml_size; /* Get the GML stream */ if (PG_ARGISNULL(0)) PG_RETURN_NULL(); xml_input = PG_GETARG_TEXT_P(0); xml = text_to_cstring(xml_input); + xml_size = VARSIZE(xml_input) - VARHDRSZ; /* Zero for undefined */ root_srid = PG_GETARG_INT32(1); - lwgeom = lwgeom_from_gml(xml); + lwgeom = lwgeom_from_gml(xml, xml_size); if ( root_srid != SRID_UNKNOWN ) lwgeom->srid = root_srid; @@ -1791,23 +1792,33 @@ static LWGEOM* parse_gml_coll(xmlNodePtr xnode, bool *hasz, int *root_srid) /** * Read GML */ -static LWGEOM* lwgeom_from_gml(const char* xml) +static LWGEOM * +lwgeom_from_gml(const char *xml, int xml_size) { xmlDocPtr xmldoc; xmlNodePtr xmlroot=NULL; - int xml_size = strlen(xml); - LWGEOM *lwgeom; + LWGEOM *lwgeom = NULL; bool hasz=true; int root_srid=SRID_UNKNOWN; /* Begin to Parse XML doc */ xmlInitParser(); - xmldoc = xmlReadMemory(xml, xml_size, NULL, NULL, XML_PARSE_SAX1); - if (!xmldoc || (xmlroot = xmlDocGetRootElement(xmldoc)) == NULL) + + xmldoc = xmlReadMemory(xml, xml_size, NULL, NULL, XML_PARSE_SAX1); + if (!xmldoc) + { + xmlCleanupParser(); + gml_lwpgerror("invalid GML representation", 1); + return NULL; + } + + xmlroot = xmlDocGetRootElement(xmldoc); + if (!xmlroot) { xmlFreeDoc(xmldoc); xmlCleanupParser(); gml_lwpgerror("invalid GML representation", 1); + return NULL; } lwgeom = parse_gml(xmlroot, &hasz, &root_srid); @@ -1816,7 +1827,6 @@ static LWGEOM* lwgeom_from_gml(const char* xml) xmlCleanupParser(); /* shouldn't we be releasing xmldoc too here ? */ - if ( root_srid != SRID_UNKNOWN ) lwgeom->srid = root_srid; diff --git a/postgis/lwgeom_in_kml.c b/postgis/lwgeom_in_kml.c index a3db8dcd6..52a7820ef 100644 --- a/postgis/lwgeom_in_kml.c +++ b/postgis/lwgeom_in_kml.c @@ -82,7 +82,6 @@ Datum geom_from_kml(PG_FUNCTION_ARGS) bool hasz=true; xmlNodePtr xmlroot=NULL; - /* Get the KML stream */ if (PG_ARGISNULL(0)) PG_RETURN_NULL(); xml_input = PG_GETARG_TEXT_P(0); diff --git a/postgis/lwgeom_inout.c b/postgis/lwgeom_inout.c index 7bb1b185a..dca963955 100644 --- a/postgis/lwgeom_inout.c +++ b/postgis/lwgeom_inout.c @@ -136,7 +136,7 @@ Datum LWGEOM_in(PG_FUNCTION_ARGS) if ( srid ) lwgeom_set_srid(lwgeom, srid); /* Add a bbox if necessary */ if ( lwgeom_needs_bbox(lwgeom) ) lwgeom_add_bbox(lwgeom); - pfree(wkb); + lwfree(wkb); ret = geometry_serialize(lwgeom); lwgeom_free(lwgeom); } @@ -322,7 +322,7 @@ Datum LWGEOM_asHEXEWKB(PG_FUNCTION_ARGS) SET_VARSIZE(result, text_size); /* Clean up and return */ - pfree(hexwkb); + lwfree(hexwkb); PG_FREE_IF_COPY(geom, 0); PG_RETURN_TEXT_P(result); } @@ -351,7 +351,7 @@ Datum LWGEOM_to_text(PG_FUNCTION_ARGS) /* Copy into text obect */ result = cstring_to_text(hexwkb); - pfree(hexwkb); + lwfree(hexwkb); /* Clean up and return */ PG_FREE_IF_COPY(geom, 0); @@ -456,7 +456,7 @@ Datum WKBFromLWGEOM(PG_FUNCTION_ARGS) SET_VARSIZE(result, wkb_size+VARHDRSZ); /* Clean up and return */ - pfree(wkb); + lwfree(wkb); PG_FREE_IF_COPY(geom, 0); PG_RETURN_BYTEA_P(result); } diff --git a/postgis/lwgeom_ogc.c b/postgis/lwgeom_ogc.c index 73a020b35..71c24b466 100644 --- a/postgis/lwgeom_ogc.c +++ b/postgis/lwgeom_ogc.c @@ -879,7 +879,7 @@ Datum LWGEOM_asText(PG_FUNCTION_ARGS) /* Write to text and free the WKT */ result = cstring_to_text(wkt); - pfree(wkt); + lwfree(wkt); /* Return the text */ PG_FREE_IF_COPY(geom, 0); @@ -926,7 +926,7 @@ Datum LWGEOM_asBinary(PG_FUNCTION_ARGS) result = palloc(wkb_size + VARHDRSZ); memcpy(VARDATA(result), wkb, wkb_size); SET_VARSIZE(result, wkb_size + VARHDRSZ); - pfree(wkb); + lwfree(wkb); /* Return the text */ PG_FREE_IF_COPY(geom, 0); diff --git a/postgis/lwgeom_window.c b/postgis/lwgeom_window.c index d44b53839..fe36875f6 100644 --- a/postgis/lwgeom_window.c +++ b/postgis/lwgeom_window.c @@ -250,7 +250,7 @@ Datum ST_ClusterKMeans(PG_FUNCTION_ARGS) /* Safe the result */ memcpy(context->result, r, sizeof(int) * N); - pfree(r); + lwfree(r); context->isdone = true; }