Skip to content

Commit

Permalink
BUG: do not crash in CanReadFile if the path does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
dzenanz committed Mar 31, 2021
1 parent b7dea3c commit 7569472
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/itkScancoImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -230,25 +230,32 @@ ScancoImageIO::EncodeDate(void * target)
bool
ScancoImageIO::CanReadFile(const char * filename)
{
std::ifstream infile;
this->OpenFileForReading(infile, filename);

bool canRead = false;
if (infile.good())
try
{
// header is a 512 byte block
char buffer[512];
infile.read(buffer, 512);
if (!infile.bad())
std::ifstream infile;
this->OpenFileForReading(infile, filename);

bool canRead = false;
if (infile.good())
{
int fileType = ScancoImageIO::CheckVersion(buffer);
canRead = (fileType > 0);
// header is a 512 byte block
char buffer[512];
infile.read(buffer, 512);
if (!infile.bad())
{
int fileType = ScancoImageIO::CheckVersion(buffer);
canRead = (fileType > 0);
}
}
}

infile.close();
infile.close();

return canRead;
return canRead;
}
catch (...) // file cannot be opened, access denied etc.
{
return false;
}
}


Expand Down

0 comments on commit 7569472

Please sign in to comment.