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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -96,7 +96,7 @@ sub process_line($) {
%regs = (); %regs = ();
%regvals = (); %regvals = ();
%disclass = (); %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>) ) { while ( defined($line = <REGS>) ) {
$nline++; $nline++;