@@ -4730,12 +4730,10 @@ Perl_sv_setsv_flags(pTHX_ SV *dsv, SV* ssv, const I32 flags)
47304730 }
47314731 SvFLAGS(dsv) |= sflags & (SVf_IOK|SVp_IOK|SVf_NOK|SVp_NOK|SVf_UTF8);
47324732 {
4733- const MAGIC * const smg = SvVSTRING_mg(ssv);
4734- if (smg) {
4735- sv_magic(dsv, NULL, PERL_MAGIC_vstring,
4736- smg->mg_ptr, smg->mg_len);
4737- SvRMAGICAL_on(dsv);
4738- }
4733+ const char *vstr_pv;
4734+ STRLEN vstr_len;
4735+ if (sv_vstring_get(ssv, &vstr_pv, &vstr_len))
4736+ sv_vstring_set(dsv, vstr_pv, vstr_len);
47394737 }
47404738 }
47414739 else if (sflags & (SVp_IOK|SVp_NOK)) {
@@ -17878,6 +17876,64 @@ Perl_sv_regex_global_pos_clear(pTHX_ SV *sv)
1787817876 mg->mg_len = -1;
1787917877}
1788017878
17879+ /*
17880+ =for apidoc sv_vstring_get
17881+
17882+ If the given SV has vstring magic, stores the string pointer and length into
17883+ the variables addressed by C<pvp> and C<lenp>, and returns true. If not,
17884+ returns false.
17885+
17886+ If a pointer is returned to the caller in the C<pvp> variable, it will point
17887+ to memory owned by the SV itself. The caller is not responsible for freeing
17888+ it after this call, though it will not remain valid for longer than the
17889+ lifetime of the SV itself. The caller should take a copy of it if it needs
17890+ to be accessed after this time.
17891+
17892+ =cut
17893+ */
17894+
17895+ bool
17896+ Perl_sv_vstring_get(pTHX_ SV * const sv, const char **pvp, STRLEN *lenp)
17897+ {
17898+ PERL_ARGS_ASSERT_SV_VSTRING_GET;
17899+
17900+ MAGIC *mg = SvVSTRING_mg(sv);
17901+ if(!mg) return false;
17902+
17903+ if(pvp) *pvp = mg->mg_ptr;
17904+ if(lenp) *lenp = mg->mg_len;
17905+ return true;
17906+ }
17907+
17908+ /*
17909+ =for apidoc sv_vstring_set
17910+
17911+ Applies vstring magic to the given SV, storing the string given by C<pv> for
17912+ C<len> bytes into it. If the SV already had vstring magic, the previous
17913+ stored string is freed and replaced.
17914+
17915+ =cut
17916+ */
17917+
17918+ void
17919+ Perl_sv_vstring_set(pTHX_ SV * const sv, const char *pv, STRLEN const len)
17920+ {
17921+ PERL_ARGS_ASSERT_SV_VSTRING_SET;
17922+
17923+ MAGIC *mg;
17924+ if((mg = SvVSTRING_mg(sv))) {
17925+ char *was_ptr = mg->mg_ptr;
17926+ mg->mg_ptr = savepvn(pv, len);
17927+ mg->mg_len = len;
17928+
17929+ Safefree(was_ptr);
17930+ }
17931+ else {
17932+ sv_magic(sv, NULL, PERL_MAGIC_vstring, pv, len);
17933+ SvRMAGICAL_on(sv);
17934+ }
17935+ }
17936+
1788117937/*
1788217938 * ex: set ts=8 sts=4 sw=4 et:
1788317939 */
0 commit comments