Skip to content

Commit e3ffc3e

Browse files
committed
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.albe@cybertec.at> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Michael Paquier <michael@paquier.xyz> Discussion: https://postgr.es/m/16ab039d1af455652bdf4173402ddda145f2c73b.camel@cybertec.at Backpatch-through: 13
1 parent 6ea812f commit e3ffc3e

File tree

3 files changed

+114
-6
lines changed

3 files changed

+114
-6
lines changed

src/backend/utils/adt/regexp.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -773,8 +773,11 @@ similar_escape_internal(text *pat_text, text *esc_text)
773773
int plen,
774774
elen;
775775
bool afterescape = false;
776-
bool incharclass = false;
777776
int nquotes = 0;
777+
int charclass_depth = 0; /* Nesting level of character classes,
778+
* encompassed by square brackets */
779+
int charclass_start = 0; /* State of the character class start,
780+
* for carets */
778781

779782
p = VARDATA_ANY(pat_text);
780783
plen = VARSIZE_ANY_EXHDR(pat_text);
@@ -904,7 +907,7 @@ similar_escape_internal(text *pat_text, text *esc_text)
904907
/* fast path */
905908
if (afterescape)
906909
{
907-
if (pchar == '"' && !incharclass) /* escape-double-quote? */
910+
if (pchar == '"' && charclass_depth < 1) /* escape-double-quote? */
908911
{
909912
/* emit appropriate part separator, per notes above */
910913
if (nquotes == 0)
@@ -953,18 +956,41 @@ similar_escape_internal(text *pat_text, text *esc_text)
953956
/* SQL escape character; do not send to output */
954957
afterescape = true;
955958
}
956-
else if (incharclass)
959+
else if (charclass_depth > 0)
957960
{
958961
if (pchar == '\\')
959962
*r++ = '\\';
960963
*r++ = pchar;
961-
if (pchar == ']')
962-
incharclass = false;
964+
965+
/*
966+
* Ignore a closing bracket at the start of a character class.
967+
* Such a bracket is taken literally rather than closing the
968+
* class. "charclass_start" is 1 right at the beginning of a
969+
* class and 2 after an initial caret.
970+
*/
971+
if (pchar == ']' && charclass_start > 2)
972+
charclass_depth--;
973+
else if (pchar == '[')
974+
charclass_depth++;
975+
976+
/*
977+
* If there is a caret right after the opening bracket, it negates
978+
* the character class, but a following closing bracket should
979+
* still be treated as a normal character. That holds only for
980+
* the first caret, so only the values 1 and 2 mean that closing
981+
* brackets should be taken literally.
982+
*/
983+
if (pchar == '^')
984+
charclass_start++;
985+
else
986+
charclass_start = 3; /* definitely past the start */
963987
}
964988
else if (pchar == '[')
965989
{
990+
/* start of a character class */
966991
*r++ = pchar;
967-
incharclass = true;
992+
charclass_depth++;
993+
charclass_start = 1;
968994
}
969995
else if (pchar == '%')
970996
{

src/test/regress/expected/strings.out

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,68 @@ SELECT 'abcdefg' SIMILAR TO '_bcd%' ESCAPE NULL AS null;
596596
SELECT 'abcdefg' SIMILAR TO '_bcd#%' ESCAPE '##' AS error;
597597
ERROR: invalid escape string
598598
HINT: Escape string must be empty or one character.
599+
-- Characters that should be left alone in character classes when a
600+
-- SIMILAR TO regexp pattern is converted to POSIX style.
601+
-- Underscore "_"
602+
EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO '_[_[:alpha:]_]_';
603+
QUERY PLAN
604+
------------------------------------------------
605+
Seq Scan on text_tbl
606+
Filter: (f1 ~ '^(?:.[_[:alpha:]_].)$'::text)
607+
(2 rows)
608+
609+
-- Percentage "%"
610+
EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO '%[%[:alnum:]%]%';
611+
QUERY PLAN
612+
--------------------------------------------------
613+
Seq Scan on text_tbl
614+
Filter: (f1 ~ '^(?:.*[%[:alnum:]%].*)$'::text)
615+
(2 rows)
616+
617+
-- Dot "."
618+
EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO '.[.[:alnum:].].';
619+
QUERY PLAN
620+
--------------------------------------------------
621+
Seq Scan on text_tbl
622+
Filter: (f1 ~ '^(?:\.[.[:alnum:].]\.)$'::text)
623+
(2 rows)
624+
625+
-- Dollar "$"
626+
EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO '$[$[:alnum:]$]$';
627+
QUERY PLAN
628+
--------------------------------------------------
629+
Seq Scan on text_tbl
630+
Filter: (f1 ~ '^(?:\$[$[:alnum:]$]\$)$'::text)
631+
(2 rows)
632+
633+
-- Opening parenthesis "("
634+
EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO '([([:alnum:](](';
635+
ERROR: invalid regular expression: parentheses () not balanced
636+
-- Caret "^"
637+
EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO '^[^[:alnum:]^[^^][[^^]][\^][[\^]]\^]^';
638+
QUERY PLAN
639+
------------------------------------------------------------------------
640+
Seq Scan on text_tbl
641+
Filter: (f1 ~ '^(?:\^[^[:alnum:]^[^^][[^^]][\^][[\^]]\^]\^)$'::text)
642+
(2 rows)
643+
644+
-- Closing square bracket "]" at the beginning of character class
645+
EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO '[]%][^]%][^%]%';
646+
QUERY PLAN
647+
------------------------------------------------
648+
Seq Scan on text_tbl
649+
Filter: (f1 ~ '^(?:[]%][^]%][^%].*)$'::text)
650+
(2 rows)
651+
652+
-- Closing square bracket effective after two carets at the beginning
653+
-- of character class.
654+
EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO '[^^]^';
655+
QUERY PLAN
656+
---------------------------------------
657+
Seq Scan on text_tbl
658+
Filter: (f1 ~ '^(?:[^^]\^)$'::text)
659+
(2 rows)
660+
599661
-- Test backslash escapes in regexp_replace's replacement string
600662
SELECT regexp_replace('1112223333', E'(\\d{3})(\\d{3})(\\d{4})', E'(\\1) \\2-\\3');
601663
regexp_replace

src/test/regress/sql/strings.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,26 @@ SELECT 'abcd\efg' SIMILAR TO '_bcd\%' ESCAPE '' AS true;
193193
SELECT 'abcdefg' SIMILAR TO '_bcd%' ESCAPE NULL AS null;
194194
SELECT 'abcdefg' SIMILAR TO '_bcd#%' ESCAPE '##' AS error;
195195

196+
-- Characters that should be left alone in character classes when a
197+
-- SIMILAR TO regexp pattern is converted to POSIX style.
198+
-- Underscore "_"
199+
EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO '_[_[:alpha:]_]_';
200+
-- Percentage "%"
201+
EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO '%[%[:alnum:]%]%';
202+
-- Dot "."
203+
EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO '.[.[:alnum:].].';
204+
-- Dollar "$"
205+
EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO '$[$[:alnum:]$]$';
206+
-- Opening parenthesis "("
207+
EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO '([([:alnum:](](';
208+
-- Caret "^"
209+
EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO '^[^[:alnum:]^[^^][[^^]][\^][[\^]]\^]^';
210+
-- Closing square bracket "]" at the beginning of character class
211+
EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO '[]%][^]%][^%]%';
212+
-- Closing square bracket effective after two carets at the beginning
213+
-- of character class.
214+
EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO '[^^]^';
215+
196216
-- Test backslash escapes in regexp_replace's replacement string
197217
SELECT regexp_replace('1112223333', E'(\\d{3})(\\d{3})(\\d{4})', E'(\\1) \\2-\\3');
198218
SELECT regexp_replace('foobarrbazz', E'(.)\\1', E'X\\&Y', 'g');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy