[postgis-tickets] r16897 - Match memory management functions, so that lwalloc is not freed with pfree.

Darafei komzpa at gmail.com
Sun Oct 14 04:13:39 PDT 2018


Author: komzpa
Date: 2018-10-14 16:13:39 -0700 (Sun, 14 Oct 2018)
New Revision: 16897

Modified:
   trunk/liblwgeom/g_box.c
   trunk/liblwgeom/lwgeom_api.c
   trunk/libpgcommon/lwgeom_pg.c
   trunk/postgis/lwgeom_functions_basic.c
   trunk/postgis/lwgeom_geos.c
   trunk/postgis/lwgeom_in_gml.c
   trunk/postgis/lwgeom_in_kml.c
   trunk/postgis/lwgeom_inout.c
   trunk/postgis/lwgeom_ogc.c
   trunk/postgis/lwgeom_window.c
Log:
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.



Modified: trunk/liblwgeom/g_box.c
===================================================================
--- trunk/liblwgeom/g_box.c	2018-10-14 23:08:46 UTC (rev 16896)
+++ trunk/liblwgeom/g_box.c	2018-10-14 23:13:39 UTC (rev 16897)
@@ -440,6 +440,7 @@
 void gbox_duplicate(const GBOX *original, GBOX *duplicate)
 {
 	assert(duplicate);
+	assert(original);
 	memcpy(duplicate, original, sizeof(GBOX));
 }
 

Modified: trunk/liblwgeom/lwgeom_api.c
===================================================================
--- trunk/liblwgeom/lwgeom_api.c	2018-10-14 23:08:46 UTC (rev 16896)
+++ trunk/liblwgeom/lwgeom_api.c	2018-10-14 23:13:39 UTC (rev 16897)
@@ -405,7 +405,7 @@
 {
 	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:

Modified: trunk/libpgcommon/lwgeom_pg.c
===================================================================
--- trunk/libpgcommon/lwgeom_pg.c	2018-10-14 23:08:46 UTC (rev 16896)
+++ trunk/libpgcommon/lwgeom_pg.c	2018-10-14 23:13:39 UTC (rev 16897)
@@ -73,7 +73,6 @@
 	elog(ERROR, "%s", lwg_unparser_result->message);
 }
 
-
 static void *
 pg_alloc(size_t size)
 {
@@ -169,6 +168,10 @@
 {
 	/* 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);
 }
 
@@ -176,11 +179,6 @@
 * 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.
-*/
 GSERIALIZED* geography_serialize(LWGEOM *lwgeom)
 {
 	size_t ret_size = 0;

Modified: trunk/postgis/lwgeom_functions_basic.c
===================================================================
--- trunk/postgis/lwgeom_functions_basic.c	2018-10-14 23:08:46 UTC (rev 16896)
+++ trunk/postgis/lwgeom_functions_basic.c	2018-10-14 23:13:39 UTC (rev 16897)
@@ -143,7 +143,7 @@
 
 	/* 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 @@
 
 	/* 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);

Modified: trunk/postgis/lwgeom_geos.c
===================================================================
--- trunk/postgis/lwgeom_geos.c	2018-10-14 23:08:46 UTC (rev 16896)
+++ trunk/postgis/lwgeom_geos.c	2018-10-14 23:13:39 UTC (rev 16897)
@@ -2936,7 +2936,7 @@
 		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 @@
 		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);

Modified: trunk/postgis/lwgeom_in_gml.c
===================================================================
--- trunk/postgis/lwgeom_in_gml.c	2018-10-14 23:08:46 UTC (rev 16896)
+++ trunk/postgis/lwgeom_in_gml.c	2018-10-14 23:13:39 UTC (rev 16897)
@@ -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 @@
 	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 @@
 /**
  * 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 @@
 	xmlCleanupParser();
 	/* shouldn't we be releasing xmldoc too here ? */
 
-
 	if ( root_srid != SRID_UNKNOWN )
 		lwgeom->srid = root_srid;
 

Modified: trunk/postgis/lwgeom_in_kml.c
===================================================================
--- trunk/postgis/lwgeom_in_kml.c	2018-10-14 23:08:46 UTC (rev 16896)
+++ trunk/postgis/lwgeom_in_kml.c	2018-10-14 23:13:39 UTC (rev 16897)
@@ -82,7 +82,6 @@
 	bool hasz=true;
 	xmlNodePtr xmlroot=NULL;
 
-
 	/* Get the KML stream */
 	if (PG_ARGISNULL(0)) PG_RETURN_NULL();
 	xml_input = PG_GETARG_TEXT_P(0);

Modified: trunk/postgis/lwgeom_inout.c
===================================================================
--- trunk/postgis/lwgeom_inout.c	2018-10-14 23:08:46 UTC (rev 16896)
+++ trunk/postgis/lwgeom_inout.c	2018-10-14 23:13:39 UTC (rev 16897)
@@ -136,7 +136,7 @@
 		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 @@
 	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 @@
 
 	/* 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 @@
 	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);
 }

Modified: trunk/postgis/lwgeom_ogc.c
===================================================================
--- trunk/postgis/lwgeom_ogc.c	2018-10-14 23:08:46 UTC (rev 16896)
+++ trunk/postgis/lwgeom_ogc.c	2018-10-14 23:13:39 UTC (rev 16897)
@@ -879,7 +879,7 @@
 
 	/* 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 @@
 	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);

Modified: trunk/postgis/lwgeom_window.c
===================================================================
--- trunk/postgis/lwgeom_window.c	2018-10-14 23:08:46 UTC (rev 16896)
+++ trunk/postgis/lwgeom_window.c	2018-10-14 23:13:39 UTC (rev 16897)
@@ -250,7 +250,7 @@
 
 		/* Safe the result */
 		memcpy(context->result, r, sizeof(int) * N);
-		pfree(r);
+		lwfree(r);
 		context->isdone = true;
 	}
 



More information about the postgis-tickets mailing list