[PATCH] Avoid potential NULL dereference in LIKE/ILIKE with C locale

From: Eugeny Goryachev <gorcom2012(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: [PATCH] Avoid potential NULL dereference in LIKE/ILIKE with C locale
Date: 2026-01-23 09:41:03
Message-ID: CABg3sZo30PKF-AYZ_eih=5snxqp73bVOGX7O_hBMqoFhcOWbjQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

While reviewing the MatchText function in backend/utils/adt/like_match.c, I
noticed a potential NULL pointer dereference when using LIKE or ILIKE with
the C locale.

The issue arises because the locale argument (of type pg_locale_t, which is
a pointer) can be NULL when the C collation is in use. However, the GETCHAR
macro unconditionally passes this locale to MATCH_LOWER, which - depending
on its definition - may attempt to dereference it (e.g., to access
locale->provider or other fields).

This can lead to a crash in builds or configurations where MATCH_LOWER is
not safe to call with a NULL locale.

The proposed patch adds an explicit check for locale == NULL in the GETCHAR
macro and falls back to pg_ascii_tolower() in that case, which is both safe
and correct for the C locale (since no locale-specific case folding is
needed).

The change aligns with existing patterns in the codebase (e.g., in text_cmp
and other collation-aware functions) where NULL locale is treated as
equivalent to C/POSIX behavior.

Best regards, Eugeny Goryachev.

Patch:
Subject: [PATCH] Avoid potential NULL dereference in LIKE/ILIKE with C
locale

---
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 892f8a745ea..884edc7ff42 100644
--- a/src/backend/utils/adt/like_match.c
+++ b/src/backend/utils/adt/like_match.c
@@ -71,7 +71,8 @@
*/

#ifdef MATCH_LOWER
-#define GETCHAR(t, locale) MATCH_LOWER(t, locale)
+#define GETCHAR(t, locale) \
+ ((locale) == 0 ? pg_ascii_tolower((unsigned char)(t)) : MATCH_LOWER(t,
locale))
#else
#define GETCHAR(t, locale) (t)
#endif
--
2.42.4

Browse pgsql-hackers by date

  From Date Subject
Next Message Jim Jones 2026-01-23 09:57:48 Re: ALTER TABLE: warn when actions do not recurse to partitions
Previous Message Amit Kapila 2026-01-23 09:06:25 Re: Newly created replication slot may be invalidated by checkpoint