提交 7b8cdd9c 编写于 作者: D Daniel Gustafsson

Replace tmpnam() based filename generation with race free version

The tempfile() interface in File::Temp is race free and has been
available as a core module since Perl 5.6.1 (released in April
2001) so replace to simplify the code and avoid excessive looping
around a solved problem.
上级 f0d5e314
......@@ -20,6 +20,7 @@ package atmsort;
use strict;
use warnings;
use File::Spec;
use File::Temp qw/ tempfile /;
# Load explain.pm from the same directory where atmsort.pm is.
use FindBin;
......@@ -1627,19 +1628,21 @@ L_push_outarr:
} # end bigloop
# The arguments are input and output file names
# The arguments is the input filename. The output filename is returned as it
# is generated in this function to avoid races around the temporary filename
# creation.
sub run
{
my $infname = shift;
my $outfname = shift;
open my $infh, '<', $infname or die "could not open $infname: $!";
open my $outfh, '>', $outfname or die "could not open $outfname: $!";
my ($outfh, $outfname) = tempfile();
run_fhs($infh, $outfh);
close $infh;
close $outfh;
return $outfname;
}
# The arguments are input and output file handles
......
......@@ -150,6 +150,8 @@ sub gpdiff_files
my ($f1, $f2, $d2d) = @_;
my $need_equiv = 0;
my @tmpfils;
my $newf1;
my $newf2;
# need gnu diff on solaris
if ($Config{'osname'} =~ m/solaris|sunos/i)
......@@ -157,40 +159,22 @@ sub gpdiff_files
$ATMDIFF = "gdiff";
}
for my $ii (1..2)
{
my $tmpnam;
for (;;)
{
my $tmpfh;
$tmpnam = tmpnam();
sysopen($tmpfh, $tmpnam, O_RDWR | O_CREAT | O_EXCL) && last;
}
push @tmpfils, $tmpnam;
}
my $newf1 = shift @tmpfils;
my $newf2 = shift @tmpfils;
# print $glob_atmsort_args, "\n";
if (defined($d2d) && exists($d2d->{equiv}))
{
# assume f1 and f2 are the same...
atmsort::atmsort_init(%glob_atmsort_args, DO_EQUIV => 'compare');
atmsort::run($f1, $newf1);
$newf1 = atmsort::run($f1);
atmsort::atmsort_init(%glob_atmsort_args, DO_EQUIV => 'make');
atmsort::run($f2, $newf2);
$newf2 = atmsort::run($f2);
}
else
{
atmsort::atmsort_init(%glob_atmsort_args);
atmsort::run($f1, $newf1);
atmsort::run($f2, $newf2);
$newf1 = atmsort::run($f1);
$newf2 = atmsort::run($f2);
}
my $args = join(" ", @ARGV, $newf1, $newf2);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册