Commit 4fe8ba63 authored by Teodor Sigaev's avatar Teodor Sigaev

Fix bug corrupting query in gist consistent function.

Thank to Mario Weilguni <mweilguni@sime.com> to discover a bug.
parent c713683f
...@@ -580,5 +580,6 @@ _ltree_consistent(PG_FUNCTION_ARGS) ...@@ -580,5 +580,6 @@ _ltree_consistent(PG_FUNCTION_ARGS)
/* internal error */ /* internal error */
elog(ERROR, "unrecognized StrategyNumber: %d", strategy); elog(ERROR, "unrecognized StrategyNumber: %d", strategy);
} }
PG_FREE_IF_COPY(query,1);
PG_RETURN_BOOL(res); PG_RETURN_BOOL(res);
} }
/* $PostgreSQL: pgsql/contrib/ltree/ltree.h,v 1.15 2006/03/11 04:38:29 momjian Exp $ */ /* $PostgreSQL: pgsql/contrib/ltree/ltree.h,v 1.16 2006/07/11 16:00:44 teodor Exp $ */
#ifndef __LTREE_H__ #ifndef __LTREE_H__
#define __LTREE_H__ #define __LTREE_H__
...@@ -163,8 +163,11 @@ bool compare_subnode(ltree_level * t, char *q, int len, ...@@ -163,8 +163,11 @@ bool compare_subnode(ltree_level * t, char *q, int len,
ltree *lca_inner(ltree ** a, 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(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(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(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 */ /* GiST support for ltree */
......
/* /*
* GiST support for ltree * GiST support for ltree
* Teodor Sigaev <teodor@stack.net> * Teodor Sigaev <teodor@stack.net>
* $PostgreSQL: pgsql/contrib/ltree/ltree_gist.c,v 1.15 2006/06/28 11:59:59 teodor Exp $ * $PostgreSQL: pgsql/contrib/ltree/ltree_gist.c,v 1.16 2006/07/11 16:00:44 teodor Exp $
*/ */
#include "ltree.h" #include "ltree.h"
...@@ -627,7 +627,7 @@ Datum ...@@ -627,7 +627,7 @@ Datum
ltree_consistent(PG_FUNCTION_ARGS) ltree_consistent(PG_FUNCTION_ARGS)
{ {
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); 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); ltree_gist *key = (ltree_gist *) DatumGetPointer(entry->key);
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
bool res = false; bool res = false;
...@@ -635,15 +635,18 @@ ltree_consistent(PG_FUNCTION_ARGS) ...@@ -635,15 +635,18 @@ ltree_consistent(PG_FUNCTION_ARGS)
switch (strategy) switch (strategy)
{ {
case BTLessStrategyNumber: case BTLessStrategyNumber:
query = PG_GETARG_LTREE(1);
res = (GIST_LEAF(entry)) ? res = (GIST_LEAF(entry)) ?
(ltree_compare((ltree *) query, LTG_NODE(key)) > 0) (ltree_compare((ltree *) query, LTG_NODE(key)) > 0)
: :
(ltree_compare((ltree *) query, LTG_GETLNODE(key)) >= 0); (ltree_compare((ltree *) query, LTG_GETLNODE(key)) >= 0);
break; break;
case BTLessEqualStrategyNumber: case BTLessEqualStrategyNumber:
query = PG_GETARG_LTREE(1);
res = (ltree_compare((ltree *) query, LTG_GETLNODE(key)) >= 0); res = (ltree_compare((ltree *) query, LTG_GETLNODE(key)) >= 0);
break; break;
case BTEqualStrategyNumber: case BTEqualStrategyNumber:
query = PG_GETARG_LTREE(1);
if (GIST_LEAF(entry)) if (GIST_LEAF(entry))
res = (ltree_compare((ltree *) query, LTG_NODE(key)) == 0); res = (ltree_compare((ltree *) query, LTG_NODE(key)) == 0);
else else
...@@ -654,21 +657,25 @@ ltree_consistent(PG_FUNCTION_ARGS) ...@@ -654,21 +657,25 @@ ltree_consistent(PG_FUNCTION_ARGS)
); );
break; break;
case BTGreaterEqualStrategyNumber: case BTGreaterEqualStrategyNumber:
query = PG_GETARG_LTREE(1);
res = (ltree_compare((ltree *) query, LTG_GETRNODE(key)) <= 0); res = (ltree_compare((ltree *) query, LTG_GETRNODE(key)) <= 0);
break; break;
case BTGreaterStrategyNumber: case BTGreaterStrategyNumber:
query = PG_GETARG_LTREE(1);
res = (GIST_LEAF(entry)) ? res = (GIST_LEAF(entry)) ?
(ltree_compare((ltree *) query, LTG_GETRNODE(key)) < 0) (ltree_compare((ltree *) query, LTG_GETRNODE(key)) < 0)
: :
(ltree_compare((ltree *) query, LTG_GETRNODE(key)) <= 0); (ltree_compare((ltree *) query, LTG_GETRNODE(key)) <= 0);
break; break;
case 10: case 10:
query = PG_GETARG_LTREE_COPY(1);
res = (GIST_LEAF(entry)) ? res = (GIST_LEAF(entry)) ?
inner_isparent((ltree *) query, LTG_NODE(key)) inner_isparent((ltree *) query, LTG_NODE(key))
: :
gist_isparent(key, (ltree *) query); gist_isparent(key, (ltree *) query);
break; break;
case 11: case 11:
query = PG_GETARG_LTREE_COPY(1);
res = (GIST_LEAF(entry)) ? res = (GIST_LEAF(entry)) ?
inner_isparent(LTG_NODE(key), (ltree *) query) inner_isparent(LTG_NODE(key), (ltree *) query)
: :
...@@ -676,6 +683,7 @@ ltree_consistent(PG_FUNCTION_ARGS) ...@@ -676,6 +683,7 @@ ltree_consistent(PG_FUNCTION_ARGS)
break; break;
case 12: case 12:
case 13: case 13:
query = PG_GETARG_LQUERY(1);
if (GIST_LEAF(entry)) if (GIST_LEAF(entry))
res = DatumGetBool(DirectFunctionCall2(ltq_regex, res = DatumGetBool(DirectFunctionCall2(ltq_regex,
PointerGetDatum(LTG_NODE(key)), PointerGetDatum(LTG_NODE(key)),
...@@ -686,6 +694,7 @@ ltree_consistent(PG_FUNCTION_ARGS) ...@@ -686,6 +694,7 @@ ltree_consistent(PG_FUNCTION_ARGS)
break; break;
case 14: case 14:
case 15: case 15:
query = PG_GETARG_LQUERY(1);
if (GIST_LEAF(entry)) if (GIST_LEAF(entry))
res = DatumGetBool(DirectFunctionCall2(ltxtq_exec, res = DatumGetBool(DirectFunctionCall2(ltxtq_exec,
PointerGetDatum(LTG_NODE(key)), PointerGetDatum(LTG_NODE(key)),
...@@ -696,6 +705,7 @@ ltree_consistent(PG_FUNCTION_ARGS) ...@@ -696,6 +705,7 @@ ltree_consistent(PG_FUNCTION_ARGS)
break; break;
case 16: case 16:
case 17: case 17:
query = DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));
if (GIST_LEAF(entry)) if (GIST_LEAF(entry))
res = DatumGetBool(DirectFunctionCall2(lt_q_regex, res = DatumGetBool(DirectFunctionCall2(lt_q_regex,
PointerGetDatum(LTG_NODE(key)), PointerGetDatum(LTG_NODE(key)),
...@@ -708,5 +718,7 @@ ltree_consistent(PG_FUNCTION_ARGS) ...@@ -708,5 +718,7 @@ ltree_consistent(PG_FUNCTION_ARGS)
/* internal error */ /* internal error */
elog(ERROR, "unrecognized StrategyNumber: %d", strategy); elog(ERROR, "unrecognized StrategyNumber: %d", strategy);
} }
PG_FREE_IF_COPY(query,1);
PG_RETURN_BOOL(res); PG_RETURN_BOOL(res);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment