pgsql: Fix conversion of SIMILAR TO regexes for character classes

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix conversion of SIMILAR TO regexes for character classes
Date: 2025-05-28 00:00:03
Message-ID: E1uK4DB-000LnI-31@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix conversion of SIMILAR TO regexes for character classes

The code that translates SIMILAR TO pattern matching expressions to
POSIX-style regular expressions did not consider that square brackets
can be nested. For example, in an expression like [[:alpha:]%_], the
logic replaced the placeholders '_' and '%' but it should not.

This commit fixes the conversion logic by tracking the nesting level of
square brackets marking character class areas, while considering that
in expressions like []] or [^]] the first closing square bracket is a
regular character. Multiple tests are added to show how the conversions
should or should not apply applied while in a character class area, with
specific cases added for all the characters converted outside character
classes like an opening parenthesis '(', dollar sign '$', etc.

Author: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Reviewed-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Reviewed-by: Michael Paquier <michael(at)paquier(dot)xyz>
Discussion: https://postgr.es/m/16ab039d1af455652bdf4173402ddda145f2c73b.camel@cybertec.at
Backpatch-through: 13

Branch
------
REL_14_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/1fe15d25e65c5ef4fe3be9efd2927f3e3891f7b1

Modified Files
--------------
src/backend/utils/adt/regexp.c | 38 +++++++++++++++++----
src/test/regress/expected/strings.out | 62 +++++++++++++++++++++++++++++++++++
src/test/regress/sql/strings.sql | 20 +++++++++++
3 files changed, 114 insertions(+), 6 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2025-05-28 00:44:19 pgsql: Adjust regex for test with opening parenthesis in character clas
Previous Message Jacob Champion 2025-05-27 22:24:37 Re: [EXT] Re: GSS Auth issue when user member of lots of AD groups