develooper Front page | perl.perl5.porters | Postings from August 2016

removing test for freed $_[N] in av_fetch()

Thread Next
From:
Dave Mitchell
Date:
August 17, 2016 08:40
Subject:
removing test for freed $_[N] in av_fetch()
Message ID:
20160817084019.GK3173@iabyn.com
Currently av_fetch() has this extra test:

    if (AvREIFY(av) && SvIS_FREED(AvARRAY(av)[key])) {
	/* eg. @_ could have freed elts */
	AvARRAY(av)[key] = NULL;	/* 1/2 reify */

which basically says that if the array has the reify flag set (typically
only @_ has this) and if the element being retrieved in it has been freed,
then replace it with an undef value instead.

This can be triggered with code like:

    sub f {
        $r = 0;
        my $var = $_[0];
    }

    $r = do { my $x; \$x };
    f($$r);

which leaves $var as undef rather than causing a "panic: attempt to copy
freed scalar".

However, code like

    my ($var) = @_;

*won't* get handled specially, and will still trigger the panic.

My feeling is that this special case should be removed from av_fetch().
It's only partially effective, and arguably it's just masking a real
issue that needs fixing in the user's code. And it slows down *all* array
fetches.

Nothing in the test suite fails with this condition removed.

It will also cease to be an issue if we ever get round to making the 
perl stack or @_ reference counted.

It was added in 1996 as a result of this thread:

    From: Andreas Koenig <k@anna.in-berlin.de>
    Subject: SEGV with $_[0] and circular references
    Message-Id: <199608131528.RAA25965@anna.in-berlin.de>

That was in the context of getting a SEGV - whereas now we the
"panic: attempt to copy freed scalar" instead.

-- 
Spock (or Data) is fired from his high-ranking position for not being able
to understand the most basic nuances of about one in three sentences that
anyone says to him.
    -- Things That Never Happen in "Star Trek" #19

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