Perl's chop / chomp considered bad, use a regexp instead

Once upon a time, there was chop, which somply chopped off the last
character of $_ or a given variable, and it was used to take off the
EOL character (\n) of strings.

... but then, you had to check for the presence of such character.

So came chomp, the better chop which checks for \n before chopping it
off.  And this worked well, as long as Perl made internally sure that
all EOLs were converted to \n.

These days, though, there seems to be a mixture of perls, so lines
from files in the "wrong" environment might have \r\n as EOL, or just
\r (Mac OS, unless I'm misinformed).

So it's time we went for the more generic variant and use s|\R$||, the
better chomp which recognises all kinds of known EOLs and chops them
off.

A few chops were left alone, as they are use as surgical tools to
remove one last slash or one last comma.

NOTE: \R came with perl 5.10.0.  It means that from now on, our
scripts will fail with any older version.

Reviewed-by: Rich Salz <rsalz@openssl.org>
This commit is contained in:
Richard Levitte 2016-02-11 21:47:30 +01:00
parent c15e95a61d
commit 9ba96fbb25
18 changed files with 35 additions and 37 deletions

View file

@ -7,7 +7,7 @@ my @directory_vars = ( "dir", "certs", "crl_dir", "new_certs_dir" );
my @file_vars = ( "database", "certificate", "serial", "crlnumber", my @file_vars = ( "database", "certificate", "serial", "crlnumber",
"crl", "private_key", "RANDFILE" ); "crl", "private_key", "RANDFILE" );
while(<STDIN>) { while(<STDIN>) {
chomp; s|\R$||;
foreach my $d (@directory_vars) { foreach my $d (@directory_vars) {
if (/^(\s*\#?\s*${d}\s*=\s*)\.\/([^\s\#]*)([\s\#].*)$/) { if (/^(\s*\#?\s*${d}\s*=\s*)\.\/([^\s\#]*)([\s\#].*)$/) {
$_ = "$1sys\\\$disk:\[.$2$3"; $_ = "$1sys\\\$disk:\[.$2$3";

View file

@ -28,7 +28,7 @@ my %translations = ();
open DEMANGLER_DATA, $ARGV[0] open DEMANGLER_DATA, $ARGV[0]
or die "Couldn't open $ARGV[0]: $!\n"; or die "Couldn't open $ARGV[0]: $!\n";
while(<DEMANGLER_DATA>) { while(<DEMANGLER_DATA>) {
chomp; s|\R$||;
(my $translated, my $original) = split /\$/; (my $translated, my $original) = split /\$/;
$translations{$original} = $translated.'$'; $translations{$original} = $translated.'$';
} }

View file

@ -121,7 +121,7 @@ if ($WHAT eq '-newcert' ) {
# ask user for existing CA certificate # ask user for existing CA certificate
print "CA certificate filename (or enter to create)\n"; print "CA certificate filename (or enter to create)\n";
$FILE = <STDIN>; $FILE = <STDIN>;
chop $FILE if $FILE; $FILE = s|\R$|| if $FILE;
if ($FILE) { if ($FILE) {
copy_pemfile($FILE,"${CATOP}/private/$CAKEY", "PRIVATE"); copy_pemfile($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
copy_pemfile($FILE,"${CATOP}/$CACERT", "CERTIFICATE"); copy_pemfile($FILE,"${CATOP}/$CACERT", "CERTIFICATE");

View file

@ -5,7 +5,7 @@
while (<>) while (<>)
{ {
next unless /^node/; next unless /^node/;
chop; s|\R$||; # Better chomp
@a=split; @a=split;
$num{$a[3]}++; $num{$a[3]}++;
} }

View file

@ -257,7 +257,7 @@ foreach (@out)
} }
$out=$t; $out=$t;
} }
chop $out; chop $out; # Get rid of the last comma
print OUT "$out"; print OUT "$out";
} }
else else

View file

@ -5,7 +5,7 @@ $max_nid=0;
$o=0; $o=0;
while(<NUMIN>) while(<NUMIN>)
{ {
chop; s|\R$||;
$o++; $o++;
s/#.*$//; s/#.*$//;
next if /^\s*$/; next if /^\s*$/;
@ -28,7 +28,7 @@ $Cname="";
$o=0; $o=0;
while (<IN>) while (<IN>)
{ {
chop; s|\R$||;
$o++; $o++;
if (/^!module\s+(.*)$/) if (/^!module\s+(.*)$/)
{ {

View file

@ -13,7 +13,7 @@ open(IN, $mac_file) || die "Can't open $mac_file, $!\n";
while (<IN>) while (<IN>)
{ {
chomp; s|\R$||; # Better chomp
my ($name, $num) = /^(\S+)\s+(\S+)$/; my ($name, $num) = /^(\S+)\s+(\S+)$/;
$oid_tbl{$name} = $num; $oid_tbl{$name} = $num;
} }
@ -25,7 +25,7 @@ my $ln = 1;
while (<IN>) while (<IN>)
{ {
chomp; s|\R$||; # Better chomp
s/#.*$//; s/#.*$//;
next if (/^\S*$/); next if (/^\S*$/);
my ($xr, $p1, $p2) = /^(\S+)\s+(\S+)\s+(\S+)/; my ($xr, $p1, $p2) = /^(\S+)\s+(\S+)\s+(\S+)/;
@ -112,6 +112,6 @@ sub check_oid
my ($chk) = @_; my ($chk) = @_;
if (!exists $oid_tbl{$chk}) if (!exists $oid_tbl{$chk})
{ {
die "Can't find \"$chk\", $!\n"; die "Can't find \"$chk\"\n";
} }
} }

View file

@ -80,7 +80,7 @@ my $nasm=0;
if ($flavour eq "mingw64") { $gas=1; $elf=0; $win64=1; if ($flavour eq "mingw64") { $gas=1; $elf=0; $win64=1;
$prefix=`echo __USER_LABEL_PREFIX__ | $ENV{CC} -E -P -`; $prefix=`echo __USER_LABEL_PREFIX__ | $ENV{CC} -E -P -`;
chomp($prefix); $prefix =~ s|\R$||; # Better chomp
} }
elsif ($flavour eq "macosx") { $gas=1; $elf=0; $prefix="_"; $decor="L\$"; } elsif ($flavour eq "macosx") { $gas=1; $elf=0; $prefix="_"; $decor="L\$"; }
elsif ($flavour eq "masm") { $gas=0; $elf=0; $masm=$masmref; $win64=1; $decor="\$L\$"; } elsif ($flavour eq "masm") { $gas=0; $elf=0; $masm=$masmref; $win64=1; $decor="\$L\$"; }
@ -852,7 +852,7 @@ ___
} }
while($line=<>) { while($line=<>) {
chomp($line); $line =~ s|\R$||; # Better chomp
$line =~ s|[#!].*$||; # get rid of asm-style comments... $line =~ s|[#!].*$||; # get rid of asm-style comments...
$line =~ s|/\*.*\*/||; # ... and C-style comments... $line =~ s|/\*.*\*/||; # ... and C-style comments...

View file

@ -7,7 +7,7 @@ my $reldir = "";
my $searchterm = ""; my $searchterm = "";
my $goal = ""; my $goal = "";
while (<$minfo>) { while (<$minfo>) {
chomp; s|\R$||;
if (/^RELATIVE_DIRECTORY=(.*)$/) { if (/^RELATIVE_DIRECTORY=(.*)$/) {
$reldir=$1; $reldir=$1;

View file

@ -2,7 +2,7 @@
$/ = ""; # Eat a paragraph at once. $/ = ""; # Eat a paragraph at once.
while(<STDIN>) { while(<STDIN>) {
chop; s|\R$||;
s/\n/ /gm; s/\n/ /gm;
if (/^=head1 /) { if (/^=head1 /) {
$name = 0; $name = 0;

View file

@ -13,7 +13,7 @@ while ($ARGV[0] =~ /^([^\s=]+)\s*=\s*(.*)$/)
$s=""; $s="";
while (<>) while (<>)
{ {
chop; s|\R$||;
s/#.*//; s/#.*//;
if (/^([^\s=]+)\s*=\s*(.*)$/) if (/^([^\s=]+)\s*=\s*(.*)$/)
{ {
@ -23,10 +23,10 @@ while (<>)
{ {
if ($b =~ /\\$/) if ($b =~ /\\$/)
{ {
chop($b); $b=$`; # Keep what is before the backslash
$o.=$b." "; $o.=$b." ";
$b=<>; $b=<>;
chop($b); $b =~ s|\R$||; # Better chomp
} }
else else
{ {
@ -43,7 +43,7 @@ while (<>)
} }
} }
$pwd=`pwd`; chop($pwd); $pwd=`pwd`; $pwd =~ s|\R$||;
if ($sym{'TOP'} eq ".") if ($sym{'TOP'} eq ".")
{ {
@ -55,7 +55,7 @@ else {
@_=split(/\//,$pwd); @_=split(/\//,$pwd);
$z=$#_-$n+1; $z=$#_-$n+1;
foreach $i ($z .. $#_) { $dir.=$_[$i]."/"; } foreach $i ($z .. $#_) { $dir.=$_[$i]."/"; }
chop($dir); chop($dir); # Remove the last slash
} }
print "RELATIVE_DIRECTORY=$dir\n"; print "RELATIVE_DIRECTORY=$dir\n";

View file

@ -59,7 +59,7 @@ open my $sha1_res, '<', $fips_target.".sha1" or die "Get hash failure";
$fips_hash=<$sha1_res>; $fips_hash=<$sha1_res>;
close $sha1_res; close $sha1_res;
unlink $fips_target.".sha1"; unlink $fips_target.".sha1";
chomp $fips_hash; $fips_hash =~ s|\R$||; # Better chomp
die "Get hash failure" if $? != 0; die "Get hash failure" if $? != 0;
@ -97,8 +97,8 @@ sub check_hash
$hashfile = <IN>; $hashfile = <IN>;
close IN; close IN;
$hashval = `$sha1_exe ${fips_libdir}/$filename`; $hashval = `$sha1_exe ${fips_libdir}/$filename`;
chomp $hashfile; $hashfile =~ s|\R$||; # Better chomp
chomp $hashval; $hashval =~ s|\R$||; # Better chomp
$hashfile =~ s/^.*=\s+//; $hashfile =~ s/^.*=\s+//;
$hashval =~ s/^.*=\s+//; $hashval =~ s/^.*=\s+//;
die "Invalid hash syntax in file" if (length($hashfile) != 40); die "Invalid hash syntax in file" if (length($hashfile) != 40);

View file

@ -553,7 +553,7 @@ if ($fips)
{ {
open (IN, "util/fipslib_path.txt") || fipslib_error(); open (IN, "util/fipslib_path.txt") || fipslib_error();
$fipslibdir = <IN>; $fipslibdir = <IN>;
chomp $fipslibdir; $fipslibdir =~ s|\R$||;
close IN; close IN;
} }
fips_check_files($fipslibdir, fips_check_files($fipslibdir,
@ -1159,7 +1159,7 @@ sub do_defs
elsif ($var eq "SSLOBJ") elsif ($var eq "SSLOBJ")
{ $ret.="\$(OBJ_D)\\\$(SSL).res "; } { $ret.="\$(OBJ_D)\\\$(SSL).res "; }
} }
chomp($ret); chomp($ret); # Does this actually do something? /RL
$ret.="\n\n"; $ret.="\n\n";
return($ret); return($ret);
} }

View file

@ -459,7 +459,7 @@ sub do_defs
if($parens > 0) { if($parens > 0) {
#Inside a DEPRECATEDIN #Inside a DEPRECATEDIN
$stored_multiline .= $_; $stored_multiline .= $_;
chomp $stored_multiline; $stored_multiline =~ s|\R$||; # Better chomp
print STDERR "DEBUG: Continuing multiline DEPRECATEDIN: $stored_multiline\n" if $debug; print STDERR "DEBUG: Continuing multiline DEPRECATEDIN: $stored_multiline\n" if $debug;
$parens = count_parens($stored_multiline); $parens = count_parens($stored_multiline);
if ($parens == 0) { if ($parens == 0) {
@ -480,9 +480,7 @@ sub do_defs
} }
if (/\\$/) { if (/\\$/) {
chomp; # remove eol $line = $`; # keep what was before the backslash
chop; # remove ending backslash
$line = $_;
next; next;
} }
@ -867,7 +865,7 @@ sub do_defs
\@current_algorithms); \@current_algorithms);
} else { } else {
$stored_multiline = $_; $stored_multiline = $_;
chomp $stored_multiline; $stored_multiline =~ s|\R$||;
print STDERR "DEBUG: Found multiline DEPRECATEDIN starting with: $stored_multiline\n" if $debug; print STDERR "DEBUG: Found multiline DEPRECATEDIN starting with: $stored_multiline\n" if $debug;
next; next;
} }
@ -1365,7 +1363,7 @@ sub load_numbers
open(IN,"<$name") || die "unable to open $name:$!\n"; open(IN,"<$name") || die "unable to open $name:$!\n";
while (<IN>) { while (<IN>) {
chop; s|\R$||; # Better chomp
s/#.*$//; s/#.*$//;
next if /^\s*$/; next if /^\s*$/;
@a=split; @a=split;

View file

@ -556,7 +556,7 @@ EOF
if (open(IN,"<$cfile")) { if (open(IN,"<$cfile")) {
my $line = ""; my $line = "";
while (<IN>) { while (<IN>) {
chomp; s|\R$||; # Better chomp
$_ = $line . $_; $_ = $line . $_;
$line = ""; $line = "";
if (/{ERR_(FUNC|REASON)\(/) { if (/{ERR_(FUNC|REASON)\(/) {

View file

@ -95,7 +95,7 @@ my $s="";
while (<IN>) while (<IN>)
{ {
chop; s|\R$||;
s/#.*//; s/#.*//;
if (/^([^\s=]+)\s*=\s*(.*)$/) if (/^([^\s=]+)\s*=\s*(.*)$/)
{ {
@ -105,10 +105,10 @@ while (<IN>)
{ {
if ($b =~ /\\$/) if ($b =~ /\\$/)
{ {
chop($b); $b=$`;
$o.=$b." "; $o.=$b." ";
$b=<IN>; $b=<IN>;
chop($b); $b =~ s|\R$||;
} }
else else
{ {

View file

@ -54,7 +54,7 @@ $cversion=`$cc -V |head -1` if $cversion =~ "Error";
$cversion=`$cc --version` if $cversion eq ""; $cversion=`$cc --version` if $cversion eq "";
$cversion =~ s/Reading specs.*\n//; $cversion =~ s/Reading specs.*\n//;
$cversion =~ s/usage.*\n//; $cversion =~ s/usage.*\n//;
chomp $cversion; $cversion =~ s|\R$||;
if (open(IN,"<CHANGES")) { if (open(IN,"<CHANGES")) {
while(<IN>) { while(<IN>) {

View file

@ -54,7 +54,7 @@ sub loadfile
$header=0 if /^[dr]sa/; $header=0 if /^[dr]sa/;
if (/^type/) { $header=0; next; } if (/^type/) { $header=0; next; }
next if $header; next if $header;
chop; s|\R$||;
@a=split; @a=split;
if ($a[0] =~ /^[dr]sa$/) if ($a[0] =~ /^[dr]sa$/)
{ {