develooper Front page | perl.perl5.porters | Postings from April 2011

Re: [perl #89548] Use of freed value in iteration at perlbug line 6

Thread Previous | Thread Next
From:
Abigail
Date:
April 29, 2011 02:41
Subject:
Re: [perl #89548] Use of freed value in iteration at perlbug line 6
Message ID:
20110429094109.GB4528@almanda
On Fri, Apr 29, 2011 at 02:21:40AM -0700, perlbug@plan9.de wrote:
> # New Ticket Created by  perlbug@plan9.de 
> # Please include the string:  [perl #89548]
> # in the subject line of all future correspondence about this issue. 
> # <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=89548 >
> 
> 
> 
> This is a bug report for perl from perlbug@plan9.de,
> generated with the help of perlbug 1.39 running under perl 5.12.3.
> 
> 
> -----------------------------------------------------------------
> [Please describe your issue here]
> 
> It seems "for" does not keep references to the values it iterates over.
> 
> The following script fails with "Use of freed value in iteration at ... line 6":
> 
>    my $a = my $b = { 1 => 1, 2 => 2 };
> 
>    for (values %$a, values %$b) {
>       %$b=();
>    }  
> 
> The expected result would be no output (and 4 iterations).
> 
> This is especially problematic as for cannot even detect this case
> reliably, causing any amount of corruption.
> 


The values entry in perlfunc says that values returned by C<< values >>
aren't copied; you get aliases. Considering that you wipe out %$b before
it's going to iterate over its values, bad things are bound to happen.

A work-around:

    for (@{[values %$a, values %$b]}) {
       %$b=();
    }

which forces copying the values.



Abigail

Thread Previous | Thread Next


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