From 7b9bc213c31e3de23003735de426d18e4d8fac8d Mon Sep 17 00:00:00 2001 From: Tony Cook Date: Thu, 13 Apr 2023 13:43:59 +1000 Subject: [PATCH] reset(): call set magic when clearing SVs reset() with an argument would clear the specified SVs when requested but didn't call set magic, this would result in the effect on magic variables not applying, such as the $| and $^W used in the ticket. This isn't a problem for AVs and HVs as their corresponding clear functions do call the appropriate clear magic. Fixes #20763 --- sv.c | 5 ++++- t/op/reset.t | 26 +++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/sv.c b/sv.c index 4e15c6236219..6bf92f9d1a71 100644 --- a/sv.c +++ b/sv.c @@ -10126,7 +10126,10 @@ Perl_sv_resetpvn(pTHX_ const char *s, STRLEN len, HV * const stash) sv = GvSV(gv); if (sv && !SvREADONLY(sv)) { SV_CHECK_THINKFIRST_COW_DROP(sv); - if (!isGV(sv)) SvOK_off(sv); + if (!isGV(sv)) { + SvOK_off(sv); + SvSETMAGIC(sv); + } } if (GvAV(gv)) { av_clear(GvAV(gv)); diff --git a/t/op/reset.t b/t/op/reset.t index bb5fbfce5ee5..e4dcfc77f35b 100644 --- a/t/op/reset.t +++ b/t/op/reset.t @@ -7,7 +7,7 @@ BEGIN { } use strict; -plan tests => 40; +plan tests => 45; package aiieee; @@ -190,6 +190,30 @@ SKIP: } } +# magic reset #20763 +{ + { + local $^W = 1; # we're resetting this + my $warn = ''; + local $SIG{__WARN__} = sub { $warn .= "@_\n" }; + reset "\cW"; + like($warn, qr/uninitialized/, "magic tries to SvIV() the new value"); + $warn = ''; + is($^W, 0, q"check $^W has been zeroed"); + is($warn, '', "should be no more warnings"); + } + { + local $| = 1; + no warnings 'uninitialized'; + reset '|'; + is($|, 0, q"check magic applied to $|"); + } + + eval { reset "1" }; + like($@, qr/Modification of a read-only value attempted/, + "\$1 isn't marked read-only, but throws on set magic"); +} + __DATA__ #!perl use warnings;