Skip to content

Commit

Permalink
std::ifstream: Defensive Patterns (ECP-WarpX#2547)
Browse files Browse the repository at this point in the history
Add failure handling if inputs in `std::ifstream`s cannot be opened
or have problems seek-ing through them.

This should catch I/O errors early.
  • Loading branch information
ax3l authored and dpgrote committed Nov 29, 2021
1 parent bcf5403 commit 96f9e85
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
6 changes: 6 additions & 0 deletions Source/Diagnostics/BTD_Plotfile_Header_Impl.cpp
Expand Up @@ -27,8 +27,11 @@ BTDPlotfileHeaderImpl::ReadHeaderData ()
amrex::Vector<char> HeaderCharPtr;
amrex::Long fileLength(0), fileLengthPadded(0);
std::ifstream iss;
iss.exceptions(std::ios_base::failbit | std::ios_base::badbit);

iss.open(m_Header_path.c_str(), std::ios::in);
if(!iss) amrex::Abort("Failed to load BTD MultiFabHeader");

iss.seekg(0, std::ios::end);
fileLength = static_cast<std::streamoff>(iss.tellg());
iss.seekg(0, std::ios::beg);
Expand Down Expand Up @@ -178,8 +181,11 @@ BTDMultiFabHeaderImpl::ReadMultiFabHeader ()
amrex::Vector<char> HeaderCharPtr;
amrex::Long fileLength(0), fileLengthPadded(0);
std::ifstream iss;
iss.exceptions(std::ios_base::failbit | std::ios_base::badbit);

iss.open(m_Header_path.c_str(), std::ios::in);
if(!iss) amrex::Abort("Failed to load BTD MultiFabHeader");

iss.seekg(0, std::ios::end);
fileLength = static_cast<std::streamoff>(iss.tellg());
iss.seekg(0, std::ios::beg);
Expand Down
2 changes: 2 additions & 0 deletions Source/Diagnostics/ReducedDiags/LoadBalanceCosts.cpp
Expand Up @@ -296,6 +296,8 @@ void LoadBalanceCosts::WriteToFile (int step) const
// open the data-containing file
std::string fileDataName = m_path + m_rd_name + "." + m_extension;
std::ifstream ifs(fileDataName, std::ifstream::in);
if(!ifs) Abort("Failed to load balance file");
ifs.exceptions(std::ios_base::badbit); // | std::ios_base::failbit

// Fill in the tmp costs file with data, padded with NaNs
for (std::string lineIn; std::getline(ifs, lineIn);)
Expand Down
3 changes: 3 additions & 0 deletions Source/Laser/LaserProfilesImpl/LaserProfileFromTXYEFile.cpp
Expand Up @@ -144,6 +144,7 @@ WarpXLaserProfiles::FromTXYEFileLaserProfile::parse_txye_file(std::string txye_f
if(ParallelDescriptor::IOProcessor()){
std::ifstream inp(txye_file_name, std::ios::binary);
if(!inp) Abort("Failed to open txye file");
inp.exceptions(std::ios_base::failbit | std::ios_base::badbit);

//Uniform grid flag
char flag;
Expand Down Expand Up @@ -291,6 +292,8 @@ WarpXLaserProfiles::FromTXYEFileLaserProfile::read_data_t_chuck(int t_begin, int
//Read data chunk
std::ifstream inp(m_params.txye_file_name, std::ios::binary);
if(!inp) Abort("Failed to open txye file");
inp.exceptions(std::ios_base::failbit | std::ios_base::badbit);

auto skip_amount = 1 +
3*sizeof(uint32_t) +
m_params.t_coords.size()*sizeof(double) +
Expand Down
7 changes: 4 additions & 3 deletions Source/Particles/Collision/MCCProcess.cpp
Expand Up @@ -97,14 +97,15 @@ MCCProcess::readCrossSectionFile (
amrex::Gpu::HostVector<amrex::Real>& sigmas )
{
std::ifstream infile(cross_section_file);
if(!infile.is_open()) amrex::Abort("Failed to open cross-section data file");

double energy, sigma;
while (infile >> energy >> sigma) {
energies.push_back(energy);
sigmas.push_back(sigma);
}
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(energies.size() > 1,
"Failed to read cross-section data from file."
);
if (infile.bad()) amrex::Abort("Failed to read cross-section data from file.");
infile.close();
}

void
Expand Down

0 comments on commit 96f9e85

Please sign in to comment.