Skip to content

Commit afcc877

Browse files
committed
Fix ancient bug in parsing of BRE-mode regular expressions.
brenext(), when parsing a '*' quantifier, forgot to return any "value" for the token; per the equivalent case in next(), it should return value 1 to indicate that greedy rather than non-greedy behavior is wanted. The result is that the compiled regexp could behave like 'x*?' rather than the intended 'x*', if we were unlucky enough to have a zero in v->nextvalue at this point. That seems to happen with some reliability if we have '.*' at the beginning of a BRE-mode regexp, although that depends on the initial contents of a stack-allocated struct, so it's not guaranteed to fail. Found by Alexander Lakhin using valgrind testing. This bug seems to be aboriginal in Spencer's code, so back-patch all the way. Discussion: https://postgr.es/m/16814-6c5e3edd2bdf0d50@postgresql.org
1 parent 15b824d commit afcc877

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

src/backend/regex/regc_lex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ brenext(struct vars *v,
994994
case CHR('*'):
995995
if (LASTTYPE(EMPTY) || LASTTYPE('(') || LASTTYPE('^'))
996996
RETV(PLAIN, c);
997-
RET('*');
997+
RETV('*', 1);
998998
break;
999999
case CHR('['):
10001000
if (HAVE(6) && *(v->now + 0) == CHR('[') &&

src/test/modules/test_regex/expected/test_regex.out

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,14 @@ ERROR: invalid regular expression: quantifier operand invalid
614614
-- expectError 7.15 - a*+ BADRPT
615615
select * from test_regex('a*+', '', '-');
616616
ERROR: invalid regular expression: quantifier operand invalid
617+
-- test for ancient brenext() bug; not currently in Tcl
618+
select * from test_regex('.*b', 'aaabbb', 'b');
619+
test_regex
620+
------------
621+
{0}
622+
{aaabbb}
623+
(2 rows)
624+
617625
-- doing 8 "braces"
618626
-- expectMatch 8.1 NQ "a{0,1}" "" ""
619627
select * from test_regex('a{0,1}', '', 'NQ');

src/test/modules/test_regex/sql/test_regex.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ select * from test_regex('a?*', '', '-');
214214
select * from test_regex('a+*', '', '-');
215215
-- expectError 7.15 - a*+ BADRPT
216216
select * from test_regex('a*+', '', '-');
217+
-- test for ancient brenext() bug; not currently in Tcl
218+
select * from test_regex('.*b', 'aaabbb', 'b');
217219

218220
-- doing 8 "braces"
219221

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