From a58fc7816849a7fc4fbf0225725bf66df968cedc Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Tue, 30 Jan 2024 15:57:40 +0000 Subject: [PATCH] codes_get_string error conditions --- src/grib_accessor_class_md5.cc | 10 ++++---- tests/CMakeLists.txt | 2 ++ tests/codes_get_string.cc | 44 ++++++++++++++++++++++++++++++++++ tests/codes_get_string.sh | 34 ++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 tests/codes_get_string.cc create mode 100755 tests/codes_get_string.sh diff --git a/src/grib_accessor_class_md5.cc b/src/grib_accessor_class_md5.cc index 142270098..d70e53af1 100644 --- a/src/grib_accessor_class_md5.cc +++ b/src/grib_accessor_class_md5.cc @@ -176,13 +176,13 @@ static int unpack_string(grib_accessor* a, char* v, size_t* len) unsigned char* p; long offset = 0, length = 0; grib_string_list* blocklist = NULL; - grib_accessor* b = NULL; - int ret = 0; - int i = 0; + int ret = GRIB_SUCCESS; + long i = 0; struct grib_md5_state md5c; if (*len < 32) { - grib_context_log(a->context, GRIB_LOG_ERROR, "md5: array too small"); + // grib_context_log(a->context, GRIB_LOG_ERROR, "grib_accessor_class_md5 unpack_string: Array too small"); + grib_context_log(a->context, GRIB_LOG_ERROR, "%s: Wrong size (%zu) for %s", __func__, *len, a->name); return GRIB_ARRAY_TOO_SMALL; } @@ -201,7 +201,7 @@ static int unpack_string(grib_accessor* a, char* v, size_t* len) if (self->blocklist) blocklist = self->blocklist; while (blocklist && blocklist->value) { - b = grib_find_accessor(grib_handle_of_accessor(a), blocklist->value); + const grib_accessor* b = grib_find_accessor(grib_handle_of_accessor(a), blocklist->value); if (!b) { grib_context_free(a->context, mess); return GRIB_NOT_FOUND; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 96e828a69..eca4a11b3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -57,6 +57,7 @@ list(APPEND test_c_bins codes_compare_keys codes_dump_content codes_codetable + codes_get_string grib_sh_ieee64 grib_ieee grib_set_bytes @@ -262,6 +263,7 @@ if( HAVE_BUILD_TOOLS ) grib_set_force bufr_ecc-556 codes_ecc-1698 + codes_get_string codes_codetable gts_get gts_ls diff --git a/tests/codes_get_string.cc b/tests/codes_get_string.cc new file mode 100644 index 000000000..8ca3bccb7 --- /dev/null +++ b/tests/codes_get_string.cc @@ -0,0 +1,44 @@ +/* + * (C) Copyright 2005- ECMWF. + * + * This software is licensed under the terms of the Apache Licence Version 2.0 + * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. + * + * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by + * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. + */ + +#include "eccodes.h" +#undef NDEBUG +#include + +int main(int argc, char* argv[]) +{ + codes_handle* h = NULL; + size_t len = 0; + int err = 0; + + assert(argc == 3); + + const char* infile = argv[1]; + FILE* in = fopen(infile, "rb"); + assert(in); + char* key = argv[2]; + char kvalue[2] = {0,}; // deliberately short + + h = codes_handle_new_from_file(NULL, in, PRODUCT_ANY, &err); + assert(h); + assert(!err); + + CODES_CHECK(codes_get_length(h, key, &len), 0); + printf("Correct len=|%zu|\n", len); + len = 1; // Cause it to fail + + err = codes_get_string(h, key, kvalue, &len); + printf("err=%d kvalue=|%s|\n", err, kvalue); + assert(err == CODES_ARRAY_TOO_SMALL || err == CODES_BUFFER_TOO_SMALL); + + codes_handle_delete(h); + fclose(in); + return 0; +} diff --git a/tests/codes_get_string.sh b/tests/codes_get_string.sh new file mode 100755 index 000000000..9292ad882 --- /dev/null +++ b/tests/codes_get_string.sh @@ -0,0 +1,34 @@ +#!/bin/sh +# (C) Copyright 2005- ECMWF. +# +# This software is licensed under the terms of the Apache Licence Version 2.0 +# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. +# +# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by +# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. +# + +. ./include.ctest.sh + +label="codes_get_string_test" +tempText=temp.$label.txt + +input=$ECCODES_SAMPLES_PATH/GRIB2.tmpl +keys="identifier md5Headers packingType" +for k in $keys; do + $EXEC ${test_dir}/codes_get_string $input $k 2> $tempText + grep -q "Wrong size" $tempText +done + + +input=$ECCODES_SAMPLES_PATH/reduced_gg_ml_grib2.tmpl +$EXEC ${test_dir}/codes_get_string "$input" gridName 2> $tempText +# TODO: No message + +# shortName = swh +input=$data_dir/reduced_latlon_surface.grib1 +$EXEC ${test_dir}/codes_get_string "$input" shortName 2> $tempText +grep -q "Wrong size" $tempText + + +rm -f $tempText