On Sun, 2026-10-18 at 15:30 +0000, Greg Burd wrote:
> Hi Hackers, Jeff,
>
> In v19, ILIKE in a database with a single-byte encoding and a C ctype
> never matches when '%' is followed by a byte >= 0x80:
Thank you for the report!
I attached a patch with a different approach though, which is to cast
the result of pg_ascii_tolower() so that GETCHAR() consistently returns
a 'char'. That addresses the real problem of commit 1e493158d3d, which
is that it unintentionally changed GETCHAR() from returning 'char' to
returning 'unsigned char' for MATCH_LOWER.
It passes the same test that you submitted, but I don't plan to commit
the tests with the change. We are missing coverage for non-UTF8
encodings, but if we're going to add tests for those, it should
probably be a whole suite rather than a one-off.
I plan to commit it very soon and backport to 19.
Regards,
Jeff Davis
From 51352fc1ec7165fe929f3670fc11de1125129a79 Mon Sep 17 00:00:00 2001
From: Jeff Davis <[email protected]>
Date: Tue, 20 Oct 2026 06:17:21 -0700
Subject: [PATCH v2j] Fix ILIKE '%' followed by a non-ASCII byte in single-byte
databases.
Commit 1e493158d3d changed the MATCH_LOWER implementation of GETCHAR()
from SB_lower_char() to use pg_ascii_tolower(). The former returned a
char, whereas the latter returns an unsigned char; but the local
variable in C_IMatchText was a char.
For single-byte encodings with ctype=C, that could lead to a broken
signed/unsigned comparison, such that the pattern U&'%\00C6' would
never match.
Fix by casting the result of pg_ascii_tolower() explicitly to char.
Reported-by: Greg Burd <[email protected]>
Discussion: https://postgr.es/m/_GW2V-rd0QW4BTguAJriA9XVmjOn2mR3-YeAZr9ZU6EtlLvYPEm_r_fV5wGKzSNMzboj_nqn3fk72AYAvOKzEnPlCCHteaYrgESrIhPg6l8=@burd.me
Backpatch-through: 19
---
src/backend/utils/adt/like_match.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/backend/utils/adt/like_match.c b/src/backend/utils/adt/like_match.c
index defcaa96fb5..0249eb49a7d 100644
--- a/src/backend/utils/adt/like_match.c
+++ b/src/backend/utils/adt/like_match.c
@@ -75,7 +75,8 @@
* locales must casefold the inputs before matching.
*/
#ifdef MATCH_LOWER
-#define GETCHAR(t) pg_ascii_tolower(t)
+/* cast to char to match data types in caller */
+#define GETCHAR(t) ((char) pg_ascii_tolower(t))
#else
#define GETCHAR(t) (t)
#endif
--
2.53.0