我在2000年的一个聊天板上引用的PerlFaq中找到了这段智慧。
glob()中有漏洞吗? 由于某些操作系统上的当前实现,在标量上下文中使用glob()函数或其角括号别名时,可能会导致泄漏和/或不可预测的行为。因此,最好只在列表上下文中使用glob()。
我读到这个问题是用Perl 5.6修复的,但后来听说5.10.1仍然会出现这个问题。
有没有人对最近的问题有过经验,在哪里能找到关于这个问题的明确答案?
晚点..。最新的PerlFAQ说:
5.18: glob()中有漏洞吗? (由brian d foy提供) 从Perl 5.6.0开始,"glob“是在内部实现的,而不是依赖外部资源。因此,"glob“的内存问题在现代perls中并不是一个问题。
=====
最后:所报告的问题是由于在给出所有匹配项之后循环中使用glob造成的误用。这没什么问题。
发布于 2012-10-11 09:46:16
发布于 2016-05-21 19:24:04
我刚刚在Debian 上用Perl 5.14.2进行了测试。
标量上下文-不幸失败
sub test
{
my $dir = shift;
my $oldDir = cwd();
chdir($dir) or die("Could not chdir() : $!");
my $firstEntry = glob('*');
print "$firstEntry\n";
chdir($oldDir) or die("Could not chdir() : $!");
}
# /tmp/test1 (contains file1 and file2)
test('/tmp/test1); # Display file1 which is expected
# /tmp/test2 (contains file3 and file4)
test('/tmp/test2'); # Display file2 which is not expected列表上下文(按预期工作)
sub test
{
my $dir = shift;
my $oldDir = cwd();
chdir($dir) or die("Could not chdir() : $!");
(my $firstEntry) = glob('*');
print "$firstEntry\n";
chdir($oldDir) or die("Could not chdir() : $!");
}
# /tmp/test1 (contains file1 and file2)
test('/tmp/test1); # Display file1 which is expected
# /tmp/test2 (contains file3 and file4)
test('/tmp/test2'); # Display file3 which is expected为了在这里继续,glob buffer不会被刷新,即使我们超出了调用范围。
使用perl 5.22-1,这两种情况都可以正常工作(标量上下文)。
https://stackoverflow.com/questions/12815091
复制相似问题