develooper Front page | perl.perl5.porters | Postings from February 2018

[perl #132729] array element lazily created at wrong index

From:
Father Chrysostomos via RT
Date:
February 19, 2018 02:14
Subject:
[perl #132729] array element lazily created at wrong index
Message ID:
rt-4.0.24-9992-1519006462-363.132729-15-0@perl.org
On Tue, 16 Jan 2018 16:42:53 -0800, zefram@fysh.org wrote:
> 
> This is a bug report for perl from zefram@fysh.org,
> generated with the help of perlbug 1.41 running under perl 5.27.7.
> 
> 
> -----------------------------------------------------------------
> [Please describe your issue here]
> 
> Simple test case for some array operations:
> 
> $ perl -lwe '$a[0] = undef; $a[1] = 1; sub foo { unshift @a, 7; $_[0]
> = 3; } foo($a[0]); print map { $_ // "u" } @a;'
> 731
> 
> The above works as expected.  Note that the sub call aliases $_[0]
> to the original $a[0], and that aliasing holds through the unshift.
> After unshift, that element is now $a[1], and an assignment to it is
> made successfully through the $_[0] alias.
> 
> But if the initial $a[0] isn't explicitly initialised, this goes awry:
> 
> $ perl -lwe '$a[1] = 1; sub foo { unshift @a, 7; $_[0] = 3; }
> foo($a[0]); print map { $_ // "u" } @a;'
> 3u1
> 
> This time, $_[0] after the unshift is aliasing the *new* $a[0], which
> didn't exist to be aliased at the time the sub call set up the alias.
> The lazy element creation is being too lazy, deferring resolution of
> the array index until it's too late.
> 
> The same effect can be seen by using foreach(@a) to set up an alias:
> 
> $ perl -lwe '$a[0] = undef; $a[1] = 1; foreach(@a) { unshift @a, 7; $_
> = 3; last; } print map { $_ // "u" } @a;'
> 731
> $ perl -lwe '$a[1] = 1; foreach(@a) { unshift @a, 7; $_ = 3; last; }
> print map { $_ // "u" } @a;'
> 3u1

The branch merged as 2a05854 includes a partial fix for the bug.  It applies to nonexistent elements within the array.

The fix doesn’t apply to elements outside the array.

-- 

Father Chrysostomos


---
via perlbug:  queue: perl5 status: new
https://rt.perl.org/Ticket/Display.html?id=132729



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About