b8876f
From fbb9dc823a06b4815ee8fd8632fc475b8034e379 Mon Sep 17 00:00:00 2001
b8876f
From: Yves Orton <demerphq@gmail.com>
b8876f
Date: Fri, 27 Jan 2017 10:18:51 +0100
b8876f
Subject: [PATCH] fix RT #130561 - recursion and optimising away impossible
b8876f
 quantifiers are not friends
b8876f
MIME-Version: 1.0
b8876f
Content-Type: text/plain; charset=UTF-8
b8876f
Content-Transfer-Encoding: 8bit
b8876f
b8876f
Ported to 5.24.1:
b8876f
b8876f
commit 31fc93954d1f379c7a49889d91436ce99818e1f6
b8876f
Author: Yves Orton <demerphq@gmail.com>
b8876f
Date:   Fri Jan 27 10:18:51 2017 +0100
b8876f
b8876f
    fix RT #130561 - recursion and optimising away impossible quantifiers are not friends
b8876f
b8876f
    Instead of optimising away impossible quantifiers like (foo){1,0} treat them
b8876f
    as unquantified, and guard them with an OPFAIL. Thus /(foo){1,0}/ is treated
b8876f
    the same as /(*FAIL)(foo)/ this is important in patterns like /(foo){1,0}|(?1)/
b8876f
    where the (?1) needs to be able to recurse into the (foo) even though the
b8876f
    (foo){1,0} can never match. It also resolves various issues (SEGVs) with patterns
b8876f
    like /((?1)){1,0}/.
b8876f
b8876f
    This patch would have been easier if S_reginsert() documented that it is
b8876f
    the callers responsibility to properly set up the NEXT_OFF() of the inserted
b8876f
    node (if the node has a NEXT_OFF())
b8876f
b8876f
Signed-off-by: Petr Písař <ppisar@redhat.com>
b8876f
---
b8876f
 regcomp.c            | 14 +++-----------
b8876f
 t/re/pat_rt_report.t | 11 ++++++++++-
b8876f
 2 files changed, 13 insertions(+), 12 deletions(-)
b8876f
b8876f
diff --git a/regcomp.c b/regcomp.c
b8876f
index bcb8db5..9f343d3 100644
b8876f
--- a/regcomp.c
b8876f
+++ b/regcomp.c
b8876f
@@ -11497,19 +11497,11 @@ S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth)
b8876f
 	    nextchar(pRExC_state);
b8876f
             if (max < min) {    /* If can't match, warn and optimize to fail
b8876f
                                    unconditionally */
b8876f
-                if (SIZE_ONLY) {
b8876f
-
b8876f
-                    /* We can't back off the size because we have to reserve
b8876f
-                     * enough space for all the things we are about to throw
b8876f
-                     * away, but we can shrink it by the amount we are about
b8876f
-                     * to re-use here */
b8876f
-                    RExC_size += PREVOPER(RExC_size) - regarglen[(U8)OPFAIL];
b8876f
-                }
b8876f
-                else {
b8876f
+                if (PASS2) {
b8876f
                     ckWARNreg(RExC_parse, "Quantifier {n,m} with n > m can't match");
b8876f
-                    RExC_emit = orig_emit;
b8876f
                 }
b8876f
-                ret = reganode(pRExC_state, OPFAIL, 0);
b8876f
+                reginsert(pRExC_state, OPFAIL, orig_emit, depth+1);
b8876f
+                NEXT_OFF(orig_emit)= regarglen[OPFAIL] + NODE_STEP_REGNODE;
b8876f
                 return ret;
b8876f
             }
b8876f
             else if (min == max && *RExC_parse == '?')
b8876f
diff --git a/t/re/pat_rt_report.t b/t/re/pat_rt_report.t
b8876f
index cb02ad2..2c1dbc4 100644
b8876f
--- a/t/re/pat_rt_report.t
b8876f
+++ b/t/re/pat_rt_report.t
b8876f
@@ -20,7 +20,7 @@ use warnings;
b8876f
 use 5.010;
b8876f
 use Config;
b8876f
 
b8876f
-plan tests => 2500;  # Update this when adding/deleting tests.
b8876f
+plan tests => 2502;  # Update this when adding/deleting tests.
b8876f
 
b8876f
 run_tests() unless caller;
b8876f
 
b8876f
@@ -1113,6 +1113,15 @@ EOP
b8876f
 	my $s = "\x{1ff}" . "f" x 32;
b8876f
 	ok($s =~ /\x{1ff}[[:alpha:]]+/gca, "POSIXA pointer wrap");
b8876f
     }
b8876f
+    {
b8876f
+        # rt
b8876f
+        fresh_perl_is(
b8876f
+            '"foo"=~/((?1)){8,0}/; print "ok"',
b8876f
+            "ok", {},  'RT #130561 - allowing impossible quantifier should not cause SEGVs');
b8876f
+        my $s= "foo";
b8876f
+        ok($s=~/(foo){1,0}|(?1)/,
b8876f
+            "RT #130561 - allowing impossible quantifier should not break recursion");
b8876f
+    }
b8876f
 } # End of sub run_tests
b8876f
 
b8876f
 1;
b8876f
-- 
b8876f
2.7.4
b8876f