Testing: codes_get_string errors

This commit is contained in:
Shahram Najm 2024-02-13 12:38:26 +00:00
parent 3f28448146
commit c7930a6dd1
7 changed files with 43 additions and 38 deletions

View File

@ -147,19 +147,19 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
grib_handle* hand = grib_handle_of_accessor(a);
const size_t alen = a->length;
if (len[0] < (alen + 1)) {
if (*len < (alen + 1)) {
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;
*len = alen + 1;
return GRIB_BUFFER_TOO_SMALL;
}
for (i = 0; i < alen; i++)
val[i] = hand->buffer->data[a->offset + i];
val[i] = 0;
len[0] = i;
*len = i;
return GRIB_SUCCESS;
}

View File

@ -297,7 +297,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
val[i] = hand->buffer->data[a->offset + i];
}
len[0] = a->length;
*len = a->length;
return GRIB_SUCCESS;
}

View File

@ -149,20 +149,20 @@ static int get_native_type(grib_accessor* a)
static int unpack_string(grib_accessor* a, char* val, size_t* len)
{
grib_handle* hand = grib_handle_of_accessor(a);
int i = 0;
long i = 0;
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;
if (*len < (a->length + 1)) {
grib_context_log(a->context, GRIB_LOG_ERROR, "unpack_string: Wrong size (%lu) for %s, it contains %ld values",
*len, a->name, a->length + 1);
*len = a->length + 1;
return GRIB_BUFFER_TOO_SMALL;
}
for (i = 0; i < a->length; i++) {
val[i] = hand->buffer->data[a->offset + i];
}
val[i] = 0;
len[0] = i;
*len = i;
return GRIB_SUCCESS;
}

View File

@ -166,18 +166,17 @@ static int get_native_type(grib_accessor* a)
static int unpack_string(grib_accessor* a, char* val, size_t* len)
{
int err = 0;
grib_accessor_to_double* self = (grib_accessor_to_double*)a;
char buff[512] = {0,};
size_t length;
int err = 0;
char buff[512] = {0,};
size_t size = 512;
size_t length = string_length(a);
length = string_length(a);
if (len[0] < length + 1) {
if (*len < 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;
*len, a->name, a->length + 1);
*len = length + 1;
return GRIB_ARRAY_TOO_SMALL;
}
@ -192,7 +191,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
memcpy(val, buff + self->start, length);
val[length] = 0;
len[0] = length;
*len = length;
return err;
}

View File

@ -164,19 +164,21 @@ static int get_native_type(grib_accessor* a)
static int unpack_string(grib_accessor* a, char* val, size_t* len)
{
int err = 0;
grib_accessor_to_integer* self = (grib_accessor_to_integer*)a;
char buff[512] = {0,};
size_t length;
int err = 0;
char buff[512] = {0,};
size_t size = 512;
length = string_length(a);
size_t length = string_length(a);
if (len[0] < 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;
if (*len < length + 1) {
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+1, *len);
*len = length + 1;
return GRIB_BUFFER_TOO_SMALL;
}
err = grib_get_string(grib_handle_of_accessor(a), self->key, buff, &size);
@ -190,7 +192,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
memcpy(val, buff + self->start, length);
val[length] = 0;
len[0] = length;
*len = length;
return GRIB_SUCCESS;
}

View File

@ -163,16 +163,18 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
{
grib_accessor_to_string* self = (grib_accessor_to_string*)a;
int err = 0;
int err = 0;
char buff[512] = {0,};
size_t length = string_length(a);
if (len[0] < 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;
if (*len < length + 1) {
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+1, *len);
*len = length + 1;
return GRIB_BUFFER_TOO_SMALL;
}
size_t size = sizeof(buff);
@ -187,7 +189,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
memcpy(val, buff + self->start, length);
val[length] = 0;
len[0] = length;
*len = length;
return GRIB_SUCCESS;
}

View File

@ -35,8 +35,10 @@ int main(int argc, char* argv[])
len = 1; // Cause it to fail
err = codes_get_string(h, key, kvalue, &len);
printf("err=%d kvalue=|%s|\n", err, kvalue);
//printf("err=%d kvalue=|%s|\n", err, kvalue);
assert(err == CODES_BUFFER_TOO_SMALL);
// The correct len should have been set
assert(len > 1);
codes_handle_delete(h);
fclose(in);