From 1fbfdd63bf81396aadbe9cb8edc8caece2baadc4 Mon Sep 17 00:00:00 2001 From: Pedro Maciel Date: Fri, 13 Dec 2024 08:24:18 +0000 Subject: [PATCH] MIR-682 always throw on sum(pl) != values.size(), earlier assert --- src/mir/input/GribInput.cc | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/mir/input/GribInput.cc b/src/mir/input/GribInput.cc index 6918326f5..321d7952a 100644 --- a/src/mir/input/GribInput.cc +++ b/src/mir/input/GribInput.cc @@ -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("$MIR_ABORT_IF_WRONGLY_ENCODED_GRIB", false); - if (abortIfWronglyEncodedGRIB) { + if (abortIfWronglyEncodedGRIB || forceThrow) { Log::error() << msg << std::endl; throw exception::UserError(msg); } @@ -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(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 &value) if (std::find(pl.rbegin(), pl.rend(), 0) != pl.rend()) { @@ -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(*p2); values_extended.insert(values_extended.end(), Ni, missingValue); } else { - auto Ni = size_t(*p1); + auto Ni = static_cast(*p1); ASSERT(i + Ni <= count); values_extended.insert(values_extended.end(), &values[i], &values[i + Ni]); @@ -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(std::accumulate(pl.begin(), pl.end(), 0L)); ASSERT(pl_sum == values.size()); } }