Skip to content

Commit

Permalink
Merge pull request #97 from wolfgangkarall/safe_open
Browse files Browse the repository at this point in the history
Use 3+ argument open() calls, as we're not dealing with 'known literals'
  • Loading branch information
techsneeze committed Apr 3, 2021
2 parents e83fe26 + eb45c1b commit e07bb49
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions dmarcts-report-parser.pl
Expand Up @@ -410,7 +410,7 @@ sub show_usage {
unlink($f);
}
$counts++;
} elsif (open FILE, $f) {
} elsif (open(FILE, "<", $f)) {

$filecontent = join("", <FILE>);
close FILE;
Expand Down Expand Up @@ -643,15 +643,14 @@ sub getXMLFromMessage {
open(XML, "<:gzip", $location)
or $unzip = "ungzip";
} else {
open(XML,"unzip -p " . $location . " |")
open(XML, "-|", "unzip", "-p", $location)
or $unzip = "unzip"; # Will never happen.

# Sadly unzip -p never failes, but we can check if the
# filehandle points to an empty file and pretend it did
# not open/failed.
if (eof XML) {
$unzip = "unzip";
close XML;
}
}

Expand All @@ -666,6 +665,7 @@ sub getXMLFromMessage {
} else {
warn "$scriptname: Subject: $subj\n:";
warn "$scriptname: Failed to $unzip ZIP file (temp. location: <$location>)! \n";
close XML;
}
} else {
warn "$scriptname: Subject: $subj\n:";
Expand All @@ -683,10 +683,8 @@ sub getXMLFromZip {
my $filename = $_[0];
my $mtype = mimetype($filename);

if (open FILE, $filename) {
if ($debug) {
print "Filename: $filename, MimeType: $mtype\n";
}
if ($debug) {
print "Filename: $filename, MimeType: $mtype\n";
}

my $isgzip = 0;
Expand Down Expand Up @@ -716,15 +714,14 @@ sub getXMLFromZip {
open(XML, "<:gzip", $filename)
or $unzip = "ungzip";
} else {
open(XML,"unzip -p " . $filename . " |")
open(XML, "-|", "unzip", "-p", $filename)
or $unzip = "unzip"; # Will never happen.

# Sadly unzip -p never failes, but we can check if the
# filehandle points to an empty file and pretend it did
# not open/failed.
if (eof XML) {
$unzip = "unzip";
close XML;
}
}

Expand All @@ -737,6 +734,7 @@ sub getXMLFromZip {
close XML;
} else {
warn "$scriptname: Failed to $unzip ZIP file (<$filename>)! \n";
close XML;
}
} else {
warn "$scriptname: Could not find an <$filename>! \n";
Expand Down

0 comments on commit e07bb49

Please sign in to comment.