gpt4 book ai didi

perl - Perl模棱两可的命令行选项,以及-i对eval的安全性影响?

转载 作者:行者123 更新时间:2023-12-04 13:42:19 24 4
gpt4 key购买 nike

我知道这是不正确的。我只想知道perl是如何解析的呢。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

所以,我在玩perl,我想要的是perl -ne我键入的是perl -ie,该行为有点有趣,我想知道发生了什么。

$ echo 1 | perl -ie'next unless /g/i'

因此,在此上使用perl Aborted (core dumped)。阅读 perl --help我看到 -i扩展了备份。
-i[extension]     edit <> files in place (makes backup if extension supplied)

对于那些不知道 -e只是评估。所以我在想,三件事之一可能是发生的,要么被解析为
  • perl -i -e'next unless /g/i'我无法定义,其余的作为e
  • 的参数
  • perl -ie 'next unless /g/i'我得到参数e,其余的像文件名
  • 一样挂起
  • perl -i"-e'next unless /g/i'"整个东西作为i
  • 的参数

    当我运行时
    $ echo 1 | perl -i -e'next unless /g/i'

    该程序不会中止。这使我相信 'next unless /g/i'不会被解析为 -e的文字参数。毫无疑问,以上内容将以这种方式进行解析,并且结果会有所不同。

    那是什么好了,多玩一点,我得到了
    $ echo 1 | perl -ie'foo bar'
    Unrecognized switch: -bar (-h will show valid options).

    $ echo 1 | perl -ie'foo w w w'
    ... works fine guess it reads it as `perl -ie'foo' -w -w -w`

    尝试以上操作,我尝试一下...
    $ echo 1 | perl -ie'foo e eval q[warn "bar"]'
    bar at (eval 1) line 1.

    现在我真的很困惑。.那么,Perl如何解析呢?最后,看来您实际上可以从 -i内获得Perl eval命令。这对安全性有影响吗?
    $ perl -i'foo e eval "warn q[bar]" '

    最佳答案

    快速回答
    Shell报价处理正在折叠和连接它认为是所有论点的内容。您的调用相当于

    $ perl '-ienext unless /g/i'
    它会立即终止,因为perl将此参数解析为包含 -u,这触发了将开始执行代码的核心转储。这是一个曾经用于创建伪可执行文件的旧功能,但如今已成为残余。
    似乎是对 eval的调用是对 -e 'ss /g/i'的误判。
    第一条线索
    如果您恰巧在没有 B::Deparse支持的系统上运行, dump 可以成为您的 friend 。
    $ echo 1 | perl -MO=Deparse,-p -ie'next unless /g/i'
    dump is not supported.
    BEGIN { $^I = "enext"; }
    BEGIN { $/ = "\n"; $\ = "\n"; }
    LINE: while (defined(($_ = <ARGV>))) {
    chomp($_);
    (('ss' / 'g') / 'i');
    }
    那么,为什么 unle消失了?如果您使用的是Linux,那么您可能还没有达到我的水平。上面的输出来自Cygwin上的Perl,有关 dump 不支持的错误是一个提示。
    下一条线索
    值得注意的是 perlrun documentation:

    -u

    This switch causes Perl to dump core after compiling your program. You can then in theory take this core dump and turn it into an executable file by using the undump program (not supplied). This speeds startup at the expense of some disk space (which you can minimize by stripping the executable). (Still, a "hello world" executable comes out to about 200K on my machine.) If you want to execute a portion of your program before dumping, use the dump operator instead. Note: availability of undump is platform specific and may not be available for a specific port of Perl.


    工作假设和确认
    Perl的参数处理将整个块视为一个选项集,因为它以短划线开头。正如我们在 implementation for -i processing中看到的那样, enext选项使用下一个单词( -i)。
    case 'i':
    Safefree(PL_inplace);
    [Cygwin-specific code elided -geb]
    {
    const char * const start = ++s;
    while (*s && !isSPACE(*s))
    ++s;

    PL_inplace = savepvn(start, s - start);
    }
    if (*s) {
    ++s;
    if (*s == '-') /* Additional switches on #! line. */
    s++;
    }
    return s;
    对于备份文件的扩展名,perl.c中的上述代码最多消耗第一个空格字符或字符串结尾(以先到者为准)。如果保留字符,则第一个必须为空格,然后跳过它;如果下一个为破折号,则也要跳过它。在Perl中,您可以将此逻辑编写为
    if ($$s =~ s/i(\S+)(?:\s-)//) {
    my $extension = $1;
    return $extension;
    }
    然后,所有 -u-n-l-e都是有效的Perl选项,因此参数处理会吞噬它们,而变得毫无意义
    ss /g/i
    作为 -e的参数,它将perl解析为一系列的除法。但是在执行开始之前,古老的 -u导致perl转储核心。
    意外行为
    甚至更奇怪的是,如果您在 nextunless之间放置两个空格
    $ perl -ie'next  unless /g/i'
    该程序尝试运行。 Back in the main option-processing loop我们看到了
    case '*':
    case ' ':
    while( *s == ' ' )
    ++s;
    if (s[0] == '-') /* Additional switches on #! line. */
    return s+1;
    break;
    多余的空间将终止该参数的选项解析。见证人:
    $ perl -ie'next胡说-垃圾-foo'-e die
    死于-e第1行。
    但没有多余的空间,我们看到了
    $ perl -ie'next废话-垃圾--foo'-e die
    无法识别的开关:-onsense -garbage --foo(-h将显示有效选项)。
    但是,如果有多余的空格和破折号,
    $ perl -ie'next-除非/g/i'
    不支持转储。
    设计动机
    正如评论所指出的那样,逻辑存在是为了 harsh shebang ( #! ) line constraints,而pert会尽力而为。

    Interpreter scripts

    An interpreter script is a text file that has execute permission enabled and whose first line is of the form:

    #! interpreter [optional-arg]

    The interpreter must be a valid pathname for an executable which is not itself a script. If the filename argument of execve specifies an interpreter script, then interpreter will be invoked with the following arguments:

    interpreter [optional-arg] filename arg...

    where arg... is the series of words pointed to by the argv argument of execve.

    For portable use, optional-arg should either be absent, or be specified as a single word (i.e., it should not contain white space) …

    关于perl - Perl模棱两可的命令行选项,以及-i对eval的安全性影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10709127/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com