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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Charles Plessy-12
Hello, debian-perl !

Following #803729 I have added "use strict" to the run-mailcap Perl
script, which is probably installed on your system.

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;
    }

https://salsa.debian.org/debian/mime-support/blob/master/run-mailcap#L328-331

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 ?

Have a nice day,

Charles

--
Charles Plessy
Akano, Uruma, Okinawa, Japan

Reply | Threaded
Open this post in threaded view
|

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

Roland Rosenfeld-2
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 | Threaded
Open this post in threaded view
|

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

Damyan Ivanov-5
-=| Roland Rosenfeld, 30.09.2019 17:17:07 +0200 |=-
> 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.

Or use Getopt::Long -- everything about parsing command line options,
including putting values in (pre-defined) scalars, arrays and hashes.


-- Damyan