From 2d9732f009c13e1bea97aa4dc72c146d397a6288 Mon Sep 17 00:00:00 2001 From: shahramn Date: Mon, 23 Dec 2024 13:21:55 +0000 Subject: [PATCH] GRIB message validity checker: Check sections for both editions --- .../grib_accessor_class_message_is_valid.cc | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/accessor/grib_accessor_class_message_is_valid.cc b/src/accessor/grib_accessor_class_message_is_valid.cc index a937502e7..e3a38b6d3 100644 --- a/src/accessor/grib_accessor_class_message_is_valid.cc +++ b/src/accessor/grib_accessor_class_message_is_valid.cc @@ -110,14 +110,28 @@ static int check_sections(grib_handle* h) long edition = 0; err = grib_get_long_internal(h, "edition", &edition); if (err) return err; - if (edition == 2) { - // sections 3 thru to 8 should exist - // section 2 is optional - for (size_t i=3; i<9; ++i) { + const int grib1_section_nums[] = {1, 2, 4}; + const int grib2_section_nums[] = {1, 3, 4, 5, 6, 7, 8}; // section 2 is optional + if (edition == 1) { + const size_t N = sizeof(grib1_section_nums) / sizeof(grib1_section_nums[0]); + for (size_t i=0; icontext, GRIB_LOG_ERROR, "Section %zu is missing!", i); + grib_context_log(h->context, GRIB_LOG_ERROR, "GRIB%ld: Section %d is missing!", edition, sec_num); + return GRIB_INVALID_MESSAGE; + } + } + } + else if (edition == 2) { + const size_t N = sizeof(grib2_section_nums) / sizeof(grib2_section_nums[0]); + for (size_t i=0; icontext, GRIB_LOG_ERROR, "GRIB%ld: Section %d is missing!", edition, sec_num); return GRIB_INVALID_MESSAGE; } }