Testing: codes_get_string errors

This commit is contained in:
Shahram Najm 2024-02-02 13:30:54 +00:00
parent 4a325a7198
commit c3703eb1ce
13 changed files with 30 additions and 24 deletions

View File

@ -148,10 +148,12 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
const size_t alen = a->length;
if (len[0] < (alen + 1)) {
grib_context_log(a->context, GRIB_LOG_ERROR, "unpack_string: Wrong size (%zu) for %s, it contains %ld values",
len[0], a->name, a->length + 1);
len[0] = 0;
return GRIB_ARRAY_TOO_SMALL;
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, alen+1, *len);
len[0] = alen + 1;
return GRIB_BUFFER_TOO_SMALL;
}
for (i = 0; i < alen; i++)

View File

@ -290,7 +290,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
"%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;
return GRIB_BUFFER_TOO_SMALL;
}
for (i = 0; i < a->length; i++) {

View File

@ -358,7 +358,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
snprintf(sval, sizeof(sval), "%g", dval);
slen = strlen(sval);
if (*len < slen)
return GRIB_ARRAY_TOO_SMALL;
return GRIB_BUFFER_TOO_SMALL;
strcpy(val, sval);
return GRIB_SUCCESS;
}

View File

@ -134,7 +134,7 @@ static int unpack_string(grib_accessor* a, char* v, size_t* len)
if (*len < (size_t)slength) {
*len = slength;
return GRIB_ARRAY_TOO_SMALL;
return GRIB_BUFFER_TOO_SMALL;
}
p = grib_handle_of_accessor(a)->buffer->data + grib_byte_offset(a);

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

@ -138,7 +138,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
l = strlen(self->value);
if (*len < l)
return GRIB_ARRAY_TOO_SMALL;
return GRIB_BUFFER_TOO_SMALL;
snprintf(val, 1024, "%s", self->value);
*len = strlen(self->value);

View File

@ -122,12 +122,12 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
if (h->gts_header == NULL || h->gts_header_len < 8) {
if (*len < 8)
return GRIB_ARRAY_TOO_SMALL;
return GRIB_BUFFER_TOO_SMALL;
snprintf(val, 1024, "missing");
return GRIB_SUCCESS;
}
if (*len < h->gts_header_len)
return GRIB_ARRAY_TOO_SMALL;
return GRIB_BUFFER_TOO_SMALL;
offset = self->gts_offset > 0 ? self->gts_offset : 0;
length = self->gts_length > 0 ? self->gts_length : h->gts_header_len;

View File

@ -282,7 +282,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
grib_handle* h = grib_handle_of_accessor(a);
if (*len < 15)
return GRIB_ARRAY_TOO_SMALL;
return GRIB_BUFFER_TOO_SMALL;
if (self->ymd == NULL) {
ret = grib_get_long(h, self->year, &year);
@ -334,6 +334,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
else {
snprintf(val, 1024, "%04ld%02ld%02ld%02ld%02ld%02ld", year, month, day, hour, minute, second);
}
*len = strlen(val)+1;
return ret;
}

View File

@ -181,7 +181,12 @@ static int unpack_string(grib_accessor* a, char* v, size_t* len)
struct grib_md5_state md5c;
if (*len < 32) {
grib_context_log(a->context, GRIB_LOG_ERROR, "%s: Wrong size (%zu) for %s", __func__, *len, a->name);
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, 32, *len);
// grib_context_log(a->context, GRIB_LOG_ERROR, "%s: Wrong size (%zu) for %s", __func__, *len, a->name);
*len = 32;
return GRIB_BUFFER_TOO_SMALL;
}

View File

@ -146,8 +146,8 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
if (len[0] < (a->length + 1)) {
grib_context_log(a->context, GRIB_LOG_ERROR, "unpack_string: Wrong size (%lu) for %s, it contains %ld values",
len[0], a->name, a->length + 1);
len[0] = 0;
return GRIB_ARRAY_TOO_SMALL;
len[0] = a->length+1;
return GRIB_BUFFER_TOO_SMALL;
}
for (i = 0; i < a->length; i++)

View File

@ -168,7 +168,7 @@ static char* get_condition(const char* name, codes_condition* condition)
condition->rightType = GRIB_TYPE_UNDEFINED;
Assert(name[0] == '/');
DEBUG_ASSERT(name[0] == '/');
while (*equal != 0 && *equal != '=')
equal++;
@ -569,7 +569,7 @@ char* grib_split_name_attribute(grib_context* c, const char* name, char* attribu
grib_accessor* grib_find_accessor(const grib_handle* h, const char* name)
{
grib_accessor* aret = NULL;
Assert(h);
DEBUG_ASSERT(h);
if (h->product_kind == PRODUCT_GRIB) {
aret = _grib_find_accessor(h, name); /* ECC-144: Performance */
}

View File

@ -36,7 +36,7 @@ int main(int argc, char* argv[])
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);
assert(err == CODES_BUFFER_TOO_SMALL);
codes_handle_delete(h);
fclose(in);

View File

@ -14,14 +14,9 @@ label="codes_get_string_test"
tempText=temp.$label.txt
input=$ECCODES_SAMPLES_PATH/GRIB2.tmpl
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
keys="projString bitmap class year gridDefinitionDescription packingType"
keys="identifier projString bitmap class year gridDefinitionDescription packingType md5Headers parameterUnits"
for k in $keys; do
$EXEC ${test_dir}/codes_get_string $input $k 2> $tempText
grep -q "Buffer too small" $tempText