🌐 AI搜索 & 代理 主页
Skip to content

Commit f79e239

Browse files
committed
Fix multibyte issue in ltree_strncasecmp().
Previously, the API for ltree_strncasecmp() took two inputs but only one length (that of the smaller input). It truncated the larger input to that length, but that could break a multibyte sequence. Change the API to be a check for prefix equality (possibly case-insensitive) instead, which is all that's needed by the callers. Also, provide the lengths of both inputs. Reviewed-by: Chao Li <li.evan.chao@gmail.com> Reviewed-by: Peter Eisentraut <peter@eisentraut.org> Discussion: https://postgr.es/m/5f65b85740197ba6249ea507cddf609f84a6188b.camel%40j-davis.com Backpatch-through: 14
1 parent 06b030e commit f79e239

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

contrib/ltree/lquery_op.c

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ getlexeme(char *start, char *end, int *len)
4141
}
4242

4343
bool
44-
compare_subnode(ltree_level *t, char *qn, int len, int (*cmpptr) (const char *, const char *, size_t), bool anyend)
44+
compare_subnode(ltree_level *t, char *qn, int len,
45+
ltree_prefix_eq_func prefix_eq, bool anyend)
4546
{
4647
char *endt = t->name + t->len;
4748
char *endq = qn + len;
@@ -57,7 +58,7 @@ compare_subnode(ltree_level *t, char *qn, int len, int (*cmpptr) (const char *,
5758
while ((tn = getlexeme(tn, endt, &lent)) != NULL)
5859
{
5960
if ((lent == lenq || (lent > lenq && anyend)) &&
60-
(*cmpptr) (qn, tn, lenq) == 0)
61+
(*prefix_eq) (qn, lenq, tn, lent))
6162
{
6263

6364
isok = true;
@@ -74,14 +75,29 @@ compare_subnode(ltree_level *t, char *qn, int len, int (*cmpptr) (const char *,
7475
return true;
7576
}
7677

77-
int
78-
ltree_strncasecmp(const char *a, const char *b, size_t s)
78+
/*
79+
* Check if 'a' is a prefix of 'b'.
80+
*/
81+
bool
82+
ltree_prefix_eq(const char *a, size_t a_sz, const char *b, size_t b_sz)
83+
{
84+
if (a_sz > b_sz)
85+
return false;
86+
else
87+
return (strncmp(a, b, a_sz) == 0);
88+
}
89+
90+
/*
91+
* Case-insensitive check if 'a' is a prefix of 'b'.
92+
*/
93+
bool
94+
ltree_prefix_eq_ci(const char *a, size_t a_sz, const char *b, size_t b_sz)
7995
{
80-
char *al = str_tolower(a, s, DEFAULT_COLLATION_OID);
81-
char *bl = str_tolower(b, s, DEFAULT_COLLATION_OID);
82-
int res;
96+
char *al = str_tolower(a, a_sz, DEFAULT_COLLATION_OID);
97+
char *bl = str_tolower(b, b_sz, DEFAULT_COLLATION_OID);
98+
bool res;
8399

84-
res = strncmp(al, bl, s);
100+
res = (strncmp(al, bl, a_sz) == 0);
85101

86102
pfree(al);
87103
pfree(bl);
@@ -109,19 +125,19 @@ checkLevel(lquery_level *curq, ltree_level *curt)
109125

110126
for (int i = 0; i < curq->numvar; i++)
111127
{
112-
int (*cmpptr) (const char *, const char *, size_t);
128+
ltree_prefix_eq_func prefix_eq;
113129

114-
cmpptr = (curvar->flag & LVAR_INCASE) ? ltree_strncasecmp : strncmp;
130+
prefix_eq = (curvar->flag & LVAR_INCASE) ? ltree_prefix_eq_ci : ltree_prefix_eq;
115131

116132
if (curvar->flag & LVAR_SUBLEXEME)
117133
{
118-
if (compare_subnode(curt, curvar->name, curvar->len, cmpptr,
134+
if (compare_subnode(curt, curvar->name, curvar->len, prefix_eq,
119135
(curvar->flag & LVAR_ANYEND)))
120136
return success;
121137
}
122138
else if ((curvar->len == curt->len ||
123139
(curt->len > curvar->len && (curvar->flag & LVAR_ANYEND))) &&
124-
(*cmpptr) (curvar->name, curt->name, curvar->len) == 0)
140+
(*prefix_eq) (curvar->name, curvar->len, curt->name, curt->len))
125141
return success;
126142

127143
curvar = LVAR_NEXT(curvar);

contrib/ltree/ltree.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ typedef struct
157157
char data[FLEXIBLE_ARRAY_MEMBER];
158158
} ltxtquery;
159159

160+
typedef bool (*ltree_prefix_eq_func) (const char *, size_t, const char *, size_t);
161+
160162
#define HDRSIZEQT MAXALIGN(VARHDRSZ + sizeof(int32))
161163
#define COMPUTESIZE(size,lenofoperand) ( HDRSIZEQT + (size) * sizeof(ITEM) + (lenofoperand) )
162164
#define LTXTQUERY_TOO_BIG(size,lenofoperand) \
@@ -208,9 +210,10 @@ bool ltree_execute(ITEM *curitem, void *checkval,
208210
int ltree_compare(const ltree *a, const ltree *b);
209211
bool inner_isparent(const ltree *c, const ltree *p);
210212
bool compare_subnode(ltree_level *t, char *qn, int len,
211-
int (*cmpptr) (const char *, const char *, size_t), bool anyend);
213+
ltree_prefix_eq_func prefix_eq, bool anyend);
212214
ltree *lca_inner(ltree **a, int len);
213-
int ltree_strncasecmp(const char *a, const char *b, size_t s);
215+
bool ltree_prefix_eq(const char *a, size_t a_sz, const char *b, size_t b_sz);
216+
bool ltree_prefix_eq_ci(const char *a, size_t a_sz, const char *b, size_t b_sz);
214217

215218
/* fmgr macros for ltree objects */
216219
#define DatumGetLtreeP(X) ((ltree *) PG_DETOAST_DATUM(X))

contrib/ltree/ltxtquery_op.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,19 @@ checkcondition_str(void *checkval, ITEM *val)
5858
ltree_level *level = LTREE_FIRST(((CHKVAL *) checkval)->node);
5959
int tlen = ((CHKVAL *) checkval)->node->numlevel;
6060
char *op = ((CHKVAL *) checkval)->operand + val->distance;
61-
int (*cmpptr) (const char *, const char *, size_t);
61+
ltree_prefix_eq_func prefix_eq;
6262

63-
cmpptr = (val->flag & LVAR_INCASE) ? ltree_strncasecmp : strncmp;
63+
prefix_eq = (val->flag & LVAR_INCASE) ? ltree_prefix_eq_ci : ltree_prefix_eq;
6464
while (tlen > 0)
6565
{
6666
if (val->flag & LVAR_SUBLEXEME)
6767
{
68-
if (compare_subnode(level, op, val->length, cmpptr, (val->flag & LVAR_ANYEND)))
68+
if (compare_subnode(level, op, val->length, prefix_eq, (val->flag & LVAR_ANYEND)))
6969
return true;
7070
}
7171
else if ((val->length == level->len ||
7272
(level->len > val->length && (val->flag & LVAR_ANYEND))) &&
73-
(*cmpptr) (op, level->name, val->length) == 0)
73+
(*prefix_eq) (op, val->length, level->name, level->len))
7474
return true;
7575

7676
tlen--;

0 commit comments

Comments
 (0)