Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3397c67

Browse files
committed
Fix broken collation-aware searches in SP-GiST text opclass.
spg_text_leaf_consistent() supposed that it should compare only Min(querylen, entrylen) bytes of the two strings, and then deal with any excess bytes in one string or the other by assuming the longer string is greater if the prefixes are equal. Quite aside from the fact that that's just wrong in some locales (e.g., 'ch' is not less than 'd' in cs_CZ), it also risked passing incomplete multibyte characters to strcoll(), with ensuing bad results. Instead, just pass the full strings to varstr_cmp, and let it decide what to do about unequal-length strings. Fortunately, this error doesn't imply any index corruption, it's just that searches might return the wrong set of entries. Per report from Emre Hasegeli, though this is not his patch. Thanks to Peter Geoghegan for review and discussion. This code was born broken, so back-patch to all supported branches. In HEAD, I failed to resist the temptation to do a bit of cosmetic cleanup/pgindent'ing on 710d90d, too. Discussion: https://postgr.es/m/CAE2gYzzb6K51VnTq5i5p52z+j9p2duEa-K1T3RrC_GQEynAKEg@mail.gmail.com
1 parent 16d3dbe commit 3397c67

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/backend/access/spgist/spgtextproc.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -613,22 +613,22 @@ spg_text_leaf_consistent(PG_FUNCTION_ARGS)
613613
/* If asserts enabled, verify encoding of reconstructed string */
614614
Assert(pg_verifymbstr(fullValue, fullLen, false));
615615

616-
r = varstr_cmp(fullValue, Min(queryLen, fullLen),
617-
VARDATA_ANY(query), Min(queryLen, fullLen),
616+
r = varstr_cmp(fullValue, fullLen,
617+
VARDATA_ANY(query), queryLen,
618618
PG_GET_COLLATION());
619619
}
620620
else
621621
{
622622
/* Non-collation-aware comparison */
623623
r = memcmp(fullValue, VARDATA_ANY(query), Min(queryLen, fullLen));
624-
}
625624

626-
if (r == 0)
627-
{
628-
if (queryLen > fullLen)
629-
r = -1;
630-
else if (queryLen < fullLen)
631-
r = 1;
625+
if (r == 0)
626+
{
627+
if (queryLen > fullLen)
628+
r = -1;
629+
else if (queryLen < fullLen)
630+
r = 1;
631+
}
632632
}
633633

634634
switch (strategy)

0 commit comments

Comments
 (0)