From 81f40a3735e4f2dc79f6890355914698ef3717d5 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Tue, 9 Jan 2024 10:23:13 +0000 Subject: [PATCH] Check return value of grib_buffer_replace --- ...g1second_order_general_extended_packing.cc | 6 ++--- ...ib_accessor_class_data_g1simple_packing.cc | 6 +++-- src/grib_accessor_class_g1_message_length.cc | 23 ++++++------------- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/grib_accessor_class_data_g1second_order_general_extended_packing.cc b/src/grib_accessor_class_data_g1second_order_general_extended_packing.cc index 2f1946b7b..e8dc5b1bb 100644 --- a/src/grib_accessor_class_data_g1second_order_general_extended_packing.cc +++ b/src/grib_accessor_class_data_g1second_order_general_extended_packing.cc @@ -1350,10 +1350,10 @@ static int pack_double(grib_accessor* a, const double* val, size_t* len) /* ECC-259: Set correct number of values */ ret = grib_set_long_internal(a->parent->h, self->number_of_values, *len); - if (ret) - return ret; + if (ret) return ret; - grib_buffer_replace(a, buffer, size, 1, 1); + ret = grib_buffer_replace(a, buffer, size, 1, 1); + if (ret) return ret; grib_context_free(a->context, buffer); grib_context_free(a->context, X); diff --git a/src/grib_accessor_class_data_g1simple_packing.cc b/src/grib_accessor_class_data_g1simple_packing.cc index c863f7595..e9f4aefdc 100644 --- a/src/grib_accessor_class_data_g1simple_packing.cc +++ b/src/grib_accessor_class_data_g1simple_packing.cc @@ -236,7 +236,8 @@ static int pack_double(grib_accessor* a, const double* cval, size_t* len) constantFieldHalfByte = 0; if ((ret = grib_set_long_internal(grib_handle_of_accessor(a), self->half_byte, constantFieldHalfByte)) != GRIB_SUCCESS) return ret; - grib_buffer_replace(a, NULL, 0, 1, 1); + ret = grib_buffer_replace(a, NULL, 0, 1, 1); + if (ret != GRIB_SUCCESS) return ret; return GRIB_SUCCESS; break; case GRIB_NO_VALUES: @@ -252,7 +253,8 @@ static int pack_double(grib_accessor* a, const double* cval, size_t* len) return ret; if ((ret = grib_set_long_internal(grib_handle_of_accessor(a), self->half_byte, constantFieldHalfByte)) != GRIB_SUCCESS) return ret; - grib_buffer_replace(a, NULL, 0, 1, 1); + ret = grib_buffer_replace(a, NULL, 0, 1, 1); + if (ret != GRIB_SUCCESS) return ret; return GRIB_SUCCESS; break; case GRIB_INVALID_BPV: diff --git a/src/grib_accessor_class_g1_message_length.cc b/src/grib_accessor_class_g1_message_length.cc index 44e2f84bc..1d38e7ea4 100644 --- a/src/grib_accessor_class_g1_message_length.cc +++ b/src/grib_accessor_class_g1_message_length.cc @@ -106,7 +106,7 @@ grib_accessor_class* grib_accessor_class_g1_message_length = &_grib_accessor_cla static void init(grib_accessor* a, const long len, grib_arguments* args) { grib_accessor_g1_message_length* self = (grib_accessor_g1_message_length*)a; - self->sec4_length = grib_arguments_get_name(grib_handle_of_accessor(a), args, 0); + self->sec4_length = grib_arguments_get_name(grib_handle_of_accessor(a), args, 0); } int grib_get_g1_message_size(grib_handle* h, grib_accessor* tl, grib_accessor* s4, @@ -134,15 +134,12 @@ int grib_get_g1_message_size(grib_handle* h, grib_accessor* tl, grib_accessor* s if (slen < 120 && (tlen & 0x800000)) { /* printf("DECODING large grib tlen=%ld slen=%ld\n",tlen,slen); */ - tlen &= 0x7fffff; tlen *= 120; tlen -= slen; tlen += 4; slen = tlen - s4->offset - 4; /* 4 is for 7777 */ - - /*printf("DECODING large grib total=%ld section4=%ld\n",tlen,slen);*/ } *total_length = tlen; @@ -184,7 +181,6 @@ static int pack_long(grib_accessor* a, const long* val, size_t* len) slen = t120 * 120 - tlen; tlen = 0x800000 | t120; - /* printf("ENCODING large grib total = %ld tlen=%ld slen=%ld \n",*val,tlen,slen); */ *len = 1; if ((ret = grib_pack_long(s4, &slen, len)) != GRIB_SUCCESS) return ret; @@ -198,11 +194,9 @@ static int pack_long(grib_accessor* a, const long* val, size_t* len) { long total_length = -1, sec4_length = -1; - grib_get_g1_message_size(grib_handle_of_accessor(a), - a, + grib_get_g1_message_size(grib_handle_of_accessor(a), a, grib_find_accessor(grib_handle_of_accessor(a), self->sec4_length), - &total_length, - &sec4_length); + &total_length, &sec4_length); if (total_length != *val) { const char* cclass_name = a->cclass->name; grib_context_log(a->context, GRIB_LOG_ERROR, @@ -212,7 +206,6 @@ static int pack_long(grib_accessor* a, const long* val, size_t* len) grib_context_log(a->context, GRIB_LOG_ERROR, "Hint: Try encoding as GRIB2\n"); return GRIB_ENCODING_ERROR; } - Assert(total_length == *val); } return GRIB_SUCCESS; @@ -222,16 +215,14 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len) { grib_accessor_g1_message_length* self = (grib_accessor_g1_message_length*)a; int ret; - long total_length, sec4_length; - if ((ret = grib_get_g1_message_size(grib_handle_of_accessor(a), - a, + if ((ret = grib_get_g1_message_size(grib_handle_of_accessor(a), a, grib_find_accessor(grib_handle_of_accessor(a), self->sec4_length), - &total_length, - &sec4_length)) != GRIB_SUCCESS) + &total_length, &sec4_length)) != GRIB_SUCCESS) + { return ret; - + } *val = total_length; return GRIB_SUCCESS;