pgsql: Fix mishandling of leading '\' in nondeterministic LIKE.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix mishandling of leading '\' in nondeterministic LIKE.
Date: 2026-07-06 18:48:15
Message-ID: E1wgoMZ-001d4Q-1S@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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(at)sss(dot)pgh(dot)pa(dot)us>
Discussion: https://postgr.es/m/391592.1783187986@sss.pgh.pa.us
Backpatch-through: 18

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/42b7ff3aaefa5f63b4890679a283f83f1a4acb00

Modified Files
--------------
src/backend/utils/adt/like_match.c | 76 +++++++++++++++-----------
src/test/regress/expected/collate.icu.utf8.out | 30 ++++++++++
src/test/regress/sql/collate.icu.utf8.sql | 6 ++
3 files changed, 80 insertions(+), 32 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Robert Haas 2026-07-06 19:46:21 pgsql: Use PG_MODULE_MAGIC_EXT in newly introduced modules
Previous Message Tom Lane 2026-07-06 17:48:47 pgsql: Make PLy_elog() use pg_integer_constant_p().