[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: "use strict" breaking run-mailcap's argument parsing ?



Hi Charles!

On Mo, 30 Sep 2019, Charles Plessy wrote:

> But after receiving reports of crashes (#939226), I have the impression,
> that "use strict" broke the script's argument parsing, for instance the
> following lines:
> 
>     elsif (m!^--(.*?)=(.*)$!) {
>         print STDERR "Warning: definition of \"$1=$2\" overrides value \"${$1}\"\n" if ($ {$1} && $ {$1} != $2);
> 	$ {$1}=$2;
>     }

Brrr, is this really what you want?  This code allows the caller to
change every variable or try to change undefined variables, which
doesn't sound like a good idea.

As far as I can see in Usage(), there is only one option allowed with
the --key=value and this is --action.  So you can explicitly check for
this: 
    elsif (m!^--action=(.*)$!) {
        print STDERR "Warning: definition of \"action=$2\" overrides value \"$action\"\n" if ($action && $action != $2);
	$action=$2;
    }

And for the three --option calls without value, I would explicitly
define them.  This makes the script way more robust:

    } elsif (m!^--debug$!) {
          print STDERR "Warning: definition of \"debug\" overrides value \"$debug\"\n" if ($debug && $debug != 1);
          $debug=1; 
    } elsif (m!^--nopager$!) {
          print STDERR "Warning: definition of \"nopager\" overrides value \"$nopager\"\n" if ($nopager && $nopager != 1);
          $nopager=1; 
    } elsif (m!^--norun$!) {
          print STDERR "Warning: definition of \"norun\" overrides value \"$norun\"\n" if ($norun && $norun != 1);
          $norun=1; 
    }

(all without testing or even perl -wc, but maybe better than the code
in git, which uses $2 without defining it ;-)

> In my understanding one can not write things like ${$foo} or ${$1} under
> "use strict", and I am tempted to remove the "use strict" statement for
> the moment.  But do you see a better solution, or is there a volunteer
> to rewrite the argument parsing in a more "strict" way ?

If you really need some generic argument parser, I'd use a hash of
them, so the parser cannot change any variable but only the keys in
the hash.  But this requires to rewrite the complete script to use the
hash instead of the scalar variables.

Greetings
Roland


Reply to: