This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c: Add a [ to fake string only if original had one
authorKarl Williamson <khw@cpan.org>
Fri, 20 Mar 2020 04:17:58 +0000 (22:17 -0600)
committerKarl Williamson <khw@cpan.org>
Fri, 20 Mar 2020 13:44:31 +0000 (07:44 -0600)
This code is assembling a fake string to parse, modified from the
original.  Prior to a future commit, this was only called when there was
a bracketed character class, so it made sense to unconditionally add
the bracket.  But soon this will be called when there isn't a bracket,
so one shouldn't be added.

regcomp.c

index ffb9cf9..0c3b37f 100644 (file)
--- a/regcomp.c
+++ b/regcomp.c
@@ -18443,13 +18443,19 @@ S_regclass(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth,
         /* If the character class contains anything else besides these
          * multi-character folds, have to include it in recursive parsing */
         if (element_count) {
-            sv_catpvs(substitute_parse, "|[");
+            bool has_l_bracket = orig_parse > RExC_start && *(orig_parse - 1) == '[';
+
+            sv_catpvs(substitute_parse, "|");
+            if (has_l_bracket) {    /* Add an [ if the original had one */
+                sv_catpvs(substitute_parse, "[");
+            }
             constructed_prefix_len = SvCUR(substitute_parse);
             sv_catpvn(substitute_parse, orig_parse, RExC_parse - orig_parse);
 
-            /* Put in a closing ']' only if not going off the end, as otherwise
-             * we are adding something that really isn't there */
-            if (RExC_parse < RExC_end) {
+            /* Put in a closing ']' to match any opening one, but not if going
+             * off the end, as otherwise we are adding something that really
+             * isn't there */
+            if (has_l_bracket && RExC_parse < RExC_end) {
                 sv_catpvs(substitute_parse, "]");
             }
         }