This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c: Fix -Dr bug
authorKarl Williamson <khw@cpan.org>
Thu, 11 Feb 2016 17:12:57 +0000 (10:12 -0700)
committerKarl Williamson <khw@cpan.org>
Fri, 19 Feb 2016 03:26:49 +0000 (20:26 -0700)
It was using a wrong length calculation, which under some circumstances
caused the output to include extra bytes.  Also I added comments, and
changed a variable name, so I don't have to figure this out again from
scratch.

regcomp.c

index e320c2e..6549508 100644 (file)
--- a/regcomp.c
+++ b/regcomp.c
@@ -18429,11 +18429,16 @@ Perl_regprop(pTHX_ const regexp *prog, SV *sv, const regnode *o, const regmatch_
                                                     bitmap_invlist);
                 if (lv && lv != &PL_sv_undef) {
                     char *s = savesvpv(lv);
-                    char * const origs = s;
+                    const char * const orig_s = s;  /* Save the beginning of
+                                                       's', so can be freed */
 
+                    /* Ignore anything before the first \n */
                     while (*s && *s != '\n')
                         s++;
 
+                    /* The data are one range per line.  A range is a single
+                     * entity; or two, separated by \t.  So can just convert \n
+                     * to space and \t to '-' */
                     if (*s == '\n') {
                         const char * const t = ++s;
 
@@ -18454,10 +18459,10 @@ Perl_regprop(pTHX_ const regexp *prog, SV *sv, const regnode *o, const regmatch_
                             if (*s == '\n') {
 
                                 /* Truncate very long output */
-                                if (s - origs > 256) {
+                                if ((UV) (s - t) > 256) {
                                     Perl_sv_catpvf(aTHX_ sv,
                                                 "%.*s...",
-                                                (int) (s - origs - 1),
+                                                (int) (s - t),
                                                 t);
                                     goto out_dump;
                                 }
@@ -18468,15 +18473,18 @@ Perl_regprop(pTHX_ const regexp *prog, SV *sv, const regnode *o, const regmatch_
                             }
                             s++;
                         }
+
+                        /* Here, it fits in the allocated space.  Replace a
+                         * final blank with a NUL */
                         if (s[-1] == ' ')
-                            s[-1] = 0;
+                            s[-1] = '\0';
 
                         sv_catpv(sv, t);
                     }
 
                   out_dump:
 
-                    Safefree(origs);
+                    Safefree(orig_s);
                     SvREFCNT_dec_NN(lv);
                 }