Skip to content

Commit f6107ca

Browse files
committed
Strengthen weak refs when sorting in-place
It's conceptually an assignment, which should strengthen any weak refs. Reported-by: Tom Molesworth <[email protected]>
1 parent ae2cf9f commit f6107ca

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pp_sort.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,9 @@ PP(pp_sort)
17691769
base[i] = newSVsv(sv);
17701770
else
17711771
SvREFCNT_inc_simple_void_NN(sv);
1772+
1773+
if (SvWEAKREF(sv))
1774+
sv_rvunweaken(sv);
17721775
}
17731776
av_clear(av);
17741777
if (max > 0) {

t/op/sort.t

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ BEGIN {
77
set_up_inc('../lib');
88
}
99
use warnings;
10-
plan(tests => 197);
10+
plan(tests => 198);
1111

1212
# these shouldn't hang
1313
{
@@ -492,7 +492,18 @@ cmp_ok($x,'eq','123',q(optimized-away comparison block doesn't take any other ar
492492
@a = sort { $a <=> $b } @a;
493493
$$r = "z";
494494
is ("@a", "3 4 5", "RT #128340");
495+
}
495496

497+
# in-place sorting of weak references
498+
SKIP: {
499+
skip_if_miniperl("no dynamic loading on miniperl, no extension Scalar::Util", 1);
500+
require Scalar::Util;
501+
my @a = map { \(my $dummy = $_) } qw(c a d b);
502+
my $r = $a[1];
503+
Scalar::Util::weaken($a[1]);
504+
@a = sort { $$a cmp $$b } @a;
505+
undef $r;
506+
ok defined $a[0] && ${$a[0]} eq 'a', "in-place sort strengthens weak references";
496507
}
497508

498509
# Test optimisations of reversed sorts. As we now guarantee stability by

0 commit comments

Comments
 (0)