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

Re: [ID 20010823.012] Core dump when redefining sort sub

Thread Previous
From:
John P. Linderman
Date:
April 15, 2002 08:30
Subject:
Re: [ID 20010823.012] Core dump when redefining sort sub
Message ID:
200204151500.LAA04367@raptor.research.att.com
Hugo noted:
> gamma@mintaka.iern.disa.mil wrote:
> :Perl gives a segmentation fault and core dump when redefining
> :sort subroutine and calling it via goto &NAME. E.g
> :
> :@sorted = sort foo @myarray
> :
> :sub foo {
> :  goto &sortsub
> :}
> :
> :sub sortsub{
> :#sort code
> :}
> 
> Just found this bug report back. I can confirm that it still coredumps
> in latest bleadperl. Here's the complete testcase:
>   crypt% ./perl -wle 'sub s1 { goto &s2 } sub s2 { $a cmp $b } print sort s1 (3, 2, 1)'
>   Use of uninitialized value in string comparison (cmp) at -e line 1.
>   Use of uninitialized value in string comparison (cmp) at -e line 1.
>   Segmentation fault (core dumped)
>   crypt% 
> 
> I don't really understand what is going on here, but I suspect that
> it may relate to the behaviour near pp_sort.c:1467:
>                 SAVEVPTR(CvROOT(cv)->op_ppaddr);
>                 CvROOT(cv)->op_ppaddr = PL_ppaddr[OP_NULL];
> ... which I suspect is modifying the last op of the sortsub to change
> how it returns. If I'm right, this modification is then bypassed by
> the goto; it would also cause problems with a sortsub that wanted
> to call itself recursively (directly or indirectly) or redefine
> itself (hence the caveat).
> 
> Does anyone understand this well enough to tell me whether I'm right,
> and explain what is really going on here?
> 
> Hugo

Looks like this one is already documented... (from perlfunc.pod)

=item goto LABEL

=item goto EXPR

=item goto &NAME

The C<goto-LABEL> form finds the statement labeled with LABEL and resumes
execution there.  It may not be used to go into any construct that
requires initialization, such as a subroutine or a C<foreach> loop.  It
also can't be used to go into a construct that is optimized away,
or to get out of a block or subroutine given to C<sort>.



Thread Previous


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