Fix some bugs introduced by the 8.2-era conversion of cube functions to V1

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.
This commit is contained in:
Tom Lane 2008-05-29 18:46:52 +00:00
parent 0e804581b6
commit c06f39de73

View 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 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 called by the backend in the process of processing queries. The calling
@ -842,10 +842,11 @@ cube_union(PG_FUNCTION_ARGS)
Datum Datum
cube_inter(PG_FUNCTION_ARGS) cube_inter(PG_FUNCTION_ARGS)
{ {
NDBOX *a = PG_GETARG_NDBOX(0);
NDBOX *b = PG_GETARG_NDBOX(1);
NDBOX *result;
bool swapped = false;
int i; int i;
NDBOX *result,
*a = PG_GETARG_NDBOX(0),
*b = PG_GETARG_NDBOX(1);
if (a->dim >= b->dim) if (a->dim >= b->dim)
{ {
@ -869,6 +870,7 @@ cube_inter(PG_FUNCTION_ARGS)
b = a; b = a;
a = tmp; a = tmp;
swapped = true;
} }
/* /*
@ -895,8 +897,17 @@ cube_inter(PG_FUNCTION_ARGS)
a->x[i + a->dim]), result->x[i + a->dim]); a->x[i + a->dim]), result->x[i + a->dim]);
} }
PG_FREE_IF_COPY(a,0); if (swapped)
PG_FREE_IF_COPY(b,1); {
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? * Is it OK to return a non-null intersection for non-overlapping boxes?
*/ */
@ -1267,14 +1278,12 @@ cube_overlap(PG_FUNCTION_ARGS)
Datum Datum
cube_distance(PG_FUNCTION_ARGS) cube_distance(PG_FUNCTION_ARGS)
{ {
int i; NDBOX *a = PG_GETARG_NDBOX(0),
*b = PG_GETARG_NDBOX(1);
bool swapped = false;
double d, double d,
distance; distance;
NDBOX *a, int i;
*b;
a = PG_GETARG_NDBOX(0);
b = PG_GETARG_NDBOX(1);
/* swap the box pointers if needed */ /* swap the box pointers if needed */
if (a->dim < b->dim) if (a->dim < b->dim)
@ -1283,6 +1292,7 @@ cube_distance(PG_FUNCTION_ARGS)
b = a; b = a;
a = tmp; a = tmp;
swapped = true;
} }
distance = 0.0; distance = 0.0;
@ -1300,8 +1310,17 @@ cube_distance(PG_FUNCTION_ARGS)
distance += d * d; distance += d * d;
} }
PG_FREE_IF_COPY(a,0); if (swapped)
PG_FREE_IF_COPY(b,1); {
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)); PG_RETURN_FLOAT8(sqrt(distance));
} }
@ -1344,14 +1363,11 @@ cube_is_point(PG_FUNCTION_ARGS)
Datum Datum
cube_dim(PG_FUNCTION_ARGS) cube_dim(PG_FUNCTION_ARGS)
{ {
NDBOX *c; NDBOX *c = PG_GETARG_NDBOX(0);
int dim; int dim = c->dim;
c = PG_GETARG_NDBOX(0);
dim = c->dim;
PG_FREE_IF_COPY(c,0); PG_FREE_IF_COPY(c,0);
PG_RETURN_INT32(c->dim); PG_RETURN_INT32(dim);
} }
/* Return a specific normalized LL coordinate */ /* Return a specific normalized LL coordinate */