From b8ae44fccf258cc020a3d2b826bc10cc0a8a2ed2 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Wed, 1 Jun 2022 16:38:24 +0100 Subject: [PATCH] ECC-1395: BUFR: Encoding should fail if attempting to encode message with an undefined element descriptor --- src/grib_accessor_class_bufr_data_array.c | 12 +++++++++--- tests/bufr_ecc-1395.sh | 14 +++++++++----- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/grib_accessor_class_bufr_data_array.c b/src/grib_accessor_class_bufr_data_array.c index 4c7def47f..4ae432171 100644 --- a/src/grib_accessor_class_bufr_data_array.c +++ b/src/grib_accessor_class_bufr_data_array.c @@ -766,7 +766,11 @@ static int descriptor_get_min_max(bufr_descriptor* bd, long width, long referenc double* minAllowed, double* maxAllowed) { /* Maximum value is allowed to be the largest number (all bits 1) which means it's MISSING */ - size_t max1 = (1ULL << width) - 1; /* Highest value for number with 'width' bits */ + const size_t max1 = (1ULL << width) - 1; /* Highest value for number with 'width' bits */ + + if (width <= 0) + return GRIB_MISSING_BUFR_ENTRY; /* ECC-1395 */ + DebugAssert(width > 0 && width < 64); *maxAllowed = (max1 + reference) * factor; @@ -804,7 +808,8 @@ static int encode_double_array(grib_context* c, grib_buffer* buff, long* pos, bu inverseFactor = grib_power(bd->scale, 10); modifiedWidth = bd->width; - descriptor_get_min_max(bd, modifiedWidth, modifiedReference, modifiedFactor, &minAllowed, &maxAllowed); + err = descriptor_get_min_max(bd, modifiedWidth, modifiedReference, modifiedFactor, &minAllowed, &maxAllowed); + if (err) return err; nvals = grib_iarray_used_size(self->iss_list); if (nvals <= 0) @@ -1006,7 +1011,8 @@ static int encode_double_value(grib_context* c, grib_buffer* buff, long* pos, bu modifiedFactor = bd->factor; modifiedWidth = bd->width; - descriptor_get_min_max(bd, modifiedWidth, modifiedReference, modifiedFactor, &minAllowed, &maxAllowed); + err = descriptor_get_min_max(bd, modifiedWidth, modifiedReference, modifiedFactor, &minAllowed, &maxAllowed); + if (err) return err; grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + modifiedWidth); if (value == GRIB_MISSING_DOUBLE) { diff --git a/tests/bufr_ecc-1395.sh b/tests/bufr_ecc-1395.sh index f21a7bb2f..194a7aca5 100755 --- a/tests/bufr_ecc-1395.sh +++ b/tests/bufr_ecc-1395.sh @@ -10,15 +10,16 @@ . ./include.ctest.sh -REDIRECT=/dev/null label="bufr_ecc-1395_test" -temp=temp.$label + tempBufr=temp.$label.bufr tempFilt=temp.$label.filt tempOut=temp.$label.out sample_bufr4=$ECCODES_SAMPLES_PATH/BUFR4.tmpl +rm -f $tempBufr + # Pick a descriptor that does not exist in version 19 cat > $tempFilt < $tempFilt < $tempOut +${tools_dir}/bufr_filter -o $tempBufr $tempFilt $sample_bufr4 > $tempOut 2>&1 status=$? set -e +[ $status -ne 0 ] -# TODO check status +[ ! -f "$tempBufr" ] + +grep -q "unable to get descriptor 025195" $tempOut -rm -f $tempFilt $tempBufr $tempOut +rm -f $tempFilt $tempOut