Commit a11dd64c72 for perl
commit a11dd64c720e1dc4400c4a52f2e15933f03fb00d
Author: Leon Timmermans <fawaka@gmail.com>
Date: Thu Nov 5 21:02:23 2026 +0100
Fixup on «mg: fix deferred element ownership»
29974cc644cdca03cbdc4d603b6965b0427934bf was accidentally pushed to
blead without review. It has several issues that should have been fixed
before being merged.
* Saving old_target makes sense, saving old_key does not; its decrement
is already after the targs increment.
* For readability its desirable if the decrement of old_target happens
directly after the increment of the new target, because that way all
the target handling code is together.
* In Perl_vivify_defelem there's a repeat of this dance that doesn't
seem to serve any purpose (the increment is already before the
decrement), it appears to be just a hallucination.
* Setting value = NULL is unnecessary because value is already
initialized to NULL, so that whole branch could be simplified.
* The tests for this do not belong in t/op/for.t, I moved them to
t/op/avhv.t instead.
* Most of the comments don't make sense because they appear to be about
the change and not about the code.
diff --git a/mg.c b/mg.c
index 6b3cfdc6bd..772bb6f712 100644
--- a/mg.c
+++ b/mg.c
@@ -3412,17 +3412,14 @@ Perl_defelem_target(pTHX_ SV *sv, MAGIC *mg)
}
}
if (targ && (targ != &PL_sv_undef)) {
- /* The container may own the only reference to targ. Retain
- targ and resolve the alias before releasing the container,
- since that release can run destructors. */
+ /* somebody else defined it for us */
SV * const old_target = LvTARG(sv);
- SV * const old_key = mg->mg_obj;
LvTARG(sv) = SvREFCNT_inc_simple_NN(targ);
+ SvREFCNT_dec(old_target);
LvTARGLEN(sv) = 0;
+ SvREFCNT_dec(mg->mg_obj);
mg->mg_obj = NULL;
mg->mg_flags &= ~MGf_REFCOUNTED;
- SvREFCNT_dec(old_target);
- SvREFCNT_dec(old_key);
}
return targ;
}
@@ -3475,27 +3472,19 @@ Perl_vivify_defelem(pTHX_ SV *sv)
croak(PL_no_aelem, LvSTARGOFF(sv));
else {
AV *const av = MUTABLE_AV(LvTARG(sv));
- /* A nonextendible miss has no value. LvTARG must keep the array
- until the common release or its reference is leaked. */
- if ((I32)LvTARGLEN(sv) < 0 && LvSTARGOFF(sv) > AvFILL(av))
- value = NULL;
- else {
+ if ((I32)LvTARGLEN(sv) >= 0 || LvSTARGOFF(sv) <= AvFILL(av)) {
SV* const * const svp = av_fetch(av, LvSTARGOFF(sv), TRUE);
if (!svp || !(value = *svp))
croak(PL_no_aelem, LvSTARGOFF(sv));
}
}
- /* Retain value and resolve the alias before releasing its old
- references, since those releases can run destructors. */
- SV * const old_target = LvTARG(sv);
- SV * const old_key = mg->mg_obj;
SvREFCNT_inc_simple_void(value);
+ SvREFCNT_dec(LvTARG(sv));
LvTARG(sv) = value;
LvTARGLEN(sv) = 0;
+ SvREFCNT_dec(mg->mg_obj);
mg->mg_obj = NULL;
mg->mg_flags &= ~MGf_REFCOUNTED;
- SvREFCNT_dec(old_target);
- SvREFCNT_dec(old_key);
}
int
diff --git a/t/op/avhv.t b/t/op/avhv.t
index 72d041f5b5..4480623c70 100644
--- a/t/op/avhv.t
+++ b/t/op/avhv.t
@@ -21,7 +21,7 @@ sub STORESIZE { $#{$_[0]} = $_[1]+1 }
package main;
-plan(tests => 40);
+plan(tests => 44);
# Helper function to check the typical error message.
sub not_hash {
@@ -282,3 +282,45 @@ eval {
@$avhv{"foo", "bar"} = (42, 53);
};
not_hash($@);
+
+{
+ my $saved;
+ my $array = [10, 20, 30];
+ sub { $saved = \@_ }->($array->[5]);
+ $array->[5] = 42;
+ undef $array;
+ is $saved->[0], 42, 'defelem retains an array element before its owner';
+}
+
+{
+ my $saved;
+ my $hash = { first => 10 };
+ sub { $saved = \@_ }->($hash->{later});
+ $hash->{later} = 42;
+ undef $hash;
+ is $saved->[0], 42, 'defelem retains a hash element before its owner';
+}
+
+{
+ package DefelemOwner;
+ our $destroyed = 0;
+ sub DESTROY { ++$destroyed }
+}
+
+for my $assign (0, 1) {
+ $DefelemOwner::destroyed = 0;
+ {
+ my @array = (bless({}, 'DefelemOwner'), 2, 3);
+ delete $array[1];
+ my $index = 0;
+ for my $element (@array) {
+ if ($index++ == 1) {
+ delete $array[2];
+ $element = 42 if $assign;
+ }
+ }
+ }
+ is $DefelemOwner::destroyed, 1,
+ $assign ? 'nonextendible defelem releases its array'
+ : 'unassigned defelem releases its array';
+}
diff --git a/t/op/for.t b/t/op/for.t
index ab2236a6ad..b931a66a6c 100644
--- a/t/op/for.t
+++ b/t/op/for.t
@@ -580,48 +580,6 @@ SKIP: {
is $@, "", 'vivify_defelem does not croak on &PL_sv_undef elements';
}
-{
- my $saved;
- my $array = [10, 20, 30];
- sub { $saved = \@_ }->($array->[5]);
- $array->[5] = 42;
- undef $array;
- is $saved->[0], 42, 'defelem retains an array element before its owner';
-}
-
-{
- my $saved;
- my $hash = { first => 10 };
- sub { $saved = \@_ }->($hash->{later});
- $hash->{later} = 42;
- undef $hash;
- is $saved->[0], 42, 'defelem retains a hash element before its owner';
-}
-
-{
- package DefelemOwner;
- our $destroyed = 0;
- sub DESTROY { ++$destroyed }
-}
-
-for my $assign (0, 1) {
- $DefelemOwner::destroyed = 0;
- {
- my @array = (bless({}, 'DefelemOwner'), 2, 3);
- delete $array[1];
- my $index = 0;
- for my $element (@array) {
- if ($index++ == 1) {
- delete $array[2];
- $element = 42 if $assign;
- }
- }
- }
- is $DefelemOwner::destroyed, 1,
- $assign ? 'nonextendible defelem releases its array'
- : 'unassigned defelem releases its array';
-}
-
for $x ($y) {
$x = 3;
($x, my $z) = (1, $y);