Skip to content

Commit

Permalink
Check fclose() succeeds for writes. Issue marbl#605.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianwalenz committed Sep 11, 2017
1 parent a7d6bbd commit 7de32e8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
54 changes: 33 additions & 21 deletions src/AS_UTL/AS_UTL_fileIO.C
Original file line number Diff line number Diff line change
Expand Up @@ -546,37 +546,38 @@ compressedFileReader::compressedFileReader(const char *filename) {
char cmd[FILENAME_MAX];
int32 len = 0;

_file = NULL;
_pipe = false;
_stdi = false;
_file = NULL;
_filename = duplicateString(filename);
_pipe = false;
_stdi = false;

cftType ft = compressedFileType(filename);
cftType ft = compressedFileType(_filename);

if ((ft != cftSTDIN) && (AS_UTL_fileExists(filename, FALSE, FALSE) == FALSE))
fprintf(stderr, "ERROR: Failed to open input file '%s': %s\n", filename, strerror(errno)), exit(1);
if ((ft != cftSTDIN) && (AS_UTL_fileExists(_filename, FALSE, FALSE) == FALSE))
fprintf(stderr, "ERROR: Failed to open input file '%s': %s\n", _filename, strerror(errno)), exit(1);

errno = 0;

switch (ft) {
case cftGZ:
snprintf(cmd, FILENAME_MAX, "gzip -dc '%s'", filename);
snprintf(cmd, FILENAME_MAX, "gzip -dc '%s'", _filename);
_file = popen(cmd, "r");
_pipe = true;
break;

case cftBZ2:
snprintf(cmd, FILENAME_MAX, "bzip2 -dc '%s'", filename);
snprintf(cmd, FILENAME_MAX, "bzip2 -dc '%s'", _filename);
_file = popen(cmd, "r");
_pipe = true;
break;

case cftXZ:
snprintf(cmd, FILENAME_MAX, "xz -dc '%s'", filename);
snprintf(cmd, FILENAME_MAX, "xz -dc '%s'", _filename);
_file = popen(cmd, "r");
_pipe = true;

if (_file == NULL) // popen() returns NULL on error. It does not reliably set errno.
fprintf(stderr, "ERROR: Failed to open input file '%s': popen() returned NULL\n", filename), exit(1);
fprintf(stderr, "ERROR: Failed to open input file '%s': popen() returned NULL\n", _filename), exit(1);

errno = 0;
break;
Expand All @@ -587,16 +588,17 @@ compressedFileReader::compressedFileReader(const char *filename) {
break;

default:
_file = fopen(filename, "r");
_file = fopen(_filename, "r");
_pipe = false;
break;
}

if (errno)
fprintf(stderr, "ERROR: Failed to open input file '%s': %s\n", filename, strerror(errno)), exit(1);
fprintf(stderr, "ERROR: Failed to open input file '%s': %s\n", _filename, strerror(errno)), exit(1);
}



compressedFileReader::~compressedFileReader() {

if (_file == NULL)
Expand All @@ -609,6 +611,8 @@ compressedFileReader::~compressedFileReader() {
pclose(_file);
else
fclose(_file);

delete [] _filename;
}


Expand All @@ -617,29 +621,30 @@ compressedFileWriter::compressedFileWriter(const char *filename, int32 level) {
char cmd[FILENAME_MAX];
int32 len = 0;

_file = NULL;
_pipe = false;
_stdi = false;
_file = NULL;
_filename = duplicateString(filename);
_pipe = false;
_stdi = false;

cftType ft = compressedFileType(filename);
cftType ft = compressedFileType(_filename);

errno = 0;

switch (ft) {
case cftGZ:
snprintf(cmd, FILENAME_MAX, "gzip -%dc > '%s'", level, filename);
snprintf(cmd, FILENAME_MAX, "gzip -%dc > '%s'", level, _filename);
_file = popen(cmd, "w");
_pipe = true;
break;

case cftBZ2:
snprintf(cmd, FILENAME_MAX, "bzip2 -%dc > '%s'", level, filename);
snprintf(cmd, FILENAME_MAX, "bzip2 -%dc > '%s'", level, _filename);
_file = popen(cmd, "w");
_pipe = true;
break;

case cftXZ:
snprintf(cmd, FILENAME_MAX, "xz -%dc > '%s'", level, filename);
snprintf(cmd, FILENAME_MAX, "xz -%dc > '%s'", level, _filename);
_file = popen(cmd, "w");
_pipe = true;
break;
Expand All @@ -650,13 +655,13 @@ compressedFileWriter::compressedFileWriter(const char *filename, int32 level) {
break;

default:
_file = fopen(filename, "w");
_file = fopen(_filename, "w");
_pipe = false;
break;
}

if (errno)
fprintf(stderr, "ERROR: Failed to open output file '%s': %s\n", filename, strerror(errno)), exit(1);
fprintf(stderr, "ERROR: Failed to open output file '%s': %s\n", _filename, strerror(errno)), exit(1);
}


Expand All @@ -668,8 +673,15 @@ compressedFileWriter::~compressedFileWriter() {
if (_stdi)
return;

errno = 0;

if (_pipe)
pclose(_file);
else
fclose(_file);

if (errno)
fprintf(stderr, "ERROR: Failed to cleanly close output file '%s': %s\n", _filename, strerror(errno)), exit(1);

delete [] _filename;
}
2 changes: 2 additions & 0 deletions src/AS_UTL/AS_UTL_fileIO.H
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public:

private:
FILE *_file;
char *_filename;
bool _pipe;
bool _stdi;
};
Expand All @@ -121,6 +122,7 @@ public:

private:
FILE *_file;
char *_filename;
bool _pipe;
bool _stdi;
};
Expand Down

0 comments on commit 7de32e8

Please sign in to comment.