]> granicus.if.org Git - postgis/commitdiff
Correctly free raster memory when handling NULL rasters.
authorBborie Park <bkpark at ucdavis.edu>
Mon, 5 Dec 2011 02:06:37 +0000 (02:06 +0000)
committerBborie Park <bkpark at ucdavis.edu>
Mon, 5 Dec 2011 02:06:37 +0000 (02:06 +0000)
Associated ticket is #1349

git-svn-id: http://svn.osgeo.org/postgis/trunk@8303 b70326c6-7e19-0410-871a-916f4a2858ee

raster/rt_pg/rt_pg.c

index 869d69c271ed20b74200ec513a0dd7483b280ac8..6117c002377872fd42651bb255edb5f2b2ed7dd6 100644 (file)
@@ -2294,71 +2294,72 @@ Datum RASTER_addband(PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1(RASTER_copyband);
 Datum RASTER_copyband(PG_FUNCTION_ARGS)
 {
-    rt_pgraster *pgraster = NULL;
-    rt_raster torast = NULL;
-    rt_raster fromrast = NULL;
-    int toindex = 0;
-    int fromband = 0;
-    int oldtorastnumbands = 0;
-    int newtorastnumbands = 0;
-    int newbandindex = 0;
-
-    /* Deserialize torast */
-               if (PG_ARGISNULL(0)) PG_RETURN_NULL();
-    pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-
-    torast = rt_raster_deserialize(pgraster, FALSE);
-    if ( ! torast ) {
-        elog(ERROR, "RASTER_copyband: Could not deserialize first raster");
-        PG_RETURN_NULL();
-    }
+       rt_pgraster *pgraster = NULL;
+       rt_raster torast = NULL;
+       rt_raster fromrast = NULL;
+       int toindex = 0;
+       int fromband = 0;
+       int oldtorastnumbands = 0;
+       int newtorastnumbands = 0;
+       int newbandindex = 0;
+
+       /* Deserialize torast */
+       if (PG_ARGISNULL(0)) PG_RETURN_NULL();
+       pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
 
-    /* Deserialize fromrast */
-    if (!PG_ARGISNULL(1)) {
-        pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+       torast = rt_raster_deserialize(pgraster, FALSE);
+       if (!torast) {
+               elog(ERROR, "RASTER_copyband: Could not deserialize first raster");
+               PG_RETURN_NULL();
+       }
 
-        fromrast = rt_raster_deserialize(pgraster, FALSE);
-        if ( ! fromrast ) {
-            elog(ERROR, "RASTER_copyband: Could not deserialize second raster");
-            PG_RETURN_NULL();
-        }
+       /* Deserialize fromrast */
+       if (!PG_ARGISNULL(1)) {
+               pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
 
-        oldtorastnumbands = rt_raster_get_num_bands(torast);
+               fromrast = rt_raster_deserialize(pgraster, FALSE);
+               if (!fromrast) {
+                       elog(ERROR, "RASTER_copyband: Could not deserialize second raster");
+                       PG_RETURN_NULL();
+               }
 
-        if (PG_ARGISNULL(2))
-            fromband = 1;
-        else
-            fromband = PG_GETARG_INT32(2);
+               oldtorastnumbands = rt_raster_get_num_bands(torast);
 
-        if (PG_ARGISNULL(3))
-            toindex = oldtorastnumbands + 1;
-        else
-            toindex = PG_GETARG_INT32(3);
+               if (PG_ARGISNULL(2))
+                       fromband = 1;
+               else
+                       fromband = PG_GETARG_INT32(2);
 
-        /* Copy band fromrast torast */
-        newbandindex = rt_raster_copy_band(torast, fromrast, fromband - 1,
-            toindex - 1);
+               if (PG_ARGISNULL(3))
+                       toindex = oldtorastnumbands + 1;
+               else
+                       toindex = PG_GETARG_INT32(3);
 
-        newtorastnumbands = rt_raster_get_num_bands(torast);
-        if (newtorastnumbands == oldtorastnumbands || newbandindex == -1) {
-            elog(NOTICE, "RASTER_copyband: Could not add band to raster. "
-                    "Returning original raster.");
-        }
-    }
+               /* Copy band fromrast torast */
+               newbandindex = rt_raster_copy_band(
+                       torast, fromrast,
+                       fromband - 1, toindex - 1
+               );
 
-    /* Serialize and return torast */
-    pgraster = rt_raster_serialize(torast);
-    if (!pgraster) PG_RETURN_NULL();
+               newtorastnumbands = rt_raster_get_num_bands(torast);
+               if (newtorastnumbands == oldtorastnumbands || newbandindex == -1) {
+                       elog(NOTICE, "RASTER_copyband: Could not add band to raster. "
+                               "Returning original raster."
+                       );
+               }
 
-    SET_VARSIZE(pgraster, pgraster->size);
+               rt_raster_destroy(fromrast);
+       }
 
-    rt_raster_destroy(fromrast);
-    rt_raster_destroy(torast);
+       /* Serialize and return torast */
+       pgraster = rt_raster_serialize(torast);
+       rt_raster_destroy(torast);
+       if (!pgraster) PG_RETURN_NULL();
 
-    PG_RETURN_POINTER(pgraster);
+       SET_VARSIZE(pgraster, pgraster->size);
+       PG_RETURN_POINTER(pgraster);
 }
 
-
 /**
  * Check if raster is empty or not
  */