diff --git a/pp.c b/pp.c index 98edc8f37b92..65d0287e35cf 100644 --- a/pp.c +++ b/pp.c @@ -3434,12 +3434,15 @@ PP(pp_substr) SV* repl_sv_copy = NULL; if (repl_need_utf8_upgrade) { - repl_sv_copy = newSVsv(repl_sv); + repl_sv_copy = newSVpvn(repl, repl_len); sv_utf8_upgrade(repl_sv_copy); repl = SvPV_const(repl_sv_copy, repl_len); } - if (!SvOK(sv)) - SvPVCLEAR(sv); + + /* The earlier SvPV_force_nomg(sv, curlen) should have ensured + * that sv is SvOK, even if it wasn't beforehand. */ + assert(SvOK(sv)); + sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0); SvREFCNT_dec(repl_sv_copy); }