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

[perl #129336] [PATCH] "#!perl -i whatever" is interpreted as"#!perl -i -whatever"

Thread Next
From:
Dan Collins via RT
Date:
September 23, 2016 06:25
Subject:
[perl #129336] [PATCH] "#!perl -i whatever" is interpreted as"#!perl -i -whatever"
Message ID:
rt-4.0.24-13249-1474611911-670.129336-14-0@perl.org
On Thu Sep 22 23:22:45 2016, dcollinsn@gmail.com wrote:
> Hello,
> 
> There is a bug in argument parsing of the -i flag. Perl_moreswitches
> processes a single switch, and returns a pointer to the start of the
> next switch. It can return either the a pointer to the next flag
> itself:
> 
> #!perl -n -p
>            ^ Can point here
> 
> Or, to the space before the next "arg":
> 
> #!perl -n -p
>          ^ Can point here
> 
> (Where the next call to Perl_moreswitches will consume " -".)
> 
> In the case of -i[extension], the pointer is by default pointing at
> the space after the end of the argument. The current code tries to do
> the former, by unconditionally advancing the pointer, and then
> advancing it again if it is on a '-'. But that is incorrect:
> 
> #!perl -i p
>           ^ Will point here, but that isn't a flag
> 
> This is a problem if -i is the last argument before either a file
> name, or a malformed argument. This normally isn't a problem on the
> command line because Perl_moreswitches operates on elements of `argv`
> one at a time. However, someone who typed `perl "-ifoo Vbar.pl"` into
> bash, or someone who has a messed up #! line, will encounter this.
> 
> I could fix this by removing the unconditional s++, and having it
> increment by 2 if *(s+1)=='-', but this work isn't actually necessary
> - it's better to just remove the special-casing for -i and leave it
> pointing at the space after the argument.
> 
> Found with AFL, believe it or not. This is not a regression, and all
> tests pass for me. I've added a test for this, it basically makes sure
> that `#!perl -i u` does /not/ crash.
> 
> Clicking submit so I get an RT number.

Patch attached.

-- 
Respectfully,
Dan Collins

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