From dccad7f9d1eec4854e9dc5291e0a9020ed3ba966 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Tue, 29 Aug 2017 15:30:46 +0100 Subject: [PATCH] GRIB-364: Memory fault --- ...rib_accessor_class_data_g2simple_packing.c | 175 +++++++-------- ...data_g2simple_packing_with_preprocessing.c | 203 +++++++++--------- 2 files changed, 190 insertions(+), 188 deletions(-) diff --git a/src/grib_accessor_class_data_g2simple_packing.c b/src/grib_accessor_class_data_g2simple_packing.c index 83c73aa8b..c63b3eb2b 100644 --- a/src/grib_accessor_class_data_g2simple_packing.c +++ b/src/grib_accessor_class_data_g2simple_packing.c @@ -149,118 +149,125 @@ static void init_class(grib_accessor_class* c) static void init(grib_accessor* a,const long v, grib_arguments* args) { - grib_accessor_data_g2simple_packing *self =(grib_accessor_data_g2simple_packing*)a; - a->flags |= GRIB_ACCESSOR_FLAG_DATA; - self->edition=2; + grib_accessor_data_g2simple_packing *self =(grib_accessor_data_g2simple_packing*)a; + a->flags |= GRIB_ACCESSOR_FLAG_DATA; + self->edition=2; } static int value_count(grib_accessor* a,long* n_vals) { - grib_accessor_data_g2simple_packing *self =(grib_accessor_data_g2simple_packing*)a; - *n_vals= 0; - return grib_get_long_internal(grib_handle_of_accessor(a),self->number_of_values,n_vals); + grib_accessor_data_g2simple_packing *self =(grib_accessor_data_g2simple_packing*)a; + *n_vals= 0; + return grib_get_long_internal(grib_handle_of_accessor(a),self->number_of_values,n_vals); } static int pack_double(grib_accessor* a, const double* cval, size_t *len) { - grib_accessor_data_g2simple_packing* self = (grib_accessor_data_g2simple_packing*)a; - grib_accessor_class* super = *(a->cclass->super); - size_t n_vals = *len; - double reference_value = 0; - long binary_scale_factor = 0; - long bits_per_value = 0; - long decimal_scale_factor = 0; - double decimal = 1; - size_t buflen = 0; - unsigned char* buf = NULL; - unsigned char* encoded = NULL; - double divisor = 1; - long off = 0; - int ret =0; - double units_factor=1.0; - double units_bias=0.0; - double* val=(double*)cval; - int i; + grib_accessor_data_g2simple_packing* self = (grib_accessor_data_g2simple_packing*)a; + grib_accessor_class* super = *(a->cclass->super); + size_t n_vals = *len; + double reference_value = 0; + long binary_scale_factor = 0; + long bits_per_value = 0; + long decimal_scale_factor = 0; + double decimal = 1; + size_t buflen = 0; + unsigned char* buf = NULL; + unsigned char* encoded = NULL; + double divisor = 1; + long off = 0; + int ret =0; + double units_factor=1.0; + double units_bias=0.0; + double* val=(double*)cval; + int i; - if(*len == 0) { - grib_buffer_replace(a, NULL, 0,1,1); - return GRIB_SUCCESS; - } + if(*len == 0) { + grib_buffer_replace(a, NULL, 0,1,1); + return GRIB_SUCCESS; + } - if(ret == GRIB_SUCCESS) - ret = grib_set_long_internal(grib_handle_of_accessor(a),self->number_of_values, *len); + if(ret == GRIB_SUCCESS) + ret = grib_set_long_internal(grib_handle_of_accessor(a),self->number_of_values, *len); - if (ret!=GRIB_SUCCESS) return ret; + if (ret!=GRIB_SUCCESS) return ret; - if(self->units_factor && - (grib_get_double_internal(grib_handle_of_accessor(a),self->units_factor,&units_factor)== GRIB_SUCCESS)) { - grib_set_double_internal(grib_handle_of_accessor(a),self->units_factor,1.0); - } + if(self->units_factor && + (grib_get_double_internal(grib_handle_of_accessor(a),self->units_factor,&units_factor)== GRIB_SUCCESS)) { + grib_set_double_internal(grib_handle_of_accessor(a),self->units_factor,1.0); + } - if(self->units_bias && - (grib_get_double_internal(grib_handle_of_accessor(a),self->units_bias,&units_bias)== GRIB_SUCCESS)) { - grib_set_double_internal(grib_handle_of_accessor(a),self->units_bias,0.0); - } + if(self->units_bias && + (grib_get_double_internal(grib_handle_of_accessor(a),self->units_bias,&units_bias)== GRIB_SUCCESS)) { + grib_set_double_internal(grib_handle_of_accessor(a),self->units_bias,0.0); + } - if (units_factor != 1.0) { - if (units_bias != 0.0) - for (i=0;ipack_double(a,val,len); - switch (ret) { - case GRIB_CONSTANT_FIELD: - grib_buffer_replace(a, NULL, 0,1,1); - return GRIB_SUCCESS; - case GRIB_SUCCESS: - break; - default: - grib_context_log(a->context,GRIB_LOG_ERROR,"unable to compute packing parameters\n"); - return ret; - } + if (super != grib_accessor_class_data_g2simple_packing) { + /* Normal case: parent not same as me! */ + ret = super->pack_double(a,val,len); + } else { + /* GRIB-364: simple packing with logarithm pre-processing */ + grib_accessor_class* super2 = *(super->super); + ret = super2->pack_double(a,val,len); + } - if((ret = grib_get_double_internal(grib_handle_of_accessor(a),self->reference_value, &reference_value)) - != GRIB_SUCCESS) - return ret; + switch (ret) { + case GRIB_CONSTANT_FIELD: + grib_buffer_replace(a, NULL, 0,1,1); + return GRIB_SUCCESS; + case GRIB_SUCCESS: + break; + default: + grib_context_log(a->context,GRIB_LOG_ERROR,"unable to compute packing parameters\n"); + return ret; + } - if((ret = grib_get_long_internal(grib_handle_of_accessor(a),self->binary_scale_factor, &binary_scale_factor)) - != GRIB_SUCCESS) - return ret; + if((ret = grib_get_double_internal(grib_handle_of_accessor(a),self->reference_value, &reference_value)) + != GRIB_SUCCESS) + return ret; - if((ret = grib_get_long_internal(grib_handle_of_accessor(a),self->bits_per_value,&bits_per_value)) != - GRIB_SUCCESS) - return ret; + if((ret = grib_get_long_internal(grib_handle_of_accessor(a),self->binary_scale_factor, &binary_scale_factor)) + != GRIB_SUCCESS) + return ret; - if((ret = grib_get_long_internal(grib_handle_of_accessor(a),self->decimal_scale_factor, &decimal_scale_factor)) - != GRIB_SUCCESS) - return ret; + if((ret = grib_get_long_internal(grib_handle_of_accessor(a),self->bits_per_value,&bits_per_value)) != + GRIB_SUCCESS) + return ret; - decimal = grib_power(decimal_scale_factor,10) ; - divisor = grib_power(-binary_scale_factor,2); + if((ret = grib_get_long_internal(grib_handle_of_accessor(a),self->decimal_scale_factor, &decimal_scale_factor)) + != GRIB_SUCCESS) + return ret; - buflen = (((bits_per_value*n_vals)+7)/8)*sizeof(unsigned char); - buf = (unsigned char*)grib_context_buffer_malloc_clear(a->context,buflen); - encoded = buf; + decimal = grib_power(decimal_scale_factor,10) ; + divisor = grib_power(-binary_scale_factor,2); - grib_encode_double_array(n_vals,val,bits_per_value,reference_value,decimal,divisor,encoded,&off); + buflen = (((bits_per_value*n_vals)+7)/8)*sizeof(unsigned char); + buf = (unsigned char*)grib_context_buffer_malloc_clear(a->context,buflen); + encoded = buf; - grib_context_log(a->context, GRIB_LOG_DEBUG, - "grib_accessor_data_g2simple_packing : pack_double : packing %s, %d values", a->name, n_vals); + grib_encode_double_array(n_vals,val,bits_per_value,reference_value,decimal,divisor,encoded,&off); - grib_buffer_replace(a, buf, buflen,1,1); + grib_context_log(a->context, GRIB_LOG_DEBUG, + "grib_accessor_data_g2simple_packing : pack_double : packing %s, %d values", a->name, n_vals); - grib_context_buffer_free(a->context,buf); + grib_buffer_replace(a, buf, buflen,1,1); - return ret; + grib_context_buffer_free(a->context,buf); + + return ret; } static int pack_bytes(grib_accessor* a, const unsigned char* val, size_t *len) { - size_t length = *len; - grib_buffer_replace(a, val, length,1,1); - return GRIB_SUCCESS; + size_t length = *len; + grib_buffer_replace(a, val, length,1,1); + return GRIB_SUCCESS; } - diff --git a/src/grib_accessor_class_data_g2simple_packing_with_preprocessing.c b/src/grib_accessor_class_data_g2simple_packing_with_preprocessing.c index cc2505370..3107a0cdd 100644 --- a/src/grib_accessor_class_data_g2simple_packing_with_preprocessing.c +++ b/src/grib_accessor_class_data_g2simple_packing_with_preprocessing.c @@ -164,149 +164,144 @@ static void init_class(grib_accessor_class* c) static void init(grib_accessor* a,const long v, grib_arguments* args) { - grib_accessor_data_g2simple_packing_with_preprocessing *self =(grib_accessor_data_g2simple_packing_with_preprocessing*)a; - self->pre_processing = grib_arguments_get_name(grib_handle_of_accessor(a),args,self->carg++); - self->pre_processing_parameter = grib_arguments_get_name(grib_handle_of_accessor(a),args,self->carg++); - a->flags |= GRIB_ACCESSOR_FLAG_DATA; + grib_accessor_data_g2simple_packing_with_preprocessing *self =(grib_accessor_data_g2simple_packing_with_preprocessing*)a; + self->pre_processing = grib_arguments_get_name(grib_handle_of_accessor(a),args,self->carg++); + self->pre_processing_parameter = grib_arguments_get_name(grib_handle_of_accessor(a),args,self->carg++); + a->flags |= GRIB_ACCESSOR_FLAG_DATA; } static int value_count(grib_accessor* a,long* n_vals) { - grib_accessor_data_g2simple_packing_with_preprocessing *self =(grib_accessor_data_g2simple_packing_with_preprocessing*)a; - *n_vals= 0; + grib_accessor_data_g2simple_packing_with_preprocessing *self =(grib_accessor_data_g2simple_packing_with_preprocessing*)a; + *n_vals= 0; - return grib_get_long_internal(grib_handle_of_accessor(a),self->number_of_values,n_vals); + return grib_get_long_internal(grib_handle_of_accessor(a),self->number_of_values,n_vals); } -static int unpack_double(grib_accessor* a, double* val, size_t *len) +static int unpack_double(grib_accessor* a, double* val, size_t *len) { - grib_accessor_data_g2simple_packing_with_preprocessing* self = (grib_accessor_data_g2simple_packing_with_preprocessing*)a; - grib_accessor_class* super = *(a->cclass->super); + grib_accessor_data_g2simple_packing_with_preprocessing* self = (grib_accessor_data_g2simple_packing_with_preprocessing*)a; + grib_accessor_class* super = *(a->cclass->super); + grib_accessor_class* super2 = *(super->super); - size_t n_vals = 0; - long nn=0; - int err = 0; + size_t n_vals = 0; + long nn=0; + int err = 0; - long pre_processing; - double pre_processing_parameter; + long pre_processing; + double pre_processing_parameter; - err=grib_value_count(a,&nn); - n_vals=nn; - if (err) return err; + err=grib_value_count(a,&nn); + n_vals=nn; + if (err) return err; - if(n_vals==0){ - *len = 0; - return GRIB_SUCCESS; - } + if (n_vals==0){ + *len = 0; + return GRIB_SUCCESS; + } - self->dirty=0; + self->dirty=0; + if((err = grib_get_long_internal(grib_handle_of_accessor(a),self->pre_processing, &pre_processing)) != GRIB_SUCCESS){ + grib_context_log(a->context, GRIB_LOG_ERROR, "Accessor %s cannont gather value for %s error %d \n", a->name, self->pre_processing, err); + return err; + } + + if((err = grib_get_double_internal(grib_handle_of_accessor(a),self->pre_processing_parameter, &pre_processing_parameter)) != GRIB_SUCCESS){ + grib_context_log(a->context, GRIB_LOG_ERROR, "Accessor %s cannont gather value for %s error %d \n", a->name, self->pre_processing_parameter, err); + return err; + } + + err = super2->unpack_double(a,val,&n_vals); + if (err!=GRIB_SUCCESS) return err; + + err=pre_processing_func(val,n_vals,pre_processing,&pre_processing_parameter,INVERSE); + if (err != GRIB_SUCCESS) return err; + + *len = (long) n_vals; - if((err = grib_get_long_internal(grib_handle_of_accessor(a),self->pre_processing, &pre_processing)) != GRIB_SUCCESS){ - grib_context_log(a->context, GRIB_LOG_ERROR, "Accessor %s cannont gather value for %s error %d \n", a->name, self->pre_processing, err); return err; - } - - if((err = grib_get_double_internal(grib_handle_of_accessor(a),self->pre_processing_parameter, &pre_processing_parameter)) != GRIB_SUCCESS){ - grib_context_log(a->context, GRIB_LOG_ERROR, "Accessor %s cannont gather value for %s error %d \n", a->name, self->pre_processing_parameter, err); - return err; - } - - err = super->unpack_double(a,val,&n_vals); - if (err!=GRIB_SUCCESS) return err; - - err=pre_processing_func(val,n_vals,pre_processing,&pre_processing_parameter,INVERSE); - if (err != GRIB_SUCCESS) return err; - - *len = (long) n_vals; - - return err; } static int pack_double(grib_accessor* a, const double* val, size_t *len) { - grib_accessor_data_g2simple_packing_with_preprocessing* self = (grib_accessor_data_g2simple_packing_with_preprocessing*)a; - grib_accessor_class* super = *(a->cclass->super); + grib_accessor_data_g2simple_packing_with_preprocessing* self = (grib_accessor_data_g2simple_packing_with_preprocessing*)a; + grib_accessor_class* super = *(a->cclass->super); - size_t n_vals = *len; - int err = 0; + size_t n_vals = *len; + int err = 0; - long pre_processing=0; - double pre_processing_parameter=0; + long pre_processing=0; + double pre_processing_parameter=0; - self->dirty=1; + self->dirty=1; - if((err = grib_get_long_internal(grib_handle_of_accessor(a),self->pre_processing, &pre_processing)) != GRIB_SUCCESS) - return err; + if((err = grib_get_long_internal(grib_handle_of_accessor(a),self->pre_processing, &pre_processing)) != GRIB_SUCCESS) + return err; - err=pre_processing_func((double*)val,n_vals,pre_processing,&pre_processing_parameter,DIRECT); - if (err != GRIB_SUCCESS) return err; + err=pre_processing_func((double*)val,n_vals,pre_processing,&pre_processing_parameter,DIRECT); + if (err != GRIB_SUCCESS) return err; - err = super->pack_double(a,val,len); - if (err!=GRIB_SUCCESS) return err; + err = super->pack_double(a,val,len); + if (err!=GRIB_SUCCESS) return err; - if((err = grib_set_double_internal(grib_handle_of_accessor(a),self->pre_processing_parameter, pre_processing_parameter)) != GRIB_SUCCESS) - return err; + if((err = grib_set_double_internal(grib_handle_of_accessor(a),self->pre_processing_parameter, pre_processing_parameter)) != GRIB_SUCCESS) + return err; + if((err = grib_set_long_internal(grib_handle_of_accessor(a),self->number_of_values, n_vals)) != GRIB_SUCCESS) + return err; - if((err = grib_set_long_internal(grib_handle_of_accessor(a),self->number_of_values, n_vals)) != GRIB_SUCCESS) - return err; - - - return GRIB_SUCCESS; - + return GRIB_SUCCESS; } - static int pre_processing_func(double* values,long length, long pre_processing, - double *pre_processing_parameter,int mode) { - int i; - int ret=0; - double min=values[0]; - double next_min=values[0]; + double *pre_processing_parameter,int mode) { + int i; + int ret=0; + double min=values[0]; + double next_min=values[0]; - switch (pre_processing) { + switch (pre_processing) { /* NONE */ case 0: - break; + break; /* LOGARITHM */ case 1: - if (mode == DIRECT) { - for (i=0;i next_min) - next_min=values[i]; - } - for (i=0;i min && values[i] < next_min ) - next_min=values[i]; - } - if ( min > 0 ) { - *pre_processing_parameter=0; - for (i=0;i next_min) + next_min=values[i]; + } + for (i=0;i min && values[i] < next_min ) + next_min=values[i]; + } + if ( min > 0 ) { + *pre_processing_parameter=0; + for (i=0;i