diff options
author | David Mitchell <davem@iabyn.com> | 2010-09-11 23:30:44 +0100 |
---|---|---|
committer | David Mitchell <davem@iabyn.com> | 2010-09-11 23:42:02 +0100 |
commit | 4596056478d3ae4ae183d2821eb95156aff83924 (patch) | |
tree | 579af715364085f5c077feea7895be4061f8d110 /t | |
parent | 37f6eaa490d61972fa887e4e7bbeaf0de90b3ee9 (diff) | |
download | perl-4596056478d3ae4ae183d2821eb95156aff83924.tar.gz |
list cxt hash assign with dups gives garbage
Fix for #31865: weird results from reverse( %x = reverse %h )
Basically, anything of the form
@a = %h = (list with some duplicate keys)
may have left @a containing weird and/or freed values.
There was a partial fix for this with ca65944e, but it was broken
(it did one big block move on the stack at the end to remove duplicates,
but duplicates weren't necessarily all in one block.)
The new fix is a two-stage process. First, while pulling key/value pairs
of the stack and assigning them to the hash, each key/val pair is written
back to the stack - possibly at a lower position if there are duplicates to
be skipped.
Finally at the end if any duplicates have been detected, then in list
context, a single pass is made through the stack, and for each key/val
pair, the key is looked up and the val on the stack is overwritten with
the new value (replacing possibly freed or other garbage values).
Diffstat (limited to 't')
-rw-r--r-- | t/op/hashassign.t | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/t/op/hashassign.t b/t/op/hashassign.t index 92ea5ca2e9..db0cf92263 100644 --- a/t/op/hashassign.t +++ b/t/op/hashassign.t @@ -8,7 +8,7 @@ BEGIN { # use strict; -plan tests => 217; +plan tests => 218; my @comma = ("key", "value"); @@ -273,11 +273,13 @@ foreach my $chr (60, 200, 600, 6000, 60000) { } # now some tests for hash assignment in scalar and list context with -# duplicate keys [perl #24380] +# duplicate keys [perl #24380], [perl #31865] { my %h; my $x; my $ar; is( (join ':', %h = (1) x 8), '1:1', 'hash assignment in list context removes duplicates' ); + is( (join ':', %h = qw(a 1 a 2 b 3 c 4 d 5 d 6)), 'a:2:b:3:c:4:d:6', + 'hash assignment in list context removes duplicates 2' ); is( scalar( %h = (1,2,1,3,1,4,1,5) ), 2, 'hash assignment in scalar context' ); is( scalar( ($x,%h) = (0,1,2,1,3,1,4,1,5) ), 3, |