From e87675b1aeb9a8b065bb7d0ffd4bd994af14a937 Mon Sep 17 00:00:00 2001 From: shahramn Date: Fri, 15 Mar 2024 13:16:42 +0000 Subject: [PATCH] Const correctness and cppcheck warnings --- src/grib_accessor_class_bufr_data_array.cc | 12 +++--- ...rib_accessor_class_bufr_extract_subsets.cc | 9 ++--- ...rib_accessor_class_bufr_simple_thinning.cc | 14 +++---- ...r_class_change_alternative_row_scanning.cc | 2 +- ...ccessor_class_change_scanning_direction.cc | 2 +- src/grib_accessor_class_closest_date.cc | 5 ++- src/grib_accessor_class_codeflag.cc | 16 ++++---- src/grib_accessor_class_data_ccsds_packing.cc | 4 +- ...rib_accessor_class_expanded_descriptors.cc | 38 +++++++++---------- 9 files changed, 50 insertions(+), 52 deletions(-) diff --git a/src/grib_accessor_class_bufr_data_array.cc b/src/grib_accessor_class_bufr_data_array.cc index c1bff6ea6..3d044835a 100644 --- a/src/grib_accessor_class_bufr_data_array.cc +++ b/src/grib_accessor_class_bufr_data_array.cc @@ -1523,10 +1523,10 @@ static int build_bitmap(grib_accessor_bufr_data_array* self, unsigned char* data int bitmapSize = 0, iDelayedReplication = 0; int i, localReference, width, bitmapEndElementsDescriptorsIndex; long ppos, n; - grib_accessor* a = (grib_accessor*)self; - grib_context* c = a->context; + grib_accessor* a = (grib_accessor*)self; + const grib_context* c = a->context; bufr_descriptor** descriptors = self->expanded->v; - long* edi = elementsDescriptorsIndex->v; + const long* edi = elementsDescriptorsIndex->v; /* int iel=grib_iarray_used_size(elementsDescriptorsIndex)-1; */ int err = 0; @@ -1652,10 +1652,10 @@ static int build_bitmap_new_data(grib_accessor_bufr_data_array* self, unsigned c int bitmapSize = 0, iDelayedReplication = 0; int i, bitmapEndElementsDescriptorsIndex; long n; - grib_accessor* a = (grib_accessor*)self; - grib_context* c = a->context; + grib_accessor* a = (grib_accessor*)self; + const grib_context* c = a->context; bufr_descriptor** descriptors = self->expanded->v; - long* edi = elementsDescriptorsIndex->v; + const long* edi = elementsDescriptorsIndex->v; switch (descriptors[iBitmapOperator]->code) { case 222000: diff --git a/src/grib_accessor_class_bufr_extract_subsets.cc b/src/grib_accessor_class_bufr_extract_subsets.cc index 599d1d7b4..567ceb61a 100644 --- a/src/grib_accessor_class_bufr_extract_subsets.cc +++ b/src/grib_accessor_class_bufr_extract_subsets.cc @@ -108,7 +108,7 @@ grib_accessor_class* grib_accessor_class_bufr_extract_subsets = &_grib_accessor_ static void get_accessors(grib_accessor* a) { grib_accessor_bufr_extract_subsets* self = (grib_accessor_bufr_extract_subsets*)a; - grib_handle* h = grib_handle_of_accessor(a); + const grib_handle* h = grib_handle_of_accessor(a); if (self->packAccessor) return; @@ -118,7 +118,7 @@ static void get_accessors(grib_accessor* a) static void init(grib_accessor* a, const long len, grib_arguments* arg) { - int n = 0; + int n = 0; grib_accessor_bufr_extract_subsets* self = (grib_accessor_bufr_extract_subsets*)a; a->length = 0; @@ -134,15 +134,14 @@ static int get_native_type(grib_accessor* a) static int pack_long(grib_accessor* a, const long* val, size_t* len) { - int err = 0; grib_accessor_bufr_extract_subsets* self = (grib_accessor_bufr_extract_subsets*)a; - size_t l = 1; + size_t l = 1; long v[1]; get_accessors(a); v[0] = 1; - err = grib_pack_long(self->packAccessor, v, &l); + int err = grib_pack_long(self->packAccessor, v, &l); if (err) { if (err == GRIB_ENCODING_ERROR) grib_context_log(a->context, GRIB_LOG_ERROR, "Could not extract subset(s).\n\tHint: Did you forget to set unpack=1?"); diff --git a/src/grib_accessor_class_bufr_simple_thinning.cc b/src/grib_accessor_class_bufr_simple_thinning.cc index b457d0e9e..cd0181fe4 100644 --- a/src/grib_accessor_class_bufr_simple_thinning.cc +++ b/src/grib_accessor_class_bufr_simple_thinning.cc @@ -133,18 +133,17 @@ static int get_native_type(grib_accessor* a) static int apply_thinning(grib_accessor* a) { - grib_accessor_bufr_simple_thinning* self = (grib_accessor_bufr_simple_thinning*)a; - int ret = 0; + const grib_accessor_bufr_simple_thinning* self = (grib_accessor_bufr_simple_thinning*)a; + long skip; grib_handle* h = grib_handle_of_accessor(a); grib_context* c = h->context; long compressed = 0, nsubsets; - long i; grib_iarray* subsets; long* subsets_ar = 0; long start = 0, radius = 0; - ret = grib_get_long(h, "compressedData", &compressed); + int ret = grib_get_long(h, "compressedData", &compressed); if (ret) return ret; if (compressed) { @@ -168,7 +167,7 @@ static int apply_thinning(grib_accessor* a) return ret; subsets = grib_iarray_new(c, numberOfSubsets / skip + 1, 10); - for (i = 0; i < numberOfSubsets; i += skip + 1) { + for (long i = 0; i < numberOfSubsets; i += skip + 1) { grib_iarray_push(subsets, i + 1); } @@ -200,12 +199,11 @@ static int apply_thinning(grib_accessor* a) static int pack_long(grib_accessor* a, const long* val, size_t* len) { - int err = 0; - grib_accessor_bufr_simple_thinning* self = (grib_accessor_bufr_simple_thinning*)a; + const grib_accessor_bufr_simple_thinning* self = (grib_accessor_bufr_simple_thinning*)a; if (*len == 0) return GRIB_SUCCESS; - err = apply_thinning(a); + int err = apply_thinning(a); if (err) return err; diff --git a/src/grib_accessor_class_change_alternative_row_scanning.cc b/src/grib_accessor_class_change_alternative_row_scanning.cc index 394bb8409..d8eef0eb4 100644 --- a/src/grib_accessor_class_change_alternative_row_scanning.cc +++ b/src/grib_accessor_class_change_alternative_row_scanning.cc @@ -125,7 +125,7 @@ static int pack_long(grib_accessor* a, const long* val, size_t* len) { int err = 0; grib_accessor_change_alternative_row_scanning* self = (grib_accessor_change_alternative_row_scanning*)a; - grib_context* c = a->context; + const grib_context* c = a->context; grib_handle* h = grib_handle_of_accessor(a); long i, j, jr, theEnd, Ni, Nj, k, kp, alternativeRowScanning; size_t size = 0; diff --git a/src/grib_accessor_class_change_scanning_direction.cc b/src/grib_accessor_class_change_scanning_direction.cc index 88fbc35fb..f951c9083 100644 --- a/src/grib_accessor_class_change_scanning_direction.cc +++ b/src/grib_accessor_class_change_scanning_direction.cc @@ -146,7 +146,7 @@ static int pack_long(grib_accessor* a, const long* val, size_t* len) size_t size = 0; double* values = NULL; grib_accessor_change_scanning_direction* self = (grib_accessor_change_scanning_direction*)a; - grib_context* c = a->context; + const grib_context* c = a->context; grib_handle* h = grib_handle_of_accessor(a); const char* cclass_name = a->cclass->name; diff --git a/src/grib_accessor_class_closest_date.cc b/src/grib_accessor_class_closest_date.cc index b4729822f..08ba9f333 100644 --- a/src/grib_accessor_class_closest_date.cc +++ b/src/grib_accessor_class_closest_date.cc @@ -159,6 +159,8 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len) /* Sets val to the 'index' of the closes date */ static int unpack_double(grib_accessor* a, double* val, size_t* len) { + const grib_accessor_closest_date* self = (grib_accessor_closest_date*)a; + int err = 0; long num_forecasts = 0; /* numberOfForecastsUsedInLocalTime */ /* These relate to the date and time in Section 1 */ @@ -171,9 +173,8 @@ static int unpack_double(grib_accessor* a, double* val, size_t* len) /* These relate to the forecast dates and times in Section 4 */ long *yearArray, *monthArray, *dayArray, *hourArray, *minuteArray, *secondArray; - grib_accessor_closest_date* self = (grib_accessor_closest_date*)a; grib_handle* h = grib_handle_of_accessor(a); - grib_context* c = a->context; + const grib_context* c = a->context; *val = -1; /* initialise to an invalid index */ if ((err = grib_get_long_internal(h, self->numForecasts, &num_forecasts)) != GRIB_SUCCESS) return err; diff --git a/src/grib_accessor_class_codeflag.cc b/src/grib_accessor_class_codeflag.cc index 77789389d..ab86a64b1 100644 --- a/src/grib_accessor_class_codeflag.cc +++ b/src/grib_accessor_class_codeflag.cc @@ -110,8 +110,8 @@ grib_accessor_class* grib_accessor_class_codeflag = &_grib_accessor_class_codefl static void init(grib_accessor* a, const long len, grib_arguments* param) { grib_accessor_codeflag* self = (grib_accessor_codeflag*)a; - a->length = len; - self->tablename = grib_arguments_get_string(grib_handle_of_accessor(a), param, 0); + a->length = len; + self->tablename = grib_arguments_get_string(grib_handle_of_accessor(a), param, 0); Assert(a->length >= 0); } @@ -123,8 +123,8 @@ static int test_bit(long a, long b) static int grib_get_codeflag(grib_accessor* a, long code, char* codename) { - grib_accessor_codeflag* self = (grib_accessor_codeflag*)a; - FILE* f = NULL; + const grib_accessor_codeflag* self = (grib_accessor_codeflag*)a; + FILE* f = NULL; char fname[1024]; char bval[50]; char num[50]; @@ -200,10 +200,10 @@ static int value_count(grib_accessor* a, long* count) static void dump(grib_accessor* a, grib_dumper* dumper) { - grib_accessor_codeflag* self = (grib_accessor_codeflag*)a; - long v; - char flagname[1024]; - char fname[1024]; + const grib_accessor_codeflag* self = (grib_accessor_codeflag*)a; + long v = 0; + char flagname[1024] = {0,}; + char fname[1024] = {0,}; size_t llen = 1; diff --git a/src/grib_accessor_class_data_ccsds_packing.cc b/src/grib_accessor_class_data_ccsds_packing.cc index 28e961e86..ccfd48c03 100644 --- a/src/grib_accessor_class_data_ccsds_packing.cc +++ b/src/grib_accessor_class_data_ccsds_packing.cc @@ -650,7 +650,7 @@ static int unpack_float(grib_accessor* a, float* val, size_t* len) static int unpack_double_element(grib_accessor* a, size_t idx, double* val) { // The index idx relates to codedValues NOT values! - grib_accessor_data_ccsds_packing* self = (grib_accessor_data_ccsds_packing*)a; + const grib_accessor_data_ccsds_packing* self = (grib_accessor_data_ccsds_packing*)a; grib_handle* hand = grib_handle_of_accessor(a); int err = 0; size_t size = 0; @@ -686,7 +686,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) { - grib_accessor_data_ccsds_packing* self = (grib_accessor_data_ccsds_packing*)a; + const grib_accessor_data_ccsds_packing* self = (grib_accessor_data_ccsds_packing*)a; grib_handle* hand = grib_handle_of_accessor(a); size_t size = 0, i = 0; double* values = NULL; diff --git a/src/grib_accessor_class_expanded_descriptors.cc b/src/grib_accessor_class_expanded_descriptors.cc index b4cb0ece7..d57454996 100644 --- a/src/grib_accessor_class_expanded_descriptors.cc +++ b/src/grib_accessor_class_expanded_descriptors.cc @@ -161,11 +161,11 @@ typedef struct change_coding_params static void init(grib_accessor* a, const long len, grib_arguments* args) { grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; - int n = 0; - grib_handle* hand = grib_handle_of_accessor(a); - self->tablesAccessorName = grib_arguments_get_name(hand, args, n++); - self->expandedName = grib_arguments_get_name(hand, args, n++); - self->rank = grib_arguments_get_long(hand, args, n++); + int n = 0; + grib_handle* hand = grib_handle_of_accessor(a); + self->tablesAccessorName = grib_arguments_get_name(hand, args, n++); + self->expandedName = grib_arguments_get_name(hand, args, n++); + self->rank = grib_arguments_get_long(hand, args, n++); if (self->rank != 0) { self->expandedAccessor = grib_find_accessor(hand, self->expandedName); } @@ -231,7 +231,7 @@ static void __expand(grib_accessor* a, bufr_descriptors_array* unexpanded, bufr_ if (BUFR_DESCRIPTORS_ARRAY_USED_SIZE(unexpanded) == 0) return; - us = grib_bufr_descriptor_clone(grib_bufr_descriptors_array_get(unexpanded, 0)); + us = grib_bufr_descriptor_clone(grib_bufr_descriptors_array_get(unexpanded, 0)); us->context = c; *err = 0; @@ -262,7 +262,7 @@ static void __expand(grib_accessor* a, bufr_descriptors_array* unexpanded, bufr_ inner_unexpanded = grib_bufr_descriptors_array_new(c, DESC_SIZE_INIT, DESC_SIZE_INCR); for (i = 0; i < size; i++) { - vv = grib_bufr_descriptor_new(self->tablesAccessor, v_array[i], !SILENT, err); + vv = grib_bufr_descriptor_new(self->tablesAccessor, v_array[i], !SILENT, err); inner_unexpanded = grib_bufr_descriptors_array_push(inner_unexpanded, vv); } grib_context_free(c, v_array); @@ -707,27 +707,27 @@ static int expand(grib_accessor* a) int grib_accessor_class_expanded_descriptors_set_do_expand(grib_accessor* a, long do_expand) { grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; - self->do_expand = do_expand; + self->do_expand = do_expand; return 0; } bufr_descriptors_array* grib_accessor_class_expanded_descriptors_get_expanded(grib_accessor* a, int* err) { - grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; - *err = expand(a); + const grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; + *err = expand(a); return self->expanded; } static int unpack_double(grib_accessor* a, double* val, size_t* len) { grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; - int ret = 0; - int i; + int ret = 0; + size_t i = 0; size_t expandedSize; if (self->rank != 2) { long* lval = (long*)grib_context_malloc_clear(a->context, *len * sizeof(long)); - ret = unpack_long(a, lval, len); + ret = unpack_long(a, lval, len); if (ret) return ret; for (i = 0; i < *len; i++) @@ -756,9 +756,9 @@ static int unpack_double(grib_accessor* a, double* val, size_t* len) static int unpack_long(grib_accessor* a, long* val, size_t* len) { grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; - int ret = 0; - size_t rlen = 0; - size_t i; + int ret = 0; + size_t rlen = 0; + size_t i = 0; ret = expand(a); if (ret) @@ -830,15 +830,15 @@ static int unpack_string_array(grib_accessor* a, char** buffer, size_t* len) static int pack_long(grib_accessor* a, const long* val, size_t* len) { grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; - self->do_expand = 1; + self->do_expand = 1; return GRIB_NOT_IMPLEMENTED; } static int value_count(grib_accessor* a, long* rlen) { grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; - int err = 0; - *rlen = 0; + int err = 0; + *rlen = 0; err = expand(a); if (err) {