diff --git a/src/grib_accessor_class_bit.cc b/src/grib_accessor_class_bit.cc index e3099d259..ab81eecda 100644 --- a/src/grib_accessor_class_bit.cc +++ b/src/grib_accessor_class_bit.cc @@ -106,22 +106,20 @@ grib_accessor_class* grib_accessor_class_bit = &_grib_accessor_class_bit; static void init(grib_accessor* a, const long len, grib_arguments* arg) { grib_accessor_bit* self = (grib_accessor_bit*)a; - a->length = 0; - self->owner = grib_arguments_get_name(grib_handle_of_accessor(a), arg, 0); - self->bit_index = grib_arguments_get_long(grib_handle_of_accessor(a), arg, 1); + a->length = 0; + self->owner = grib_arguments_get_name(grib_handle_of_accessor(a), arg, 0); + self->bit_index = grib_arguments_get_long(grib_handle_of_accessor(a), arg, 1); } - static int unpack_long(grib_accessor* a, long* val, size_t* len) { grib_accessor_bit* self = (grib_accessor_bit*)a; - int ret = 0; - + int ret = 0; long data = 0; if (*len < 1) { - grib_context_log(a->context, GRIB_LOG_ERROR, "grib_accessor_bit : unpack_long : Wrong size for %s it contains %d values ", a->name, 1); - *len = 0; + grib_context_log(a->context, GRIB_LOG_ERROR, "grib_accessor_bit: unpack_long: Wrong size for %s, it contains %d values ", a->name, 1); + *len = 1; return GRIB_ARRAY_TOO_SMALL; } @@ -142,23 +140,22 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len) static int pack_long(grib_accessor* a, const long* val, size_t* len) { grib_accessor_bit* self = (grib_accessor_bit*)a; - grib_accessor* owner = NULL; - unsigned char* mdata = 0; + if (*len < 1) { - grib_context_log(a->context, GRIB_LOG_ERROR, "grib_accessor_bit : pack_long : At least one value to pack for %s", a->name); - *len = 0; + grib_context_log(a->context, GRIB_LOG_ERROR, "grib_accessor_bit: pack_long: At least one value to pack for %s", a->name); + *len = 1; return GRIB_ARRAY_TOO_SMALL; } - owner = grib_find_accessor(grib_handle_of_accessor(a), self->owner); - + grib_accessor* owner = grib_find_accessor(grib_handle_of_accessor(a), self->owner); if (!owner) { - grib_context_log(a->context, GRIB_LOG_ERROR, "grib_accessor_bit : Cannot get the owner %s for computing the bit value of %s ", self->owner, a->name); + grib_context_log(a->context, GRIB_LOG_ERROR, "grib_accessor_bit: Cannot get the owner %s for computing the bit value of %s", + self->owner, a->name); *len = 0; return GRIB_NOT_FOUND; } - mdata = grib_handle_of_accessor(a)->buffer->data; + unsigned char* mdata = grib_handle_of_accessor(a)->buffer->data; mdata += grib_byte_offset(owner); /* Note: In the definitions, flagbit numbers go from 7 to 0 (the bit_index), while WMO convention is from 1 to 8 */ diff --git a/src/grib_accessor_class_bitmap.cc b/src/grib_accessor_class_bitmap.cc index bd52d16cd..b86b34332 100644 --- a/src/grib_accessor_class_bitmap.cc +++ b/src/grib_accessor_class_bitmap.cc @@ -161,8 +161,8 @@ static void compute_size(grib_accessor* a) static void init(grib_accessor* a, const long len, grib_arguments* arg) { grib_accessor_bitmap* self = (grib_accessor_bitmap*)a; - grib_handle* hand = grib_handle_of_accessor(a); - int n = 0; + grib_handle* hand = grib_handle_of_accessor(a); + int n = 0; self->tableReference = grib_arguments_get_name(hand, arg, n++); self->missing_value = grib_arguments_get_name(hand, arg, n++); @@ -190,23 +190,21 @@ static void dump(grib_accessor* a, grib_dumper* dumper) static int unpack_long(grib_accessor* a, long* val, size_t* len) { - long pos = a->offset * 8; - long tlen = 0; - long i = 0; - int err = 0; + long pos = a->offset * 8; + long tlen = 0; const grib_handle* hand = grib_handle_of_accessor(a); - err = grib_value_count(a, &tlen); + int err = grib_value_count(a, &tlen); if (err) return err; if (*len < tlen) { grib_context_log(a->context, GRIB_LOG_ERROR, "Wrong size for %s, it contains %ld values", a->name, tlen); - *len = 0; + *len = tlen; return GRIB_ARRAY_TOO_SMALL; } - for (i = 0; i < tlen; i++) { + for (long i = 0; i < tlen; i++) { val[i] = (long)grib_decode_unsigned_long(hand->buffer->data, &pos, 1); } *len = tlen; @@ -219,21 +217,19 @@ static int unpack(grib_accessor* a, T* val, size_t* len) static_assert(std::is_floating_point::value, "Requires floating points numbers"); long pos = a->offset * 8; long tlen; - long i; - int err = 0; grib_handle* hand = grib_handle_of_accessor(a); - err = grib_value_count(a, &tlen); + int err = grib_value_count(a, &tlen); if (err) return err; if (*len < tlen) { grib_context_log(a->context, GRIB_LOG_ERROR, "Wrong size for %s, it contains %ld values", a->name, tlen); - *len = 0; + *len = tlen; return GRIB_ARRAY_TOO_SMALL; } - for (i = 0; i < tlen; i++) { + for (long i = 0; i < tlen; i++) { val[i] = (T)grib_decode_unsigned_long(hand->buffer->data, &pos, 1); } *len = tlen; @@ -261,8 +257,7 @@ static int unpack_double_element(grib_accessor* a, size_t idx, double* val) } static int unpack_double_element_set(grib_accessor* a, const size_t* index_array, size_t len, double* val_array) { - size_t i = 0; - for (i=0; ilength; @@ -293,7 +287,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len) return GRIB_BUFFER_TOO_SMALL; } - for (i = 0; i < a->length; i++) { + for (long i = 0; i < a->length; i++) { val[i] = hand->buffer->data[a->offset + i]; } diff --git a/tests/grib_unpack_subarray.sh b/tests/grib_unpack_subarray.sh index 43bc730d8..3ac3b2530 100755 --- a/tests/grib_unpack_subarray.sh +++ b/tests/grib_unpack_subarray.sh @@ -13,6 +13,9 @@ label="grib_unpack_subarray_test" temp=${label}".grib.tmp" +infile=$data_dir/constant_field.grib2 +$EXEC ${test_dir}/grib_unpack_subarray $infile + infile=$data_dir/sample.grib2 $EXEC ${test_dir}/grib_unpack_subarray $infile