diff --git a/src/grib_accessor_class_bufr_data_array.c b/src/grib_accessor_class_bufr_data_array.c index 222e9194e..6c28403e7 100644 --- a/src/grib_accessor_class_bufr_data_array.c +++ b/src/grib_accessor_class_bufr_data_array.c @@ -743,7 +743,12 @@ static int encode_string_array(grib_context* c, grib_buffer* buff, long* pos, bu modifiedWidth = bd->width; grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + modifiedWidth); - grib_encode_string(buff->data, pos, modifiedWidth / 8, stringValues->v[ival]); + err = grib_encode_string(buff->data, pos, modifiedWidth / 8, stringValues->v[ival]); + if (err) { + grib_context_log(c, GRIB_LOG_ERROR, "encode_string_array: %s. Failed to encode '%s'", + bd->shortName, stringValues->v[ival]); + return err; + } width = n > 1 ? modifiedWidth : 0; grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + 6); @@ -752,7 +757,12 @@ static int encode_string_array(grib_context* c, grib_buffer* buff, long* pos, bu grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + width * n); for (j = 0; j < n; j++) { k = self->iss_list->v[j]; - grib_encode_string(buff->data, pos, width / 8, stringValues->v[k]); + err = grib_encode_string(buff->data, pos, width / 8, stringValues->v[k]); + if (err) { + grib_context_log(c, GRIB_LOG_ERROR, "encode_string_array: %s. Failed to encode '%s'", + bd->shortName, stringValues->v[k]); + return err; + } } } return err; @@ -1057,8 +1067,10 @@ static int encode_string_value(grib_context* c, grib_buffer* buff, long* pos, bu len = bd->width / 8; grib_buffer_set_ulength_bits(c, buff, buff->ulength_bits + bd->width); - - grib_encode_string(buff->data, pos, len, sval); + err = grib_encode_string(buff->data, pos, len, sval); + if (err) { + grib_context_log(c, GRIB_LOG_ERROR, "encode_string_value: %s. Failed to encode '%s'", bd->shortName, sval); + } return err; } diff --git a/src/grib_bits_any_endian.c b/src/grib_bits_any_endian.c index bcdc7eb2d..3c5059339 100644 --- a/src/grib_bits_any_endian.c +++ b/src/grib_bits_any_endian.c @@ -91,7 +91,7 @@ int grib_is_all_bits_one(int64_t val, long nbits) int grib_encode_string(unsigned char* bitStream, long* bitOffset, size_t numberOfCharacters, const char* string) { - size_t i; + size_t i = 0, slen = 0; int err = 0; long byteOffset = *bitOffset / 8; int remainder = *bitOffset % 8; @@ -103,15 +103,20 @@ int grib_encode_string(unsigned char* bitStream, long* bitOffset, size_t numberO char* s = str; Assert(numberOfCharacters < 512); + Assert(string); - if (string) - memcpy(s, string, strlen(string)); + slen = strlen(string); + memcpy(s, string, slen); /* if (remainder) byteOffset++; */ if (numberOfCharacters == 0) return err; + if (slen > numberOfCharacters) { + return GRIB_ENCODING_ERROR; + } + p = (unsigned char*)bitStream + byteOffset; if (remainder == 0) {