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

List::Util does not do SvGETMAGIC where I expect it to, and does thewrong thing with gmagic vars like $#array

From:
demerphq
Date:
September 14, 2016 21:21
Subject:
List::Util does not do SvGETMAGIC where I expect it to, and does thewrong thing with gmagic vars like $#array
Message ID:
CANgJU+Xnrmje-eZVbJhRa8bvpbv9DdTdDgLsE+A1m8CaHLksuw@mail.gmail.com
I'm not really sure if this is a List::Util bug, or if it is a Perl bug.

$#array returns a magic value (apparently to support assignment),
which needs SvGETMAGIC() on it before it can be safely used.

This then makes List::Util::min() and List::Util::sum() do the wrong
thing with $#array type values under certain situations.

I have fixed this in a Perl branch below, however I admit to some
surprise at the lack of SvGETMAGIC in many places in the code in
List::Util and Scalar::Util.

I did a *quick* audit and noticed that sum() also can be convinced to
mishandle $#array.

What I find quite odd however is that max() which shares an XS sub
with min() does not seem to exhibit the same issues. I have not been
able to find an explanation for this.

Another strange thing I found is that the tests which fail in the
patch change if you remove the call the max(). So I half suspect that
there is some internal issue here.

I have not tried to work through and find all the possible issues with
List::Util, but my gut feeling is that all of the code should be
calling SvGETMAGIC() on every argument that it touches before it does
anything with the values it is passed. I find it odd that there is a
LOT of logic about amagic, but none about gmagic. Shouldnt it handle
both?

I can file a bug against the RT List::Util if you wish, or file a bug
against Perl. Or both. Not sure the right thing to do here.

This bug seems to manifest in every version of Perl/List::Util since
the 5.18.2 release that I tested, iow since 1.27 at least.

cheers,
Yves

On 14 September 2016 at 23:03, Yves Orton <demerphq@gmail.com> wrote:
> In perl.git, the branch yves/list_util_bugfix has been created
>
> <http://perl5.git.perl.org/perl.git/commitdiff/c5b6be5d534a2208cb9ecfdfb7081a0c4ea8ab90?hp=0000000000000000000000000000000000000000>
>
>         at  c5b6be5d534a2208cb9ecfdfb7081a0c4ea8ab90 (commit)
>
> - Log -----------------------------------------------------------------
> commit c5b6be5d534a2208cb9ecfdfb7081a0c4ea8ab90
> Author: Yves Orton <demerphq@gmail.com>
> Date:   Wed Sep 14 23:02:08 2016 +0200
>
>     sum/min/max in List::Util need to call SvGETMAGIC
> -----------------------------------------------------------------------
>
> --
> Perl5 Master Repository



-- 
perl -Mre=debug -e "/just|another|perl|hacker/"



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