Skip to content

Commit

Permalink
MIR-682 always throw on sum(pl) != values.size(), earlier assert
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaciel committed Dec 13, 2024
1 parent f61dcab commit 1fbfdd6
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/mir/input/GribInput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ class ConditionNOT : public Condition {
*/


void wrongly_encoded_grib(const std::string& msg) {
void wrongly_encoded_grib(const std::string& msg, bool forceThrow = false) {
static bool abortIfWronglyEncodedGRIB = eckit::Resource<bool>("$MIR_ABORT_IF_WRONGLY_ENCODED_GRIB", false);

if (abortIfWronglyEncodedGRIB) {
if (abortIfWronglyEncodedGRIB || forceThrow) {
Log::error() << msg << std::endl;
throw exception::UserError(msg);
}
Expand Down Expand Up @@ -733,6 +733,13 @@ data::MIRField GribInput::field() const {
GRIB_CALL(codes_get_long_array(grib_, "pl", pl.data(), &size));
ASSERT(count_pl == size);

if (auto pl_sum = static_cast<size_t>(std::accumulate(pl.begin(), pl.end(), 0L)); pl_sum != values.size()) {
wrongly_encoded_grib("GribInput: sum of pl array (" + std::to_string(pl_sum) +
") does not match the size of values array (" + std::to_string(values.size()) +
")",
true);
}

// NOTE: this fix ties with the method get(const std::string &name, std::vector<long> &value)
if (std::find(pl.rbegin(), pl.rend(), 0) != pl.rend()) {

Expand Down Expand Up @@ -760,11 +767,11 @@ data::MIRField GribInput::field() const {
for (auto p1 = pl.begin(), p2 = pl_fixed.begin(); p1 != pl.end(); ++p1, ++p2) {
if (*p1 == 0) {
ASSERT(*p2 > 0);
auto Ni = size_t(*p2);
auto Ni = static_cast<size_t>(*p2);
values_extended.insert(values_extended.end(), Ni, missingValue);
}
else {
auto Ni = size_t(*p1);
auto Ni = static_cast<size_t>(*p1);
ASSERT(i + Ni <= count);

values_extended.insert(values_extended.end(), &values[i], &values[i + Ni]);
Expand All @@ -777,7 +784,7 @@ data::MIRField GribInput::field() const {
values.swap(values_extended);

ASSERT(get("pl", pl));
size_t pl_sum = size_t(std::accumulate(pl.begin(), pl.end(), 0L));
auto pl_sum = static_cast<size_t>(std::accumulate(pl.begin(), pl.end(), 0L));
ASSERT(pl_sum == values.size());
}
}
Expand Down

0 comments on commit 1fbfdd6

Please sign in to comment.