codes_get_string error conditions

This commit is contained in:
Shahram Najm 2024-01-30 15:57:40 +00:00
parent 80b06beee8
commit a58fc78168
4 changed files with 85 additions and 5 deletions

View File

@ -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;

View File

@ -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

44
tests/codes_get_string.cc Normal file
View File

@ -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 <assert.h>
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;
}

34
tests/codes_get_string.sh Executable file
View File

@ -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