Skip to content

Commit f69c01f

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 788e35a commit f69c01f

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
@@ -944,7 +944,7 @@ parseqatom(struct vars * v,
944944
EMPTYARC(lp, s);
945945
EMPTYARC(s2, rp);
946946
NOERR();
947-
atom = parse(v, ')', PLAIN, s, s2);
947+
atom = parse(v, ')', type, s, s2);
948948
assert(SEE(')') || ISERR());
949949
NEXT();
950950
NOERR();

src/test/regress/expected/regex.out

+5
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,8 @@ select 'a' ~ '()+\1';
321321
t
322322
(1 row)
323323

324+
-- Error conditions
325+
select 'xyz' ~ 'x(\w)(?=\1)'; -- no backrefs in LACONs
326+
ERROR: invalid regular expression: invalid backreference number
327+
select 'xyz' ~ 'x(\w)(?=(\1))';
328+
ERROR: invalid regular expression: invalid backreference number

src/test/regress/sql/regex.sql

+4
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,7 @@ select 'a' ~ '$()|^\1';
8282
select 'a' ~ '.. ()|\1';
8383
select 'a' ~ '()*\1';
8484
select 'a' ~ '()+\1';
85+
86+
-- Error conditions
87+
select 'xyz' ~ 'x(\w)(?=\1)'; -- no backrefs in LACONs
88+
select 'xyz' ~ 'x(\w)(?=(\1))';

0 commit comments

Comments
 (0)