perl: change to the new, safer 3-operand form of open()

The 2-operand form was inherently unsafe.  Use the 3-operand form
instead, which guarantees that arbitrary filenames are supported.

This also means we can remove a few instances of sysopen() which was
used for exactly this reason, however, at least in theory sysopen()
isn't portable.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin 2017-04-02 19:28:13 -07:00
parent dd535a6d19
commit 841d904f88
10 changed files with 31 additions and 32 deletions

View file

@ -50,7 +50,7 @@ my($output, $directives_dat, $outfile) = @ARGV;
# so D_none == 0.
@specials = ('none', 'unknown', 'corrupt');
open(DD, "< ${directives_dat}\0")
open(DD, '<', $directives_dat)
or die "$0: cannot open: ${directives_dat}: $!\n";
while (defined($line = <DD>)) {
chomp $line;
@ -61,7 +61,7 @@ while (defined($line = <DD>)) {
close(DD);
if ($output eq 'h') {
open(H, "> ${outfile}\0")
open(H, '>', $outfile)
or die "$0: cannot create: ${outfile}: $!\n";
print H "/*\n";
@ -114,7 +114,7 @@ if ($output eq 'h') {
die if ($n & ($n-1));
open(C, "> ${outfile}\0")
open(C, '>', $outfile)
or die "$0: cannot create: ${directives_c}: $!\n";
print C "/*\n";

View file

@ -43,7 +43,7 @@ my($what, $in, $out) = @ARGV;
#
# Read pptok.dat
#
open(IN, "< $in") or die "$0: cannot open: $in\n";
open(IN, '<', $in) or die "$0: cannot open: $in\n";
while (defined($line = <IN>)) {
$line =~ s/\r?\n$//; # Remove trailing \r\n or \n
$line =~ s/^\s+//; # Remove leading whitespace
@ -86,7 +86,7 @@ foreach $ct (@cctok) {
$first_uncond = $pptok[0];
@pptok = (@cptok, @pptok);
open(OUT, "> $out") or die "$0: cannot open: $out\n";
open(OUT, '>', $out) or die "$0: cannot open: $out\n";
#
# Output pptok.h

View file

@ -55,7 +55,7 @@ my($output, $insns_dat, $regs_dat, $tokens_dat) = @ARGV;
#
# Read insns.dat
#
open(ID, "< ${insns_dat}") or die "$0: cannot open $insns_dat: $!\n";
open(ID, '<', $insns_dat) or die "$0: cannot open $insns_dat: $!\n";
while (defined($line = <ID>)) {
if ($line =~ /^([A-Z0-9_]+)(|cc)\s/) {
$insn = $1.$2;
@ -83,7 +83,7 @@ close(ID);
#
# Read regs.dat
#
open(RD, "< ${regs_dat}") or die "$0: cannot open $regs_dat: $!\n";
open(RD, '<', $regs_dat) or die "$0: cannot open $regs_dat: $!\n";
while (defined($line = <RD>)) {
if ($line =~ /^([a-z0-9_-]+)\s*\S+\s*\S+\s*[0-9]+\s*(\S*)/) {
$reg = $1;
@ -126,7 +126,7 @@ close(RD);
#
# Read tokens.dat
#
open(TD, "< ${tokens_dat}") or die "$0: cannot open $tokens_dat: $!\n";
open(TD, '<', $tokens_dat) or die "$0: cannot open $tokens_dat: $!\n";
while (defined($line = <TD>)) {
if ($line =~ /^\%\s+(.*)$/) {
$pattern = $1;

View file

@ -39,8 +39,6 @@
require 'psfonts.ph'; # The fonts we want to use
require 'pswidth.ph'; # PostScript string width
use Fcntl;
#
# PostScript configurables; these values are also available to the
# PostScript code itself
@ -190,10 +188,11 @@ for ( $i = 0 ; $i < 256 ; $i++ ) {
# a cleaner representation
#
if ( defined($input) ) {
sysopen(PARAS, $input, O_RDONLY) or
open(PARAS, '<', $input) or
die "$0: cannot open $input: $!\n";
} else {
open(PARAS, "<&STDIN") or die "$0: $!\n";
# stdin
open(PARAS, '<-') or die "$0: $!\n";
}
while ( defined($line = <PARAS>) ) {
chomp $line;
@ -1116,7 +1115,7 @@ print "sti show\n";
# and DocumentFonts in the header of the EPSF and add those to the
# global header.
if ( defined($metadata{epslogo}) &&
sysopen(EPS, $metadata{epslogo}, O_RDONLY) ) {
open(EPS, '<', $metadata{epslogo}) ) {
my @eps = ();
my ($bbllx,$bblly,$bburx,$bbury) = (undef,undef,undef,undef);
my $line;

View file

@ -57,9 +57,9 @@ foreach $arg ( @ARGV ) {
}
$fname = "../insns.dat" unless $fname = $args[0];
open (F, $fname) || die "unable to open $fname";
open (F, '<', $fname) || die "unable to open $fname";
print STDERR "Writing inslist.src...\n";
open S, ">inslist.src";
open S, '>', 'inslist.src';
$line = 0;
$insns = 0;
while (<F>) {

View file

@ -541,7 +541,7 @@ sub indexsort {
sub indexdiag {
my $iitem,$ientry,$w,$ww,$foo,$node;
open INDEXDIAG,">index.diag";
open INDEXDIAG,'>', 'index.diag';
foreach $iitem (@itags) {
$ientry = $idxmap{$iitem};
print INDEXDIAG "<$iitem> ";
@ -593,7 +593,7 @@ sub write_txt {
# Open file.
print "writing file...";
open TEXT,">nasmdoc.txt";
open TEXT,'>', 'nasmdoc.txt';
select TEXT;
# Preamble.
@ -724,7 +724,7 @@ sub write_html {
# Write contents file. Just the preamble, then a menu of links to the
# separate chapter files and the nodes therein.
print "writing contents file...";
open TEXT,">nasmdoc0.html";
open TEXT,'>', 'nasmdoc0.html';
select TEXT;
&html_preamble(0);
print "<p>This manual documents NASM, the Netwide Assembler: an assembler\n";
@ -759,7 +759,7 @@ sub write_html {
# Open a null file, to ensure output (eg random &html_jumppoints calls)
# goes _somewhere_.
print "writing chapter files...";
open TEXT,">/dev/null";
open TEXT, '>', '/dev/null';
select TEXT;
$html_lastf = '';
@ -780,7 +780,7 @@ sub write_html {
$html_lastf = $html_fnames{$chapternode};
$chapternode = $nodexrefs{$xref};
$html_nextf = $html_fnames{$tstruct_mnext{$chapternode}};
open TEXT,">$html_fnames{$chapternode}"; select TEXT; &html_preamble(1);
open(TEXT, '>', $html_fnames{$chapternode}); select TEXT; &html_preamble(1);
foreach $i (@$pname) {
$ww = &word_html($i);
$title .= $ww unless $ww eq "\001";
@ -796,7 +796,7 @@ sub write_html {
$html_lastf = $html_fnames{$chapternode};
$chapternode = $nodexrefs{$xref};
$html_nextf = $html_fnames{$tstruct_mnext{$chapternode}};
open TEXT,">$html_fnames{$chapternode}"; select TEXT; &html_preamble(1);
open(TEXT, '>', $html_fnames{$chapternode}); select TEXT; &html_preamble(1);
foreach $i (@$pname) {
$ww = &word_html($i);
$title .= $ww unless $ww eq "\001";
@ -865,7 +865,7 @@ sub write_html {
close TEXT;
print "\n writing index file...";
open TEXT,">nasmdoci.html";
open TEXT,'>', 'nasmdoci.html';
select TEXT;
&html_preamble(0);
print "<p align=center><a href=\"nasmdoc0.html\">Contents</a>\n";
@ -986,7 +986,7 @@ sub write_texi {
# Open file.
print "writing file...";
open TEXT,">nasmdoc.texi";
open TEXT,'>', 'nasmdoc.texi';
select TEXT;
# Preamble.
@ -1252,7 +1252,7 @@ sub write_hlp {
# Write the HPJ project-description file.
print "writing .hpj file...";
open HPJ,">nasmdoc.hpj";
open HPJ,'>', 'nasmdoc.hpj';
print HPJ "[OPTIONS]\ncompress=true\n";
print HPJ "title=NASM: The Netwide Assembler\noldkeyphrase=no\n\n";
print HPJ "[FILES]\nnasmdoc.rtf\n\n";
@ -1264,7 +1264,7 @@ sub write_hlp {
# Open file.
print "\n writing .rtf file...";
open TEXT,">nasmdoc.rtf";
open TEXT,'>', 'nasmdoc.rtf';
select TEXT;
# Preamble.
@ -1514,7 +1514,7 @@ sub add_item {
# by future backends, instead of putting it all in the same script.
#
sub write_dip {
open(PARAS, "> nasmdoc.dip");
open(PARAS, '>', 'nasmdoc.dip');
foreach $k (sort(keys(%metadata))) {
print PARAS 'meta :', $k, "\n";
print PARAS $metadata{$k},"\n";

View file

@ -90,7 +90,7 @@ sub charcify(@) {
#
# Generate macros.c
#
open(OUT,"> macros/macros.c\0") or die "unable to open macros.c\n";
open(OUT, '>', 'macros/macros.c') or die "unable to open macros.c\n";
print OUT "/*\n";
print OUT " * Do not edit - this file auto-generated by macros.pl from:\n";
@ -116,7 +116,7 @@ my $z;
foreach $args ( @ARGV ) {
my @file_list = glob ( $args );
foreach $fname ( @file_list ) {
open(INPUT,"< $fname\0") or die "$0: $fname: $!\n";
open(INPUT,'<', $fname) or die "$0: $fname: $!\n";
while (<INPUT>) {
$line++;
chomp;

View file

@ -67,7 +67,7 @@ $first_file = $ARGV[0];
die unless (defined($first_file));
foreach $file (@ARGV) {
open(FILE, "< $file\0") or die;
open(FILE, '<', $file) or die;
# First, read the syntax hints
%hints = %def_hints;
@ -116,7 +116,7 @@ foreach $file (@ARGV) {
# Write the file back out
if (!$first) {
open(FILE, "> $file\0") or die;
open(FILE, '>', $file) or die;
print FILE @lines;
close(FILE);
}

View file

@ -82,7 +82,7 @@ foreach $arg ( @ARGV ) {
die if (scalar(@args) != 2); # input output
($fname, $oname) = @args;
open (F, $fname) || die "unable to open $fname";
open(F, '<', $fname) || die "unable to open $fname";
%dinstables = ();
@bytecode_list = ();

View file

@ -96,7 +96,7 @@ sub process_line($) {
%regs = ();
%regvals = ();
%disclass = ();
open(REGS, "< ${file}") or die "$0: Cannot open $file\n";
open(REGS, '<', $file) or die "$0: Cannot open $file\n";
while ( defined($line = <REGS>) ) {
$nline++;