]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Fix mishandling of leading '\' in nondeterministic LIKE.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 6 Jul 2026 18:47:58 +0000 (14:47 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 6 Jul 2026 18:47:58 +0000 (14:47 -0400)
commit42b7ff3aaefa5f63b4890679a283f83f1a4acb00
treee16226f18bcdd51d06527eabcb40cda39202b769
parentd6ffcae32a10bd9b53fcfe7be507ba00c6083acc
Fix mishandling of leading '\' in nondeterministic LIKE.

The loop in MatchText() processed a leading '\' without regard to
nondeterministic locales, which is problematic if what the '\'
precedes is an ordinary character that should be subject to
nondeterministic matching.  We'd insist on a literal match for it,
which is not right and is not like what happens with a '\' that
follows some ordinary characters.  Worse, we'd then advance the text
and pattern pointers by one byte, so that if the escaped character
is multibyte the next loop iteration would take the nondeterministic
code path starting at a point within the character.  That could very
possibly cause pg_strncoll() to misbehave.

The fix is quite simple: move the stanza that handles '\' down past
the one that handles nondeterminism.  The stanzas for '%' and '_'
are fine where they are, but the '\' stanza is only correct for
deterministic matching.  The logic for nondeterministic cases is
already prepared to do the right things with a '\'.

While here, I replaced tests of "locale && !locale->deterministic"
with a boolean local variable, reasoning that those are in the hot
loop paths so saving a branch and indirect fetch is worth the
trouble.  I also improved a number of related comments.

Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/391592.1783187986@sss.pgh.pa.us
Backpatch-through: 18
src/backend/utils/adt/like_match.c
src/test/regress/expected/collate.icu.utf8.out
src/test/regress/sql/collate.icu.utf8.sql