Check return value of grib_buffer_replace

This commit is contained in:
Shahram Najm 2024-01-09 10:23:13 +00:00
parent e4bf750349
commit 81f40a3735
3 changed files with 14 additions and 21 deletions

View File

@ -1350,10 +1350,10 @@ static int pack_double(grib_accessor* a, const double* val, size_t* len)
/* ECC-259: Set correct number of values */ /* ECC-259: Set correct number of values */
ret = grib_set_long_internal(a->parent->h, self->number_of_values, *len); ret = grib_set_long_internal(a->parent->h, self->number_of_values, *len);
if (ret) if (ret) return 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, buffer);
grib_context_free(a->context, X); grib_context_free(a->context, X);

View File

@ -236,7 +236,8 @@ static int pack_double(grib_accessor* a, const double* cval, size_t* len)
constantFieldHalfByte = 0; constantFieldHalfByte = 0;
if ((ret = grib_set_long_internal(grib_handle_of_accessor(a), self->half_byte, constantFieldHalfByte)) != GRIB_SUCCESS) if ((ret = grib_set_long_internal(grib_handle_of_accessor(a), self->half_byte, constantFieldHalfByte)) != GRIB_SUCCESS)
return ret; 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; return GRIB_SUCCESS;
break; break;
case GRIB_NO_VALUES: case GRIB_NO_VALUES:
@ -252,7 +253,8 @@ static int pack_double(grib_accessor* a, const double* cval, size_t* len)
return ret; return ret;
if ((ret = grib_set_long_internal(grib_handle_of_accessor(a), self->half_byte, constantFieldHalfByte)) != GRIB_SUCCESS) if ((ret = grib_set_long_internal(grib_handle_of_accessor(a), self->half_byte, constantFieldHalfByte)) != GRIB_SUCCESS)
return ret; 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; return GRIB_SUCCESS;
break; break;
case GRIB_INVALID_BPV: case GRIB_INVALID_BPV:

View File

@ -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) static void init(grib_accessor* a, const long len, grib_arguments* args)
{ {
grib_accessor_g1_message_length* self = (grib_accessor_g1_message_length*)a; 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, 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)) { if (slen < 120 && (tlen & 0x800000)) {
/* printf("DECODING large grib tlen=%ld slen=%ld\n",tlen,slen); */ /* printf("DECODING large grib tlen=%ld slen=%ld\n",tlen,slen); */
tlen &= 0x7fffff; tlen &= 0x7fffff;
tlen *= 120; tlen *= 120;
tlen -= slen; tlen -= slen;
tlen += 4; tlen += 4;
slen = tlen - s4->offset - 4; /* 4 is for 7777 */ slen = tlen - s4->offset - 4; /* 4 is for 7777 */
/*printf("DECODING large grib total=%ld section4=%ld\n",tlen,slen);*/
} }
*total_length = tlen; *total_length = tlen;
@ -184,7 +181,6 @@ static int pack_long(grib_accessor* a, const long* val, size_t* len)
slen = t120 * 120 - tlen; slen = t120 * 120 - tlen;
tlen = 0x800000 | t120; tlen = 0x800000 | t120;
/* printf("ENCODING large grib total = %ld tlen=%ld slen=%ld \n",*val,tlen,slen); */
*len = 1; *len = 1;
if ((ret = grib_pack_long(s4, &slen, len)) != GRIB_SUCCESS) if ((ret = grib_pack_long(s4, &slen, len)) != GRIB_SUCCESS)
return ret; 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; long total_length = -1, sec4_length = -1;
grib_get_g1_message_size(grib_handle_of_accessor(a), grib_get_g1_message_size(grib_handle_of_accessor(a), a,
a,
grib_find_accessor(grib_handle_of_accessor(a), self->sec4_length), grib_find_accessor(grib_handle_of_accessor(a), self->sec4_length),
&total_length, &total_length, &sec4_length);
&sec4_length);
if (total_length != *val) { if (total_length != *val) {
const char* cclass_name = a->cclass->name; const char* cclass_name = a->cclass->name;
grib_context_log(a->context, GRIB_LOG_ERROR, 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"); grib_context_log(a->context, GRIB_LOG_ERROR, "Hint: Try encoding as GRIB2\n");
return GRIB_ENCODING_ERROR; return GRIB_ENCODING_ERROR;
} }
Assert(total_length == *val);
} }
return GRIB_SUCCESS; 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; grib_accessor_g1_message_length* self = (grib_accessor_g1_message_length*)a;
int ret; int ret;
long total_length, sec4_length; long total_length, sec4_length;
if ((ret = grib_get_g1_message_size(grib_handle_of_accessor(a), if ((ret = grib_get_g1_message_size(grib_handle_of_accessor(a), a,
a,
grib_find_accessor(grib_handle_of_accessor(a), self->sec4_length), grib_find_accessor(grib_handle_of_accessor(a), self->sec4_length),
&total_length, &total_length, &sec4_length)) != GRIB_SUCCESS)
&sec4_length)) != GRIB_SUCCESS) {
return ret; return ret;
}
*val = total_length; *val = total_length;
return GRIB_SUCCESS; return GRIB_SUCCESS;