From: Bborie Park Date: Sat, 21 Jun 2014 17:30:13 +0000 (+0000) Subject: fix premature memory free in RASTER_getBandPath (ST_BandPath) X-Git-Tag: 2.2.0rc1~1077 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=741e3d50ede3d42999c3edb826a2ce279ca6c40c;p=postgis fix premature memory free in RASTER_getBandPath (ST_BandPath) git-svn-id: http://svn.osgeo.org/postgis/trunk@12631 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/NEWS b/NEWS index 15732178a..b0c62dd5a 100644 --- a/NEWS +++ b/NEWS @@ -69,6 +69,7 @@ PostGIS 2.2.0 - #2634, regression in sphere distance code - #2697, invalid GeoJSON Polygon input crashes server process - #2706, ST_DumpPoints of EMPTY geometries crashes server + - #2772, Premature memory free in RASTER_getBandPath (ST_BandPath) * Code refactoring * diff --git a/raster/rt_pg/rtpg_band_properties.c b/raster/rt_pg/rtpg_band_properties.c index ea5d25355..7808c1038 100644 --- a/raster/rt_pg/rtpg_band_properties.c +++ b/raster/rt_pg/rtpg_band_properties.c @@ -284,56 +284,61 @@ Datum RASTER_bandIsNoData(PG_FUNCTION_ARGS) PG_FUNCTION_INFO_V1(RASTER_getBandPath); Datum RASTER_getBandPath(PG_FUNCTION_ARGS) { - rt_pgraster *pgraster = NULL; - rt_raster raster = NULL; - rt_band band = NULL; - int32_t bandindex; - const char *bandpath; - text *result; + rt_pgraster *pgraster = NULL; + rt_raster raster = NULL; + rt_band band = NULL; + int32_t bandindex; + const char *bandpath; + text *result; - /* Index is 1-based */ - bandindex = PG_GETARG_INT32(1); - if ( bandindex < 1 ) { - elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL"); - PG_RETURN_NULL(); - } + /* Index is 1-based */ + bandindex = PG_GETARG_INT32(1); + if ( bandindex < 1 ) { + elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL"); + PG_RETURN_NULL(); + } - /* Deserialize raster */ - if (PG_ARGISNULL(0)) PG_RETURN_NULL(); - pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + /* Deserialize raster */ + if (PG_ARGISNULL(0)) PG_RETURN_NULL(); + pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - raster = rt_raster_deserialize(pgraster, FALSE); - if ( ! raster ) { - PG_FREE_IF_COPY(pgraster, 0); - elog(ERROR, "RASTER_getBandPath: Could not deserialize raster"); - PG_RETURN_NULL(); - } + raster = rt_raster_deserialize(pgraster, FALSE); + if (!raster) { + PG_FREE_IF_COPY(pgraster, 0); + elog(ERROR, "RASTER_getBandPath: Could not deserialize raster"); + PG_RETURN_NULL(); + } - /* Fetch requested band and its nodata value */ - band = rt_raster_get_band(raster, bandindex - 1); - if ( ! band ) { - elog(NOTICE, "Could not find raster band of index %d when getting band path. Returning NULL", bandindex); - rt_raster_destroy(raster); - PG_FREE_IF_COPY(pgraster, 0); - PG_RETURN_NULL(); - } + /* Fetch requested band */ + band = rt_raster_get_band(raster, bandindex - 1); + if (!band) { + elog( + NOTICE, + "Could not find raster band of index %d when getting band path. Returning NULL", + bandindex + ); + rt_raster_destroy(raster); + PG_FREE_IF_COPY(pgraster, 0); + PG_RETURN_NULL(); + } - bandpath = rt_band_get_ext_path(band); + bandpath = rt_band_get_ext_path(band); + if (!bandpath) { rt_band_destroy(band); - rt_raster_destroy(raster); - PG_FREE_IF_COPY(pgraster, 0); - if ( ! bandpath ) - { - PG_RETURN_NULL(); - } - - result = (text *) palloc(VARHDRSZ + strlen(bandpath) + 1); + rt_raster_destroy(raster); + PG_FREE_IF_COPY(pgraster, 0); + PG_RETURN_NULL(); + } - SET_VARSIZE(result, VARHDRSZ + strlen(bandpath) + 1); + result = (text *) palloc(VARHDRSZ + strlen(bandpath) + 1); + SET_VARSIZE(result, VARHDRSZ + strlen(bandpath) + 1); + strcpy((char *) VARDATA(result), bandpath); - strcpy((char *) VARDATA(result), bandpath); + rt_band_destroy(band); + rt_raster_destroy(raster); + PG_FREE_IF_COPY(pgraster, 0); - PG_RETURN_TEXT_P(result); + PG_RETURN_TEXT_P(result); } /** diff --git a/raster/test/regress/rt_utility.sql b/raster/test/regress/rt_utility.sql index 61b1a2dc8..d8dee3da5 100644 --- a/raster/test/regress/rt_utility.sql +++ b/raster/test/regress/rt_utility.sql @@ -434,5 +434,16 @@ SELECT FROM raster_outdb_template ORDER BY rid; +----------------------------------------------------------------------- +-- st_memsize() +----------------------------------------------------------------------- + SELECT 'ms1', ST_MemSize(ST_MakeEmptyRaster(10, 10, 0, 0, 1, -1, 0, 0, 0)); SELECT 'ms2', ST_MemSize(rast) from raster_outdb_template order by rid; + +----------------------------------------------------------------------- +-- st_bandpath() +----------------------------------------------------------------------- + +SELECT 'bandpath1', right(ST_BandPath(ST_MakeEmptyRaster(10, 10, 0, 0, 1, -1, 0, 0, 0)), 14); +SELECT 'bandpath2', right(ST_BandPath(rast), 14) from raster_outdb_template order by rid; diff --git a/raster/test/regress/rt_utility_expected b/raster/test/regress/rt_utility_expected index fc714cafd..771ec378c 100644 --- a/raster/test/regress/rt_utility_expected +++ b/raster/test/regress/rt_utility_expected @@ -56,3 +56,9 @@ ms2|280 ms2|280 ms2|8240 ms2|8240 +NOTICE: Could not find raster band of index 1 when getting band path. Returning NULL +bandpath1| +bandpath2|testraster.tif +bandpath2|testraster.tif +bandpath2| +bandpath2|testraster.tif