This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
PATCH: [perl #134325] Heap buffer overflow
authorKarl Williamson <khw@cpan.org>
Sun, 25 Aug 2019 01:17:19 +0000 (19:17 -0600)
committerKarl Williamson <khw@cpan.org>
Tue, 27 Aug 2019 04:00:42 +0000 (22:00 -0600)
This was the result of trying to continue to parse after realizing that
we were going to have to reparse using long jumps.  This continuing only
happened when we realized we were going to have to reparse in order to
count parentheses anyway, and it was an attempt to save a pass in the
regex compiler, as without doing the continuing we'd restart the parse
to use long jumps from the beginning, and then when finished, would
restart the parse to count the parentheses.

However, in most cases this doesn't help, as when we get towards the end
of the parse (as in the test case in this ticket), we need the long
jump, and will segfault because we don't have it.  So we need the extra
pass anyway.

So this commit restarts the parse as soon as we discover we are going to
need longjumps

regcomp.c
t/re/pat.t

index 05dd9a5..abb029f 100644 (file)
--- a/regcomp.c
+++ b/regcomp.c
@@ -377,12 +377,8 @@ struct RExC_state_t {
 #define REQUIRE_BRANCHJ(flagp, restart_retval)                              \
     STMT_START {                                                            \
                 RExC_use_BRANCHJ = 1;                                       \
-                if (LIKELY(! IN_PARENS_PASS)) {                             \
-                    /* No need to restart the parse immediately if we're    \
-                     * going to reparse anyway to count parens */           \
-                    *flagp |= RESTART_PARSE;                                \
-                    return restart_retval;                                  \
-                }                                                           \
+                *flagp |= RESTART_PARSE;                                    \
+                return restart_retval;                                      \
     } STMT_END
 
 /* Until we have completed the parse, we leave RExC_total_parens at 0 or
index e54affc..6a868f4 100644 (file)
@@ -25,7 +25,7 @@ BEGIN {
 skip_all('no re module') unless defined &DynaLoader::boot_DynaLoader;
 skip_all_without_unicode_tables();
 
-plan tests => 863;  # Update this when adding/deleting tests.
+plan tests => 864;  # Update this when adding/deleting tests.
 
 run_tests() unless caller;
 
@@ -33,7 +33,6 @@ run_tests() unless caller;
 # Tests start here.
 #
 sub run_tests {
-
     my $sharp_s = uni_to_native("\xdf");
 
     {
@@ -2105,6 +2104,17 @@ x{0c!}\;\;îçÿ \0\7f/0f/!\0F\ 5\0\0/;îçÿù\Q\0\ 1\0\0x\10ÿÿÿÿ\0\0\0ù\0\0\0\7f`x{0c!}\ 1;\0\0\0ù\Q
                         eval $z;:, "", {}, 'foo');
     }
 
+    {   # [perl #134325]
+        my $quote="\\Q";
+        my $back="\\\\";
+        my $ff="\xff";
+        my $s = sprintf "/\\1|(|%s)%s%s   /i",
+                        $quote x 8 . $back x 69,
+                        $quote x 5 . $back x 4,
+                        $ff x 48;
+        like(runperl(prog => "$s", stderr => 1), qr/Unmatched \(/);
+   }
+
 } # End of sub run_tests
 
 1;