diff --git a/src/grib_accessor_class_ascii.cc b/src/grib_accessor_class_ascii.cc index 9d88fa9f9..b0c90182e 100644 --- a/src/grib_accessor_class_ascii.cc +++ b/src/grib_accessor_class_ascii.cc @@ -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; } diff --git a/src/grib_accessor_class_bitmap.cc b/src/grib_accessor_class_bitmap.cc index b3c371a35..bd52d16cd 100644 --- a/src/grib_accessor_class_bitmap.cc +++ b/src/grib_accessor_class_bitmap.cc @@ -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; } diff --git a/src/grib_accessor_class_non_alpha.cc b/src/grib_accessor_class_non_alpha.cc index 55ee9f917..f324257fe 100644 --- a/src/grib_accessor_class_non_alpha.cc +++ b/src/grib_accessor_class_non_alpha.cc @@ -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; } diff --git a/src/grib_accessor_class_to_double.cc b/src/grib_accessor_class_to_double.cc index c9725e52d..69b06add9 100644 --- a/src/grib_accessor_class_to_double.cc +++ b/src/grib_accessor_class_to_double.cc @@ -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; } diff --git a/src/grib_accessor_class_to_integer.cc b/src/grib_accessor_class_to_integer.cc index 787b122b8..76ce1205d 100644 --- a/src/grib_accessor_class_to_integer.cc +++ b/src/grib_accessor_class_to_integer.cc @@ -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; } diff --git a/src/grib_accessor_class_to_string.cc b/src/grib_accessor_class_to_string.cc index 818ad386d..648edd1aa 100644 --- a/src/grib_accessor_class_to_string.cc +++ b/src/grib_accessor_class_to_string.cc @@ -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; } diff --git a/tests/codes_get_string.cc b/tests/codes_get_string.cc index 2cc90798a..d0cf15192 100644 --- a/tests/codes_get_string.cc +++ b/tests/codes_get_string.cc @@ -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);