This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c: regpiece() Don't match 0 length more than once
authorKarl Williamson <khw@cpan.org>
Sun, 4 Oct 2020 22:46:51 +0000 (16:46 -0600)
committerKarl Williamson <khw@cpan.org>
Mon, 12 Oct 2020 15:45:47 +0000 (09:45 -0600)
There's no point in matching something more than once that doesn't
advance the parse, as long as there are no side effects beyond those
from the first match.  Those could only happen in code blocks, which
this code doesn't have.

regcomp.c

index 5d78f60..945e444 100644 (file)
--- a/regcomp.c
+++ b/regcomp.c
@@ -12714,6 +12714,9 @@ S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth)
         }
     }
 
+    /* Here we have a quantifier, and have calculated 'min' and 'max'.
+     *
+     * Check and possibly adjust a zero width operand */
     if (! (flags & (HASWIDTH|POSTPONED))) {
         if (max > REG_INFTY/3) {
             if (origparse[0] == '\\' && origparse[1] == 'K') {
@@ -12733,6 +12736,16 @@ S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth)
                            origparse));
             }
         }
+
+        /* There's no point in trying to match something 0 length more than
+         * once except for extra side effects, which we don't have here since
+         * not POSTPONED */
+        if (max > 1) {
+            max = 1;
+            if (min > max) {
+                min = max;
+            }
+        }
     }
 
     if ((flags&SIMPLE)) {