From d7bf7f9fa5c3af7e0749f801773ed579d0807c75 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Mon, 28 Aug 2023 13:40:32 +0100 Subject: [PATCH] Replace Asserts with proper error messages and exit codes --- definitions/grib1/3.82.table | 2 +- fortran/eccodes_f90_int.f90 | 2 +- fortran/eccodes_f90_long_int.f90 | 2 +- fortran/eccodes_f90_tail.f90 | 8 ++-- fortran/grib_f90_int.f90 | 2 +- fortran/grib_f90_long_int.f90 | 2 +- fortran/grib_f90_tail.f90 | 8 ++-- ...rib_accessor_class_data_complex_packing.cc | 29 +++++++-------- src/grib_accessor_class_data_sh_packed.cc | 37 +++++++------------ 9 files changed, 40 insertions(+), 52 deletions(-) diff --git a/definitions/grib1/3.82.table b/definitions/grib1/3.82.table index b50e3bfef..29e0e2320 100644 --- a/definitions/grib1/3.82.table +++ b/definitions/grib1/3.82.table @@ -4,7 +4,7 @@ ## created: 6 Oct 2011 ## modified: 13 May 2013 ## -# CODE TABLE 3 Fixed levels or layers for wich the data are included +# CODE TABLE 3 Fixed levels or layers for which the data are included 0 0 Reserved 1 surf Surface (of the Earth, which includes sea surface) 2 bcld Cloud base level diff --git a/fortran/eccodes_f90_int.f90 b/fortran/eccodes_f90_int.f90 index 5202aa4f7..83caa7881 100644 --- a/fortran/eccodes_f90_int.f90 +++ b/fortran/eccodes_f90_int.f90 @@ -16,7 +16,7 @@ !> \b Examples: \ref grib_index.f90 "grib_index.f90" !> !> @param indexid id of an index created from a file. The index must have been created with the key in argument. - !> @param key key for wich the values are returned + !> @param key key for which the values are returned !> @param values array of values. The array must be allocated before entering this function and its size must be enough to contain all the values. !> @param status CODES_SUCCESS if OK, integer value on error interface codes_index_get diff --git a/fortran/eccodes_f90_long_int.f90 b/fortran/eccodes_f90_long_int.f90 index 5399b54be..c124bfae9 100644 --- a/fortran/eccodes_f90_long_int.f90 +++ b/fortran/eccodes_f90_long_int.f90 @@ -16,7 +16,7 @@ !> \b Examples: \ref grib_index.f90 "grib_index.f90" !> !> @param indexid id of an index created from a file. The index must have been created with the key in argument. - !> @param key key for wich the values are returned + !> @param key key for which the values are returned !> @param values array of values. The array must be allocated before entering this function and its size must be enough to contain all the values. !> @param status CODES_SUCCESS if OK, integer value on error interface codes_index_get diff --git a/fortran/eccodes_f90_tail.f90 b/fortran/eccodes_f90_tail.f90 index 7dfdd952a..faf4e7e36 100644 --- a/fortran/eccodes_f90_tail.f90 +++ b/fortran/eccodes_f90_tail.f90 @@ -121,7 +121,7 @@ !> \b Examples: \ref grib_index.f90 "grib_index.f90" !> !> @param indexid id of an index created from a file. The index must have been created with the key in argument. - !> @param key key for wich the values are returned + !> @param key key for which the values are returned !> @param values array of values. The array must be allocated before entering this function and its size must be enough to contain all the values. !> @param status CODES_SUCCESS if OK, integer value on error subroutine codes_index_get_int(indexid, key, values, status) @@ -143,7 +143,7 @@ !> \b Examples: \ref grib_index.f90 "grib_index.f90" !> !> @param indexid id of an index created from a file. The index must have been created with the key in argument. - !> @param key key for wich the values are returned + !> @param key key for which the values are returned !> @param values array of values. The array must be allocated before entering this function and its size must be enough to contain all the values. !> @param status CODES_SUCCESS if OK, integer value on error subroutine codes_index_get_long(indexid, key, values, status) @@ -165,7 +165,7 @@ !> \b Examples: \ref grib_index.f90 "grib_index.f90" !> !> @param indexid id of an index created from a file. The index must have been created with the key in argument. - !> @param key key for wich the values are returned + !> @param key key for which the values are returned !> @param values array of values. The array must be allocated before entering this function and its size must be enough to contain all the values. !> @param status CODES_SUCCESS if OK, integer value on error subroutine codes_index_get_real8(indexid, key, values, status) @@ -189,7 +189,7 @@ !> \b Examples: \ref grib_index.f90 "grib_index.f90" !> !> @param indexid id of an index created from a file. The index must have been created with the key in argument. - !> @param key key for wich the values are returned + !> @param key key for which the values are returned !> @param values array of values. The array must be allocated before entering this function and its size must be enough to contain all the values. !> @param status CODES_SUCCESS if OK, integer value on error subroutine codes_index_get_string(indexid, key, values, status) diff --git a/fortran/grib_f90_int.f90 b/fortran/grib_f90_int.f90 index d474cfa8e..d5fdb45f3 100644 --- a/fortran/grib_f90_int.f90 +++ b/fortran/grib_f90_int.f90 @@ -16,7 +16,7 @@ !> \b Examples: \ref grib_index.f90 "grib_index.f90" !> !> @param indexid id of an index created from a file. The index must have been created with the key in argument. - !> @param key key for wich the values are returned + !> @param key key for which the values are returned !> @param values array of values. The array must be allocated before entering this function and its size must be enough to contain all the values. !> @param status GRIB_SUCCESS if OK, integer value on error interface grib_index_get diff --git a/fortran/grib_f90_long_int.f90 b/fortran/grib_f90_long_int.f90 index 9cd98a352..1d7e53afb 100644 --- a/fortran/grib_f90_long_int.f90 +++ b/fortran/grib_f90_long_int.f90 @@ -16,7 +16,7 @@ !> \b Examples: \ref grib_index.f90 "grib_index.f90" !> !> @param indexid id of an index created from a file. The index must have been created with the key in argument. - !> @param key key for wich the values are returned + !> @param key key for which the values are returned !> @param values array of values. The array must be allocated before entering this function and its size must be enough to contain all the values. !> @param status GRIB_SUCCESS if OK, integer value on error interface grib_index_get diff --git a/fortran/grib_f90_tail.f90 b/fortran/grib_f90_tail.f90 index 0f8b19103..600e621a5 100644 --- a/fortran/grib_f90_tail.f90 +++ b/fortran/grib_f90_tail.f90 @@ -154,7 +154,7 @@ !> \b Examples: \ref grib_index.f90 "grib_index.f90" !> !> @param indexid ID of an index created from a file. The index must have been created with the key in argument. - !> @param key key for wich the values are returned + !> @param key key for which the values are returned !> @param values array of values. The array must be allocated before entering this function and its size must be enough to contain all the values. !> @param status GRIB_SUCCESS if OK, integer value on error subroutine grib_index_get_int(indexid, key, values, status) @@ -184,7 +184,7 @@ !> \b Examples: \ref grib_index.f90 "grib_index.f90" !> !> @param indexid ID of an index created from a file. The index must have been created with the key in argument. - !> @param key key for wich the values are returned + !> @param key key for which the values are returned !> @param values array of values. The array must be allocated before entering this function and its size must be enough to contain all the values. !> @param status GRIB_SUCCESS if OK, integer value on error subroutine grib_index_get_long(indexid, key, values, status) @@ -214,7 +214,7 @@ !> \b Examples: \ref grib_index.f90 "grib_index.f90" !> !> @param indexid id of an index created from a file. The index must have been created with the key in argument. - !> @param key key for wich the values are returned + !> @param key key for which the values are returned !> @param values array of values. The array must be allocated before entering this function and its size must be enough to contain all the values. !> @param status GRIB_SUCCESS if OK, integer value on error subroutine grib_index_get_real8(indexid, key, values, status) @@ -246,7 +246,7 @@ !> \b Examples: \ref grib_index.f90 "grib_index.f90" !> !> @param indexid id of an index created from a file. The index must have been created with the key in argument. - !> @param key key for wich the values are returned + !> @param key key for which the values are returned !> @param values array of values. The array must be allocated before entering this function and its size must be enough to contain all the values. !> @param status GRIB_SUCCESS if OK, integer value on error subroutine grib_index_get_string(indexid, key, values, status) diff --git a/src/grib_accessor_class_data_complex_packing.cc b/src/grib_accessor_class_data_complex_packing.cc index f10640b6e..95612de5a 100644 --- a/src/grib_accessor_class_data_complex_packing.cc +++ b/src/grib_accessor_class_data_complex_packing.cc @@ -168,13 +168,11 @@ static void init(grib_accessor* a, const long v, grib_arguments* args) static int value_count(grib_accessor* a, long* count) { grib_accessor_data_complex_packing* self = (grib_accessor_data_complex_packing*)a; - int ret = 0; - grib_handle* gh = grib_handle_of_accessor(a); - + int ret = 0; + grib_handle* gh = grib_handle_of_accessor(a); long pen_j = 0; long pen_k = 0; long pen_m = 0; - *count = 0; if (a->length == 0) @@ -188,15 +186,16 @@ static int value_count(grib_accessor* a, long* count) return ret; if (pen_j != pen_k || pen_j != pen_m) { - grib_context_log(a->context, GRIB_LOG_ERROR, "pen_j=%ld, pen_k=%ld, pen_m=%ld\n", pen_j, pen_k, pen_m); - Assert((pen_j == pen_k) && (pen_j == pen_m)); + grib_context_log(a->context, GRIB_LOG_ERROR, "Invalid pentagonal resolution parameters"); + grib_context_log(a->context, GRIB_LOG_ERROR, "pen_j=%ld, pen_k=%ld, pen_m=%ld", pen_j, pen_k, pen_m); + return GRIB_DECODING_ERROR; } *count = (pen_j + 1) * (pen_j + 2); return ret; } -static double calculate_pfactor(grib_context* ctx, const double* spectralField, long fieldTruncation, long subsetTruncation) +static double calculate_pfactor(const grib_context* ctx, const double* spectralField, long fieldTruncation, long subsetTruncation) { /*long n_vals = ((fieldTruncation+1)*(fieldTruncation+2));*/ long loop, index, m, n = 0; @@ -416,10 +415,10 @@ static int pack_double(grib_accessor* a, const double* val, size_t* len) return GRIB_NOT_IMPLEMENTED; } - Assert(sub_j == sub_k); - Assert(sub_j == sub_m); - Assert(pen_j == pen_k); - Assert(pen_j == pen_m); + if (sub_j != sub_k || sub_j != sub_m || pen_j != pen_k || pen_j != pen_m) { + grib_context_log(a->context, GRIB_LOG_ERROR, "%s: Invalid pentagonal resolution parameters", cclass_name); + return GRIB_ENCODING_ERROR; + } n_vals = (pen_j + 1) * (pen_j + 2); @@ -790,10 +789,10 @@ static int unpack(grib_accessor* a, T* val, size_t* len) return GRIB_NOT_IMPLEMENTED; } - Assert(sub_j == sub_k); - Assert(sub_j == sub_m); - Assert(pen_j == pen_k); - Assert(pen_j == pen_m); + if (sub_j != sub_k || sub_j != sub_m || pen_j != pen_k || pen_j != pen_m) { + grib_context_log(a->context, GRIB_LOG_ERROR, "%s: Invalid pentagonal resolution parameters", cclass_name); + return GRIB_DECODING_ERROR; + } buf = (unsigned char*)gh->buffer->data; diff --git a/src/grib_accessor_class_data_sh_packed.cc b/src/grib_accessor_class_data_sh_packed.cc index 6944c7a0a..a02d60275 100644 --- a/src/grib_accessor_class_data_sh_packed.cc +++ b/src/grib_accessor_class_data_sh_packed.cc @@ -164,8 +164,9 @@ static void init(grib_accessor* a, const long v, grib_arguments* args) static int value_count(grib_accessor* a, long* count) { grib_accessor_data_sh_packed* self = (grib_accessor_data_sh_packed*)a; - grib_handle* hand = grib_handle_of_accessor(a); - int ret = 0; + grib_handle* hand = grib_handle_of_accessor(a); + int ret = 0; + const char* cclass_name = a->cclass->name; long sub_j = 0; long sub_k = 0; @@ -189,8 +190,9 @@ static int value_count(grib_accessor* a, long* count) return ret; if (pen_j != pen_k || pen_j != pen_m) { - grib_context_log(a->context, GRIB_LOG_ERROR, "pen_j=%ld, pen_k=%ld, pen_m=%ld\n", pen_j, pen_k, pen_m); - Assert((pen_j == pen_k) && (pen_j == pen_m)); + grib_context_log(a->context, GRIB_LOG_ERROR, "%s: pen_j=%ld, pen_k=%ld, pen_m=%ld\n", + cclass_name, pen_j, pen_k, pen_m); + return GRIB_DECODING_ERROR; } *count = (pen_j + 1) * (pen_j + 2) - (sub_j + 1) * (sub_j + 2); return ret; @@ -200,20 +202,13 @@ static int unpack_double(grib_accessor* a, double* val, size_t* len) { grib_accessor_data_sh_packed* self = (grib_accessor_data_sh_packed*)a; - size_t i = 0; - int ret = GRIB_SUCCESS; - long hcount = 0; - long lcount = 0; - long hpos = 0; - long lup = 0; - long mmax = 0; - long n_vals = 0; + size_t i = 0; + int ret = GRIB_SUCCESS; + long hcount = 0, lcount = 0, hpos = 0, lup = 0, mmax = 0, n_vals = 0; double* scals = NULL; /* double *pscals=NULL; */ - double s = 0; - double d = 0; - double laplacianOperator = 0; + double s = 0, d = 0, laplacianOperator = 0; unsigned char* buf = NULL; unsigned char* hres = NULL; unsigned char* lres = NULL; @@ -223,22 +218,16 @@ static int unpack_double(grib_accessor* a, double* val, size_t* len) long maxv = 0; long GRIBEX_sh_bug_present = 0; long ieee_floats = 0; - long offsetdata = 0; long bits_per_value = 0; double reference_value = 0; long binary_scale_factor = 0; long decimal_scale_factor = 0; - long sub_j = 0; - long sub_k = 0; - long sub_m = 0; - long pen_j = 0; - long pen_k = 0; - long pen_m = 0; + long sub_j = 0, sub_k = 0, sub_m = 0, pen_j = 0, pen_k = 0, pen_m = 0; double operat = 0; - int bytes; + int bytes = 0; int err = 0; decode_float_proc decode_float = NULL; @@ -326,7 +315,7 @@ static int unpack_double(grib_accessor* a, double* val, size_t* len) d = codes_power(-decimal_scale_factor, 10); scals = (double*)grib_context_malloc(a->context, maxv * sizeof(double)); - Assert(scals); + if(!scals) return GRIB_OUT_OF_MEMORY; scals[0] = 0; for (i = 1; i < maxv; i++) {