Skip to content

Commit c1c4bf0

Browse files
authored
Add sanity check for read_flat_grid_file (Exawind#1655)
1 parent 3a6030f commit c1c4bf0

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

amr-wind/utilities/io_utils.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,36 @@ void read_flat_grid_file(
2424

2525
size_t nx = 0;
2626
size_t ny = 0;
27-
file >> nx;
28-
file >> ny;
29-
AMREX_ALWAYS_ASSERT(nx > 0);
30-
AMREX_ALWAYS_ASSERT(ny > 0);
27+
if (!(file >> nx)) {
28+
amrex::Abort("Failed to read grid dimension nx");
29+
}
30+
if (!(file >> ny)) {
31+
amrex::Abort("Failed to read grid dimension ny");
32+
}
33+
34+
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(nx > 0, "nx must be > 0");
35+
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ny > 0, "ny must be > 0");
36+
3137
xs.resize(nx);
3238
ys.resize(ny);
3339
zs.resize(nx * ny);
40+
3441
for (size_t n = 0; n < nx; n++) {
35-
file >> xs[n];
42+
if (!(file >> xs[n])) {
43+
amrex::Abort("Failed to read xs[" + std::to_string(n) + "]");
44+
}
3645
}
3746
for (size_t n = 0; n < ny; n++) {
38-
file >> ys[n];
47+
if (!(file >> ys[n])) {
48+
amrex::Abort("Failed to read ys[" + std::to_string(n) + "]");
49+
}
3950
}
4051
for (size_t n = 0; n < nx * ny; n++) {
41-
file >> zs[n];
52+
if (!(file >> zs[n])) {
53+
amrex::Abort("Failed to read zs[" + std::to_string(n) + "]");
54+
}
4255
}
56+
4357
file.close();
4458
}
4559
} // namespace amr_wind::ioutils

0 commit comments

Comments
 (0)