Testing: codes_get_string

This commit is contained in:
Shahram Najm 2024-02-01 17:23:58 +00:00
parent 98e8126ae9
commit b7d78a79b2
8 changed files with 39 additions and 18 deletions

View File

@ -282,11 +282,14 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
{
long i = 0;
grib_handle* hand = grib_handle_of_accessor(a);
const size_t l = a->length;
if (len[0] < (a->length)) {
grib_context_log(a->context, GRIB_LOG_ERROR, "%s: Wrong size (%zu) for %s, it contains %ld values",
__func__, len[0], a->name, a->length);
len[0] = 0;
if (*len < l) {
const char* cclass_name = a->cclass->name;
grib_context_log(a->context, GRIB_LOG_ERROR,
"%s: Buffer too small for %s. It is %zu bytes long (len=%zu)",
cclass_name, a->name, l, *len);
*len = l;
return GRIB_ARRAY_TOO_SMALL;
}

View File

@ -700,6 +700,10 @@ static int unpack_string(grib_accessor* a, char* buffer, size_t* len)
l = strlen(tmp) + 1;
if (*len < l) {
const char* cclass_name = a->cclass->name;
grib_context_log(a->context, GRIB_LOG_ERROR,
"%s: Buffer too small for %s. It is %zu bytes long (len=%zu)",
cclass_name, a->name, l, *len);
*len = l;
return GRIB_BUFFER_TOO_SMALL;
}

View File

@ -156,8 +156,11 @@ static int unpack_string(grib_accessor* a, char* buffer, size_t* len)
l = strlen(tmp) + 1;
if (*len < l) {
const char* cclass_name = a->cclass->name;
grib_context_log(a->context, GRIB_LOG_ERROR,
"%s: Buffer too small for %s. It is %zu bytes long (len=%zu)",
cclass_name, a->name, l, *len);
*len = l;
grib_context_log(a->context, GRIB_LOG_ERROR, "%s: Wrong size (%zu) for %s", __func__, *len, a->name);
return GRIB_BUFFER_TOO_SMALL;
}

View File

@ -559,7 +559,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
slen = strlen(p) + 1;
if (*len < slen) {
grib_context_log(a->context, GRIB_LOG_ERROR,
"Concept unpack_string. Wrong size for %s, value='%s' which requires %lu bytes (len=%lu)",
"Concept unpack_string. Buffer too small for %s, value='%s' which requires %lu bytes (len=%lu)",
a->name, p, slen, *len);
*len = slen;
return GRIB_BUFFER_TOO_SMALL;

View File

@ -157,8 +157,11 @@ static int unpack_string(grib_accessor* a, char* v, size_t* len)
length = strlen(tmp) + 1;
if (*len < length) {
const char* cclass_name = a->cclass->name;
grib_context_log(a->context, GRIB_LOG_ERROR,
"%s: Buffer too small for %s. It is %zu bytes long (len=%zu)",
cclass_name, a->name, length, *len);
*len = length;
grib_context_log(a->context, GRIB_LOG_ERROR, "%s: Wrong size (%zu) for %s", __func__, *len, a->name);
return GRIB_BUFFER_TOO_SMALL;
}

View File

@ -122,7 +122,6 @@ static int unpack_string(grib_accessor* a, char* v, size_t* len)
char repres[1024];
char format[32] = "%ld";
grib_handle* h = grib_handle_of_accessor(a);
const char* cclass_name = a->cclass->name;
err = grib_unpack_long(a, &val, &l);
/* TODO: We should catch all errors but in this case the test ERA_Gen.sh will fail
@ -140,7 +139,8 @@ static int unpack_string(grib_accessor* a, char* v, size_t* len)
l = strlen(repres) + 1;
if (l > *len) {
if (*len < l) {
const char* cclass_name = a->cclass->name;
grib_context_log(a->context, GRIB_LOG_ERROR,
"%s: Buffer too small for %s. It is %zu bytes long (len=%zu)",
cclass_name, a->name, l, *len);

View File

@ -297,15 +297,20 @@ static int unpack_string(grib_accessor* a, char* v, size_t* len)
{
grib_accessor_proj_string* self = (grib_accessor_proj_string*)a;
int err = 0, found = 0;
size_t i = 0;
size_t i = 0;
char grid_type[64] = {0,};
grib_handle* h = grib_handle_of_accessor(a);
size_t size = sizeof(grid_type) / sizeof(*grid_type);
size_t size = sizeof(grid_type) / sizeof(*grid_type);
Assert(self->endpoint == ENDPOINT_SOURCE || self->endpoint == ENDPOINT_TARGET);
if (*len < 100) { // Safe bet
grib_context_log(a->context, GRIB_LOG_ERROR, "%s: Wrong size (%zu) for %s", __func__, *len, a->name);
size_t l = 100; // Safe bet
if (*len < l) {
const char* cclass_name = a->cclass->name;
grib_context_log(a->context, GRIB_LOG_ERROR,
"%s: Buffer too small for %s. It is at least %zu bytes long (len=%zu)",
cclass_name, a->name, l, *len);
*len = l;
return GRIB_BUFFER_TOO_SMALL;
}

View File

@ -14,25 +14,28 @@ label="codes_get_string_test"
tempText=temp.$label.txt
input=$ECCODES_SAMPLES_PATH/GRIB2.tmpl
keys="identifier md5Headers packingType gridDefinitionDescription parameterUnits projString"
keys="identifier md5Headers parameterUnits"
for k in $keys; do
$EXEC ${test_dir}/codes_get_string $input $k 2> $tempText
grep -q "Wrong size" $tempText
done
input=$data_dir/reduced_latlon_surface.grib2
$EXEC ${test_dir}/codes_get_string "$input" bitmap 2> $tempText
grep -q "Wrong size" $tempText
keys="projString bitmap class year gridDefinitionDescription packingType"
for k in $keys; do
$EXEC ${test_dir}/codes_get_string $input $k 2> $tempText
grep -q "Buffer too small" $tempText
done
input=$ECCODES_SAMPLES_PATH/reduced_gg_ml_grib2.tmpl
$EXEC ${test_dir}/codes_get_string "$input" gridName 2> $tempText
grep -q "Wrong size" $tempText
grep -q "Buffer too small" $tempText
# shortName = swh
input=$data_dir/reduced_latlon_surface.grib1
$EXEC ${test_dir}/codes_get_string "$input" shortName 2> $tempText
grep -q "Wrong size" $tempText
grep -q "Buffer too small" $tempText
rm -f $tempText