Fix bug corrupting query in gist consistent function.

Thank to Mario Weilguni <mweilguni@sime.com> to discover a bug.
This commit is contained in:
Teodor Sigaev 2006-07-11 16:06:40 +00:00
parent 6e37be02e7
commit 7284c36ae7
3 changed files with 17 additions and 1 deletions

View File

@ -569,5 +569,6 @@ _ltree_consistent(PG_FUNCTION_ARGS)
/* internal error */
elog(ERROR, "unrecognized StrategyNumber: %d", strategy);
}
PG_FREE_IF_COPY(query,1);
PG_RETURN_BOOL(res);
}

View File

@ -161,8 +161,11 @@ bool compare_subnode(ltree_level * t, char *q, int len,
ltree *lca_inner(ltree ** a, int len);
#define PG_GETARG_LTREE(x) ((ltree*)DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(x))))
#define PG_GETARG_LTREE_COPY(x) ((ltree*)DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(x))))
#define PG_GETARG_LQUERY(x) ((lquery*)DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(x))))
#define PG_GETARG_LQUERY_COPY(x) ((lquery*)DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(x))))
#define PG_GETARG_LTXTQUERY(x) ((ltxtquery*)DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(x))))
#define PG_GETARG_LTXTQUERY_COPY(x) ((ltxtquery*)DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(x))))
/* GiST support for ltree */

View File

@ -626,7 +626,7 @@ Datum
ltree_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
char *query = (char *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));
void *query = NULL;
ltree_gist *key = (ltree_gist *) DatumGetPointer(entry->key);
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
bool res = false;
@ -638,15 +638,18 @@ ltree_consistent(PG_FUNCTION_ARGS)
switch (strategy)
{
case BTLessStrategyNumber:
query = PG_GETARG_LTREE(1);
res = (GIST_LEAF(entry)) ?
(ltree_compare((ltree *) query, LTG_NODE(key)) > 0)
:
(ltree_compare((ltree *) query, LTG_GETLNODE(key)) >= 0);
break;
case BTLessEqualStrategyNumber:
query = PG_GETARG_LTREE(1);
res = (ltree_compare((ltree *) query, LTG_GETLNODE(key)) >= 0);
break;
case BTEqualStrategyNumber:
query = PG_GETARG_LTREE(1);
if (GIST_LEAF(entry))
res = (ltree_compare((ltree *) query, LTG_NODE(key)) == 0);
else
@ -657,21 +660,25 @@ ltree_consistent(PG_FUNCTION_ARGS)
);
break;
case BTGreaterEqualStrategyNumber:
query = PG_GETARG_LTREE(1);
res = (ltree_compare((ltree *) query, LTG_GETRNODE(key)) <= 0);
break;
case BTGreaterStrategyNumber:
query = PG_GETARG_LTREE(1);
res = (GIST_LEAF(entry)) ?
(ltree_compare((ltree *) query, LTG_GETRNODE(key)) < 0)
:
(ltree_compare((ltree *) query, LTG_GETRNODE(key)) <= 0);
break;
case 10:
query = PG_GETARG_LTREE_COPY(1);
res = (GIST_LEAF(entry)) ?
inner_isparent((ltree *) query, LTG_NODE(key))
:
gist_isparent(key, (ltree *) query);
break;
case 11:
query = PG_GETARG_LTREE_COPY(1);
res = (GIST_LEAF(entry)) ?
inner_isparent(LTG_NODE(key), (ltree *) query)
:
@ -679,6 +686,7 @@ ltree_consistent(PG_FUNCTION_ARGS)
break;
case 12:
case 13:
query = PG_GETARG_LQUERY(1);
if (GIST_LEAF(entry))
res = DatumGetBool(DirectFunctionCall2(ltq_regex,
PointerGetDatum(LTG_NODE(key)),
@ -689,6 +697,7 @@ ltree_consistent(PG_FUNCTION_ARGS)
break;
case 14:
case 15:
query = PG_GETARG_LQUERY(1);
if (GIST_LEAF(entry))
res = DatumGetBool(DirectFunctionCall2(ltxtq_exec,
PointerGetDatum(LTG_NODE(key)),
@ -699,6 +708,7 @@ ltree_consistent(PG_FUNCTION_ARGS)
break;
case 16:
case 17:
query = DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));
if (GIST_LEAF(entry))
res = DatumGetBool(DirectFunctionCall2(lt_q_regex,
PointerGetDatum(LTG_NODE(key)),
@ -711,5 +721,7 @@ ltree_consistent(PG_FUNCTION_ARGS)
/* internal error */
elog(ERROR, "unrecognized StrategyNumber: %d", strategy);
}
PG_FREE_IF_COPY(query,1);
PG_RETURN_BOOL(res);
}