Commit 002ed9c92d for perl

commit 002ed9c92dbed62ba485d4b507b4112c69ea1354
Author: Richard Leach <rich+perl@hyphen-dash-hyphen.info>
Date:   Wed Nov 4 14:05:46 2026 +0000

    pp_subst: handle a known replacement more safely & efficiently

    In the unlikely event of a known replacement not being a simple string,
    there was the potential (via the `force_it` path) to fail to upgrade
    the stringified representation to UTF-8 correctly when needed.

    With this commit, stringification happens early to try to ensure that
    upgrading always occurs when required.

    (Spotted by a Claude code review of the working branch.)

    This was also an opportunity to replace a mortal SV copy and
    `sv_utf8_upgrade` pass with use of the more modern
    `bytes_to_utf8_temp_pv`.

    (Spotted by the human.)

    Review comments also suggested renaming `doutf8` to `do_utf8` for
    legibility and using C99's `true/false` rather than the older macros.

diff --git a/pp_hot.c b/pp_hot.c
index 4ae893cd74..e474888d4c 100644
--- a/pp_hot.c
+++ b/pp_hot.c
@@ -5417,11 +5417,10 @@ PP(pp_subst)
     STRLEN len;
     int force_on_match = 0;
     const I32 oldsave = PL_savestack_ix;
-    bool doutf8 = FALSE; /* whether replacement is in utf8 */
+    bool do_utf8 = false; /* whether replacement is in utf8 */
 #ifdef PERL_ANY_COW
     bool was_cow;
 #endif
-    SV *nsv = NULL;
     SSize_t sp_offset = 0; /* number of items left on stack */
     SV *dstr;
     SV *retval;
@@ -5548,16 +5547,13 @@ PP(pp_subst)

     /* known replacement string? */
     if (dstr) {
-        /* replacement needing upgrading? */
-        if (DO_UTF8(TARG) && !doutf8) {
-             nsv = sv_mortalcopy_flags(dstr, SV_GMAGIC|SV_DO_COW_SVSETSV);
-             sv_utf8_upgrade(nsv);
-             c = SvPV_const(nsv, clen);
-             doutf8 = TRUE;
-        }
-        else {
-            c = SvPV_const(dstr, clen);
-            doutf8 = DO_UTF8(dstr);
+        /* Stringify once to avoid duplicate calls to GET magic/overloads */
+        c = SvPV_const(dstr, clen);
+        do_utf8 = DO_UTF8(dstr);
+
+        if (DO_UTF8(TARG) && !do_utf8) {
+            c = (const char *)bytes_to_utf8_temp_pv((const U8 *)c, &clen);
+            do_utf8 = true;
         }

         if (UNLIKELY(TAINT_get))
@@ -5565,9 +5561,9 @@ PP(pp_subst)
     }
     else {
         c = NULL;
-        doutf8 = FALSE;
+        do_utf8 = false;
     }
-
+
     if (c
 #ifdef PERL_ANY_COW
         && !was_cow
@@ -5578,7 +5574,7 @@ PP(pp_subst)
            || (!SvGMAGICAL(dstr) && !(RXp_EXTFLAGS(prog) & RXf_EVAL_SEEN))
            )
         && !(RXp_EXTFLAGS(prog) & RXf_NO_INPLACE_SUBST)
-        && (!doutf8 || SvUTF8(TARG))
+        && (!do_utf8 || SvUTF8(TARG))
         && !(rpm->op_pmflags & PMf_NONDESTRUCT))
     {
         /* known replacement string and can do in-place substitution */
@@ -5735,7 +5731,7 @@ PP(pp_subst)
             if (first) {
                 /* replacement already stringified */
               if (clen)
-                sv_catpvn_nomg_maybeutf8(dstr, c, clen, doutf8);
+                sv_catpvn_nomg_maybeutf8(dstr, c, clen, do_utf8);
               first = FALSE;
             }
             else {
diff --git a/t/re/subst.t b/t/re/subst.t
index e669ae07a7..f4c913d97b 100644
--- a/t/re/subst.t
+++ b/t/re/subst.t
@@ -10,7 +10,7 @@ BEGIN {
 use Config;
 use constant constcow => *Config::{NAME};

-plan(tests => 281);
+plan(tests => 285);

 $_ = 'david';
 $a = s/david/rules/r;
@@ -1200,3 +1200,17 @@ __EOF__
     is( $ret[1], $str,
         "Issue #20690 - s/b{0}//gr on utf8 string should not remove anything");
 }
+
+{
+    # Note: these tests are only likely to fail on non-COW builds
+    my $r = "\xe9";
+    my $v = v256.97.98;
+    $v =~ s/a/$r/;
+    is($v, "\x{100}\x{e9}b", "byte replacement, UTF-8 v-string target");
+    ok(utf8::valid($v), "... result is valid UTF-8");
+
+    my $g = *{"main::\x{100}a"};
+    $g =~ s/a/$r/;
+    is($g, "*m\xe9in::\x{100}a", "byte replacement, UTF-8 glob copy target");
+    ok(utf8::valid($g), "... result is valid UTF-8");
+}