这段使用Switch.pm的代码安全吗?

4
在我们公司,我们使用这段代码(在结尾处给出)已经约10年时间,一直运行良好。
几天前我们遇到了一些问题,不得不重新编写整个包,我们决定用Damian的Switch模块来替换这段代码(以提高代码的可读性)。
对我们来说一切都很正常。
后来我在Perlmonks上发现,Damian把这个模块放在了

Damian modules you shouldn't use in production because their purpose is to explore and prototype future core language features.

但是它对我们来说很好用,因为我们没有触及到这个模块的限制(我想)。
现在我要求大家请看看两种实现(嵌套if else vs switch),并让我知道在新实现中使用Switch是否可以,或者我们是否正在为自己创造未来的问题?在下面给出的代码中使用Switch是否可以,或者是否存在任何隐藏的错误/问题?
我已经阅读了CPAN和Perlmonks上关于这个模块的错误和评论,我认为我们的代码离那些错误还很远(我想是这样的)。
我们正在使用 Perl 5.8.5。
PS:我知道 Switch 的替代方案,Perl 5.10 中有 given/when,我们可以使用 dispatch table 和其他解决方案,这些都在 这里 指定,但现在我们只想比较使用 Switch 的新实现。
使用嵌套 if else。
if ($command =~ /^enter$/) {
    $self->show_main_frames();
}       
elsif ($command =~ /^XYZ_MENU/i) {
    $self->show_main_menu($manual, $dbot);
}
elsif ($command =~ /^DBOT/i) {
    $dbot->process();
}
# XML is used for the reminders-history: Request 2666
elsif ($command =~ /^XML_DBOT/i) {
    $dbot->process();
}
elsif ($command =~ /^UGS/i) {
    $ugsui->process();
}
elsif ($command eq "kill") {
    my $login = $self->{COMMON_HASH}{login} || "";
    my $su_login = $self->{CONF}->get("start", "SU_LOGIN");
    if ($login eq $su_login) {      
            # usually only certain user with certain permission will be
            # able to do this. 
            $self->do_error("Daemon was killed by ".$login);
            $self->db_connection->disconnect();
            $self->{LOG}->write("User $login killed the daemon", 0);
            exit; # this 'exit' actually kill the daemon
    }
    else {
            $self->do_error("User $login tried to kill the daemon. ".
            "This incident will be reported");
            $self->{LOG}->write("User $login tried to kill the daemon", 2);
    }
}
elsif ($command eq "logout") {
    # check if we should delete the password cookie
    my $forget_me = $self->{CGI}->param("forget_me") || 0;
    if ($forget_me) {
            $self->{DB_PASSWORD_COOKIE}->delete_cookie();
    }

    $ugsui->do_logout();
    # Cliff edit remove id from logged_in
    $session->remove_session($session->login());
    # delete the session of the user
    delete $self->{SESSIONS}{$session->id()};
    if ($self->{CACHE_TO_FILE}) {
            my $session_data_path = 
                XYZ_DIR
            ."/code/cache/session_data"
        .$session->id();
            unlink($session_data_path);
    }
}
# if we just login we should create all the main frames        
elsif ($command eq "login") {
    # if extra_param holds "command*XXX" the XXX will be placed instead of
    # the command. extra_param holds pairs that are astrics-separated
    my $extra_param = $cgi->param("extra_param");
    $extra_param = "" if (!defined($extra_param));
    $extra_param =~ /command\*([^\*]+)/i;
    my $other_command = defined($1) ? $1 : "";
    if ($other_command =~ /^dbot/i) { # meanwhile - works only on dbot 
                                  # commands
        $command = $other_command;
            # now we will get the other parameters from the extra_param 
            # (actually including the command that is still in the 
            # $extra_param)         
            while ($extra_param =~ /^\*?([^\*]+)\*([^\*]+)(.*)/) {
            $extra_param = $3;
            my $name = $1;
            my $value = $2;     
            $cgi->param(-name => $name,
                 -value => $value);
            }#end while
    }#end if
    else{
    $self->show_main_frames();
    }
}#end elsif
else {
    $self->show_main_frames();
}#end outer else

使用Switch
switch ($command) 
{
    case /^enter$/      { $self->show_main_frames() }
    case /^XYZ_MENU/i   { $self->show_main_menu($manual, $dbot) }
    case /^DBOT/i       { $dbot->process() }
        case /^XML_DBOT/i   { $dbot->process() }
        case /^UGS/i        { $ugsui->process() }
        case "kill"     {
                my $login = $self->{COMMON_HASH}{login} || "";
                    my $su_login = $self->{CONF}->get("start", "SU_LOGIN");
                if ($login eq $su_login) {      
                        # usually only certain user with certain permission will be
                        # able to do this. 
                        $self->do_error("Daemon was killed by ".$login);
                        $self->db_connection->disconnect();
                        $self->{LOG}->write("User $login killed the daemon", 0);
                        exit; # this 'exit' actually kill the daemon
                    }
                else    {
                        $self->do_error("User $login tried to kill the daemon. ".
                            "This incident will be reported");
                        $self->{LOG}->write("User $login tried to kill the daemon", 2);
                    }
                    }
        case "logout"       {
                    # check if we should delete the password cookie
                    my $forget_me = $self->{CGI}->param("forget_me") || 0;
                    if ($forget_me) {
                            $self->{DB_PASSWORD_COOKIE}->delete_cookie();
                    }

                $ugsui->do_logout();
                # Cliff edit remove id from logged_in
                $session->remove_session($session->login());
                # delete the session of the user
                delete $self->{SESSIONS}{$session->id()};
                    if ($self->{CACHE_TO_FILE}) {
                            my $session_data_path = 
                                XYZ_DIR
                        ."/code/cache/session_data"
                        .$session->id();
                    unlink($session_data_path);
                    }
                    }
        case "login"        {
                # if extra_param holds "command*XXX" the XXX will be placed instead of
                # the command. extra_param holds pairs that are astrics-separated
                my $extra_param = $cgi->param("extra_param");
                $extra_param = "" if (!defined($extra_param));
                $extra_param =~ /command\*([^\*]+)/i;
                my $other_command = defined($1) ? $1 : "";
                if ($other_command =~ /^dbot/i) 
                    { # meanwhile - works only on dbot 
                                       # commands
                    $command = $other_command;
                        # now we will get the other parameters from the extra_param 
                        # (actually including the command that is still in the 
                        # $extra_param)         
                        while ($extra_param =~ /^\*?([^\*]+)\*([^\*]+)(.*)/) {
                        $extra_param = $3;
                        my $name = $1;
                        my $value = $2;     
                        $cgi->param(-name => $name,
                             -value => $value);
                            }#end while
                    }#end if
                else {$self->show_main_frames();}
                }
    else            {$self->show_main_frames();}
} # end switch

1
你为什么使用这么古老的 Perl 版本?目前最新版是5.16.2,因此你错过了 5.10、5.12、5.14 和 5.16 系列中的改进和漏洞修复。Perl 5.8.5 是在2004年7月发布的。那已经很久以前了。 - Jonathan Leffler
我知道,但是无能为力。我个人使用5.12版本,但在公司中我必须使用5.8.5版本。顺便说一下,在下一个实现(我们产品的新版本)中,我们将使用5.12版本。 - Chankey Pathak
1
@JonathanLeffler 这是由RHEL 5提供的版本,该版本仍然被广泛使用。Red Hat实际上将支持该版本的perl直到2017年(通过付费扩展支持)。 - jordanm
2
这取决于你所说的“支持”的含义。它有很多漏洞,RH 不会修复。例如,它甚至不是该版本的最新发布版(5.8.9 是)。 - ikegami
3
RHEL 5 提供版本号为 5.8.8。你至少应该将其更新至该版本。 - Dave Cross
3个回答

8

Switch会对源代码进行解析。这可能会导致直接使用它的代码出现难以诊断的错误。Switch创建的问题不是间歇性的,因此如果您的代码有效,就没有什么可担心的。

但实际上,它并没有增加太多价值。

使用Switch:

switch ($command) {
    case /^enter$/      { $self->show_main_frames() }
    case /^XYZ_MENU/i   { $self->show_main_menu($manual, $dbot) }
    case /^DBOT/i       { $dbot->process() }
    case /^XML_DBOT/i   { $dbot->process() }
    case /^UGS/i        { $ugsui->process() }
    case "kill"         {
        my $login = $self->{COMMON_HASH}{login} || "";

没有开关:

for ($command) {
    if    (/^enter$/)      { $self->show_main_frames() }
    elsif (/^XYZ_MENU/i)   { $self->show_main_menu($manual, $dbot) }
    elsif (/^DBOT/i)       { $dbot->process() }
    elsif (/^XML_DBOT/i)   { $dbot->process() }
    elsif (/^UGS/i)        { $ugsui->process() }
    elsif ($_ eq "kill")   {
        my $login = $self->{COMMON_HASH}{login} || "";

(elsif (/^kill\z/) 也可以使用。)

1
我看到的另一种无开关 for 的变化是 /^enter$/ && do { ...; next; }; - RobEarl

7
实际上,“Switch”模块并没有为您提供任何“杀手级功能”;可以使用“elsif”语句来完成相同的操作,它是安全、稳定的,并且没有“Switch”存在的缺点。以下是我在项目中遇到的“Switch”问题(我不再使用它):
“Switch”是通过Perl过滤器制作的。这种技术有以下限制:
- 您的源代码实际上是即时重写并替换为连续的“elsif”语句。 - 一些Perl错误报告将引用错误的行;其中一些显示您源代码中没有的代码(自动生成的代码)。
不是过滤器的限制,而是模块本身的限制:
- 如果调用“use Switch”的文件(.pl或.pm)大小超过1M字节,则可能导致“神秘错误”(如文档中所述)。我可以确认这些错误与“Switch”模块无关,完全不明显,因此您在编码/文档化几周后可能会遇到难以调试的问题。
我建议使用Perl 5.10以后可用的elsifgiven..when语句。因此,如果您使用的是Perl 5.8.x,请使用elsif。
此外,您可以阅读Switch文档中的“限制”段落。

作为一个小的侧面说明:Brian本人 建议使用for而不是given - memowe
如果调用 use Switch 的文件大小超过 1MB,那么你遇到的问题与 use Switch 无关 :-)(除此之外,我同意。) - Tanktalus

4

由于Switch没有自己的源代码解析,因此在某些情况下它根本无法工作。例如,无法与mod_perl一起使用。

但是,如果您有Perl 5.10或更高版本,则有一个更好的替代方案,具有实际上相同的功能:given/when

use v5.10;

given ($var) {
when (/^abc/) { $abc = 1 }
when (/^def/) { $def = 1 }
when (/^xyz/) { $xyz = 1 }
default       { $nothing = 1 }
}

given被Perl核心支持(并且可以在包括mod_perl在内的任何地方使用)- 您只需use v5.10;即可轻松访问。


作为一个小的侧面说明:Brian本人 建议使用for而不是given - memowe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接