Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a43b4ab

Browse files
committed
Fix enforcement of restrictions inside regexp lookaround constraints.
Lookahead and lookbehind constraints aren't allowed to contain backrefs, and parentheses within them are always considered non-capturing. Or so says the manual. But the regexp parser forgot about these rules once inside a parenthesized subexpression, so that constructs like (\w)(?=(\1)) were accepted (but then not correctly executed --- a case like this acted like (\w)(?=\w), without any enforcement that the two \w's match the same text). And in (?=((foo))) the innermost parentheses would be counted as capturing parentheses, though no text would ever be captured for them. To fix, properly pass down the "type" argument to the recursive invocation of parse(). Back-patch to all supported branches; it was agreed that silent misexecution of such patterns is worse than throwing an error, even though new errors in minor releases are generally not desirable.
1 parent 8d7396e commit a43b4ab

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

src/backend/regex/regcomp.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ parseqatom(struct vars * v,
951951
EMPTYARC(lp, s);
952952
EMPTYARC(s2, rp);
953953
NOERR();
954-
atom = parse(v, ')', PLAIN, s, s2);
954+
atom = parse(v, ')', type, s, s2);
955955
assert(SEE(')') || ISERR());
956956
NEXT();
957957
NOERR();

src/test/regress/expected/regex.out

+5
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,8 @@ select 'a' ~ '()+\1';
490490
t
491491
(1 row)
492492

493+
-- Error conditions
494+
select 'xyz' ~ 'x(\w)(?=\1)'; -- no backrefs in LACONs
495+
ERROR: invalid regular expression: invalid backreference number
496+
select 'xyz' ~ 'x(\w)(?=(\1))';
497+
ERROR: invalid regular expression: invalid backreference number

src/test/regress/sql/regex.sql

+4
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,7 @@ select 'a' ~ '$()|^\1';
117117
select 'a' ~ '.. ()|\1';
118118
select 'a' ~ '()*\1';
119119
select 'a' ~ '()+\1';
120+
121+
-- Error conditions
122+
select 'xyz' ~ 'x(\w)(?=\1)'; -- no backrefs in LACONs
123+
select 'xyz' ~ 'x(\w)(?=(\1))';

0 commit comments

Comments
 (0)