]> granicus.if.org Git - postgresql/commitdiff
Fix some bugs introduced by the 8.2-era conversion of cube functions to V1
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 29 May 2008 18:46:52 +0000 (18:46 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 29 May 2008 18:46:52 +0000 (18:46 +0000)
calling convention.  cube_inter and cube_distance could attempt to pfree
their input arguments, and cube_dim returned a value from a struct it
might have just pfree'd (which would only really cause a problem in a
debug build, but it's still wrong).  Per bug #4208 and additional code
reading.

In HEAD and 8.3, I also made a batch of cosmetic changes to bring these
functions into line with the preferred coding style for V1 functions,
ie declare and fetch all the arguments at the top so readers can easily
see what they are.

contrib/cube/cube.c

index cf9428887465af9d60860a09dac5c01ea403416a..2419afe0f6f7b0ddfbc00d1d6cfad0b17ac0c10d 100644 (file)
@@ -1,5 +1,5 @@
 /******************************************************************************
-  $PostgreSQL: pgsql/contrib/cube/cube.c,v 1.30.2.1 2007/03/07 21:25:18 teodor Exp $
+  $PostgreSQL: pgsql/contrib/cube/cube.c,v 1.30.2.2 2008/05/29 18:46:52 tgl Exp $
 
   This file contains routines that can be bound to a Postgres backend and
   called by the backend in the process of processing queries.  The calling
@@ -842,10 +842,11 @@ cube_union(PG_FUNCTION_ARGS)
 Datum
 cube_inter(PG_FUNCTION_ARGS)
 {
+       NDBOX      *a = PG_GETARG_NDBOX(0);
+       NDBOX      *b = PG_GETARG_NDBOX(1);
+       NDBOX      *result;
+       bool            swapped = false;
        int                     i;
-       NDBOX      *result,
-                          *a = PG_GETARG_NDBOX(0),
-                          *b = PG_GETARG_NDBOX(1);
 
        if (a->dim >= b->dim)
        {
@@ -869,6 +870,7 @@ cube_inter(PG_FUNCTION_ARGS)
 
                b = a;
                a = tmp;
+               swapped = true;
        }
 
        /*
@@ -895,8 +897,17 @@ cube_inter(PG_FUNCTION_ARGS)
                                                                   a->x[i + a->dim]), result->x[i + a->dim]);
        }
 
-       PG_FREE_IF_COPY(a,0);
-       PG_FREE_IF_COPY(b,1);
+       if (swapped)
+       {
+               PG_FREE_IF_COPY(b, 0);
+               PG_FREE_IF_COPY(a, 1);
+       }
+       else
+       {
+               PG_FREE_IF_COPY(a, 0);
+               PG_FREE_IF_COPY(b, 1);
+       }
+
        /*
         * Is it OK to return a non-null intersection for non-overlapping boxes?
         */
@@ -1267,14 +1278,12 @@ cube_overlap(PG_FUNCTION_ARGS)
 Datum
 cube_distance(PG_FUNCTION_ARGS)
 {
-       int                     i;
+       NDBOX      *a = PG_GETARG_NDBOX(0),
+                          *b = PG_GETARG_NDBOX(1);
+       bool            swapped = false;
        double          d,
                                distance;
-       NDBOX      *a,
-                          *b;
-
-       a = PG_GETARG_NDBOX(0);
-       b = PG_GETARG_NDBOX(1);
+       int                     i;
 
        /* swap the box pointers if needed */
        if (a->dim < b->dim)
@@ -1283,6 +1292,7 @@ cube_distance(PG_FUNCTION_ARGS)
 
                b = a;
                a = tmp;
+               swapped = true;
        }
 
        distance = 0.0;
@@ -1300,8 +1310,17 @@ cube_distance(PG_FUNCTION_ARGS)
                distance += d * d;
        }
 
-       PG_FREE_IF_COPY(a,0);
-       PG_FREE_IF_COPY(b,1);
+       if (swapped)
+       {
+               PG_FREE_IF_COPY(b, 0);
+               PG_FREE_IF_COPY(a, 1);
+       }
+       else
+       {
+               PG_FREE_IF_COPY(a, 0);
+               PG_FREE_IF_COPY(b, 1);
+       }
+
        PG_RETURN_FLOAT8(sqrt(distance));
 }
 
@@ -1344,14 +1363,11 @@ cube_is_point(PG_FUNCTION_ARGS)
 Datum
 cube_dim(PG_FUNCTION_ARGS)
 {
-       NDBOX      *c;
-       int                     dim;
-
-       c = PG_GETARG_NDBOX(0);
-       dim = c->dim;
+       NDBOX      *c = PG_GETARG_NDBOX(0);
+       int                     dim = c->dim;
 
        PG_FREE_IF_COPY(c,0);
-       PG_RETURN_INT32(c->dim);
+       PG_RETURN_INT32(dim);
 }
 
 /* Return a specific normalized LL coordinate */