This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make magic_setsubstr check UTF8 flag after stringification
[perl5.git] / mg.c
diff --git a/mg.c b/mg.c
index d6561c9..7b13f61 100644 (file)
--- a/mg.c
+++ b/mg.c
@@ -2246,6 +2246,7 @@ Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
     dVAR;
     STRLEN len, lsv_len, oldtarglen, newtarglen;
     const char * const tmps = SvPV_const(sv, len);
+    const char *targs;
     SV * const lsv = LvTARG(sv);
     STRLEN lvoff = LvTARGOFF(sv);
     STRLEN lvlen = LvTARGLEN(sv);
@@ -2260,8 +2261,8 @@ Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
        Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
                            "Attempt to use reference as lvalue in substr"
        );
-    if (SvUTF8(lsv)) lsv_len = sv_len_utf8_nomg(lsv);
-    else (void)SvPV_nomg(lsv,lsv_len);
+    targs = SvPV_nomg(lsv,lsv_len);
+    if (SvUTF8(lsv)) lsv_len = sv_or_pv_len_utf8(lsv,targs,lsv_len);
     if (!translate_substr_offsets(
            lsv_len,
            negoff ? -(IV)lvoff : (IV)lvoff, !negoff,