Fix off-by-one bug in ' to :: substitution
Leon Timmermans [Sun, 19 Jun 2016 18:35:39 +0000 (20:35 +0200)]
Name.xs

diff --git a/Name.xs b/Name.xs
index 10a8cd2..6168073 100644 (file)
--- a/Name.xs
+++ b/Name.xs
@@ -67,24 +67,25 @@ subname(name, sub)
                        end = s - 1;
                        begin = ++s;
                        if (seen_quote)
-                               seen_quote++;
+                               need_subst++;
                }
                else if (*s && s[-1] == '\'') {
                        end = s - 1;
                        begin = s;
-                       seen_quote++;
+                       if (seen_quote++)
+                               need_subst++;
                }
        }
        s--;
        if (end) {
                SV* tmp;
-               if (seen_quote > 1) {
-                       STRLEN length = end - nameptr + seen_quote;
+               if (need_subst) {
+                       STRLEN length = end - nameptr + seen_quote - (*end == '\'' ? 1 : 0);
                        char* left;
                        int i, j;
                        tmp = newSV(length);
                        left = SvPVX(tmp);
-                       for (i = 0, j = 0; j <= end - nameptr; ++i, ++j) {
+                       for (i = 0, j = 0; j < end - nameptr; ++i, ++j) {
                                if (nameptr[j] == '\'') {
                                        left[i] = ':';
                                        left[++i] = ':';
@@ -93,7 +94,7 @@ subname(name, sub)
                                        left[i] = nameptr[j];
                                }
                        }
-                       stash = gv_stashpvn(left, i - 2, GV_ADD | utf8flag);
+                       stash = gv_stashpvn(left, length, GV_ADD | utf8flag);
                        SvREFCNT_dec(tmp);
                }
                else