Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.84k stars 524 forks source link

local(@_) in sub munges caller's @_ #4826

Closed p5pRT closed 20 years ago

p5pRT commented 22 years ago

Migrated from rt.perl.org#8231 (status was 'resolved')

Searchable as RT8231$

p5pRT commented 22 years ago

From simonm@cavalletto.org

Created by simonm@cavalletto.org

I believe I've discovered a subtle bug relating to local and @​_.

Here's my test case​:

#!/usr/bin/perl -w use strict;

print_args( 123\, 456\, 789 );

sub print_args {   tamper_args( 'All your arguments are belong to us.' );   print "My arguments were​: " . join('\, '\, @​_) . "\n"; }

sub tamper_args {   local @​_ = ( 'This is not a potato.' ); }

On my machine (perl v5.6.1 for darwin)\, the call to tamper_args somehow clobbers the argument list visible to print_args\, producing the following startling result​: "My arguments were​: All your arguments are belong to us."

This appears to be a conflict between the restoration of local values at the end of a sub {...} scope and the restoration of @​_ that always happens at the end of a sub. In particular\, the problem goes away if the local is inside another { } block (presumably because the two scope-exits are handled separately). It also goes away if tamper_args is called without a parenthesized argument list (presumably because there's no implicit local-ization in that case.

This issue was posted to PerlMonks.org\, where others reported similar behavior on Perl 5.6.x\, and a different failure condition for Perl 5.005.   http​://www.perlmonks.org/index.pl?node_id=138370

Now\, I'll grant that there's generally no need to local @​_ like this\, but I've also not been able to find anything that says you're not allowed to do so -- seems like a low priority core bug.

-Simon

Perl Info ``` Flags: category=core severity=low Site configuration information for perl v5.6.1: Configured by admin at Fri Nov 30 22:30:20 EST 2001. Summary of my perl5 (revision 5.0 version 6 subversion 1) configuration: Platform: osname=darwin, osvers=5.1, archname=darwin uname='darwin stellar.cavalletto.org 5.1 darwin kernel version 5.1: tue oct 30 00:06:34 pst 2001; root:xnuxnu-201.5.obj~1release_ppc power macintosh powerpc ' config_args='-des -Dfirstmakefile=GNUmakefile -Dldflags=-flat_namespace' hint=recommended, useposix=true, d_sigaction=define usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef useperlio=undef d_sfio=undef uselargefiles=define usesocks=undef use64bitint=undef use64bitall=undef uselongdouble=undef Compiler: cc='cc', ccflags ='-pipe -fno-common -DHAS_TELLDIR_PROTOTYPE -fno-strict-aliasing', optimize='-O3', cppflags='-pipe -fno-common -DHAS_TELLDIR_PROTOTYPE -fno-strict-aliasing' ccversion='', gccversion='Apple devkit-based CPP 6.0', gccosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=4321 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=8 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8 alignbytes=8, usemymalloc=n, prototype=define Linker and Libraries: ld='cc', ldflags ='-flat_namespace' libpth=/usr/lib libs=-lm -lc perllibs=-lm -lc libc=/System/Library/Frameworks/System.framework/System, so=dylib, useshrplib=true, libperl=libperl.dylib Dynamic Linking: dlsrc=dl_dyld.xs, dlext=bundle, d_dlsymun=undef, ccdlflags=' ' cccdlflags=' ', lddlflags='-flat_namespace -bundle -undefined suppress' Locally applied patches: @INC for perl v5.6.1: /System/Library/Perl/darwin /System/Library/Perl /Library/Perl/darwin /Library/Perl /Library/Perl /Network/Library/Perl/darwin /Network/Library/Perl /Network/Library/Perl . Environment for perl v5.6.1: DYLD_LIBRARY_PATH (unset) HOME=/var/root LANG (unset) LANGUAGE (unset) LD_LIBRARY_PATH (unset) LOGDIR (unset) PATH=~/bin/powerpc-apple- darwin:/Users/admin/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/ sbin:/sbin PERL_BADLANG (unset) SHELL=/bin/tcsh ```
p5pRT commented 22 years ago

From [Unknown Contact. See original ticket]

Confirmed in perl@​13811 (which I had lying around)\, so presumably still present in bleadperl.

Would it be reasonable just to forbid localisation of @​_? Maybe that's overkill\, since it seems to work in a loop (or block) scope\, just not in an actual subroutine scope.

.robin.

p5pRT commented 22 years ago

From @abigail

On Tue\, Jan 15\, 2002 at 02​:29​:49PM +0000\, Robin Houston wrote​:

Would it be reasonable just to forbid localisation of @​_?

No.

There are function that take @​_ as a default argument\, like shift and pop. To forbid localisation of @​_ is like forbidding localisation of $_.

Abigail

p5pRT commented 20 years ago

From @iabyn

(Just reviewing old perl bug reports).

This has been fixed in the soon-to-be-released Perl 5.8.1\, Probably by patch #19064

p5pRT commented 20 years ago

@iabyn - Status changed from 'open' to 'resolved'