]> granicus.if.org Git - postgis/commitdiff
fix premature memory free in RASTER_getBandPath (ST_BandPath)
authorBborie Park <bkpark at ucdavis.edu>
Sat, 21 Jun 2014 17:30:13 +0000 (17:30 +0000)
committerBborie Park <bkpark at ucdavis.edu>
Sat, 21 Jun 2014 17:30:13 +0000 (17:30 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@12631 b70326c6-7e19-0410-871a-916f4a2858ee

NEWS
raster/rt_pg/rtpg_band_properties.c
raster/test/regress/rt_utility.sql
raster/test/regress/rt_utility_expected

diff --git a/NEWS b/NEWS
index 15732178ae5492b743a87c2223e3f89f36cd8da5..b0c62dd5ad372c98ab75afe235ee9dff98bb8a3f 100644 (file)
--- 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 *
index ea5d25355cdbda12fa42ce1a16d4102a42760e38..7808c1038875a780e4df38dfb0ea2098ebb43f64 100644 (file)
@@ -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);
 }
 
 /**
index 61b1a2dc82baf7d7fc6991a290292fd556f7e6b8..d8dee3da5150bb56f7ed9c9c49f725518f4460ec 100644 (file)
@@ -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;
index fc714cafd5d3d0af20de078111ad362f8239ea11..771ec378cd2f38bf148cca4ef7a60c703ecd404e 100644 (file)
@@ -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