From 05c16a0b4ee7229ca3a782c704d0cfb431a12af0 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Fri, 19 Oct 2018 14:29:47 +0100 Subject: [PATCH] ECC-758: GRIB1: grib_dump -j crashes --- src/grib_accessor_class_mars_param.c | 93 ++++++++++++++-------------- tests/CMakeLists.txt | 1 + tests/grib_dump_json.sh | 78 +++++++++++++++++++++++ 3 files changed, 125 insertions(+), 47 deletions(-) create mode 100755 tests/grib_dump_json.sh diff --git a/src/grib_accessor_class_mars_param.c b/src/grib_accessor_class_mars_param.c index dc2a71af4..c98910ec7 100644 --- a/src/grib_accessor_class_mars_param.c +++ b/src/grib_accessor_class_mars_param.c @@ -136,73 +136,72 @@ static void init_class(grib_accessor_class* c) static void init(grib_accessor* a,const long l, grib_arguments* c) { - int n=0; - grib_accessor_mars_param* self = (grib_accessor_mars_param*)a; - self->paramId= grib_arguments_get_name(grib_handle_of_accessor(a),c,n++); - self->table= grib_arguments_get_name(grib_handle_of_accessor(a),c,n++); - self->param= grib_arguments_get_name(grib_handle_of_accessor(a),c,n++); + int n=0; + grib_accessor_mars_param* self = (grib_accessor_mars_param*)a; + self->paramId= grib_arguments_get_name(grib_handle_of_accessor(a),c,n++); + self->table= grib_arguments_get_name(grib_handle_of_accessor(a),c,n++); + self->param= grib_arguments_get_name(grib_handle_of_accessor(a),c,n++); } static int pack_string(grib_accessor* a, const char* val, size_t *len){ #if 1 - return GRIB_NOT_IMPLEMENTED; + return GRIB_NOT_IMPLEMENTED; #else - grib_accessor_mars_param* self = (grib_accessor_mars_param*)a; - long paramId=0; - long param=0; - long table=0; - char* p=(char*)val; - char* q=NULL; + grib_accessor_mars_param* self = (grib_accessor_mars_param*)a; + long paramId=0; + long param=0; + long table=0; + char* p=(char*)val; + char* q=NULL; - param=strtol(val, &p,10); - if ( *p!=0 ) table=strtol(++p, &q,10); - else table=128; + param=strtol(val, &p,10); + if ( *p!=0 ) table=strtol(++p, &q,10); + else table=128; - paramId=table*1000+param; + paramId=table*1000+param; - return grib_set_long_internal(grib_handle_of_accessor(a),self->paramId,paramId); -#endif + return grib_set_long_internal(grib_handle_of_accessor(a),self->paramId,paramId); +#endif } - -static int unpack_string(grib_accessor* a, char* val, size_t *len) +static int unpack_string(grib_accessor* a, char* val, size_t *len) { - grib_accessor_mars_param* self = (grib_accessor_mars_param*)a; - long param=0; - long table=0; - int ret=0; + grib_accessor_mars_param* self = (grib_accessor_mars_param*)a; + long param=0; + long table=0; + int ret=0; #if 1 - if(self->table!=NULL && (ret = grib_get_long_internal(grib_handle_of_accessor(a),self->table,&table)) != GRIB_SUCCESS) return ret; - if(self->param!=NULL && (ret = grib_get_long_internal(grib_handle_of_accessor(a),self->param,¶m)) != GRIB_SUCCESS) return ret; + if(self->table!=NULL && (ret = grib_get_long_internal(grib_handle_of_accessor(a),self->table,&table)) != GRIB_SUCCESS) return ret; + if(self->param!=NULL && (ret = grib_get_long_internal(grib_handle_of_accessor(a),self->param,¶m)) != GRIB_SUCCESS) return ret; #else - { - long paramId=0; - grib_get_long(grib_handle_of_accessor(a),self->paramId,¶mId); + { + long paramId=0; + grib_get_long(grib_handle_of_accessor(a),self->paramId,¶mId); - if (paramId==0 || (paramId < 4000 && paramId > 1000 )) { - if(self->table!=NULL && (ret = grib_get_long_internal(grib_handle_of_accessor(a),self->table,&table)) - != GRIB_SUCCESS) return ret; - if(self->param!=NULL && (ret = grib_get_long_internal(grib_handle_of_accessor(a),self->param,¶m)) - != GRIB_SUCCESS) return ret; - } else if (paramId<1000) { - table=128; - param=paramId; - } else { - table=paramId/1000; - param=paramId-table*1000; - } - } + if (paramId==0 || (paramId < 4000 && paramId > 1000 )) { + if(self->table!=NULL && (ret = grib_get_long_internal(grib_handle_of_accessor(a),self->table,&table)) + != GRIB_SUCCESS) return ret; + if(self->param!=NULL && (ret = grib_get_long_internal(grib_handle_of_accessor(a),self->param,¶m)) + != GRIB_SUCCESS) return ret; + } else if (paramId<1000) { + table=128; + param=paramId; + } else { + table=paramId/1000; + param=paramId-table*1000; + } + } #endif - /*if (table==200) table=128;*/ - sprintf(val,"%ld.%ld",param,table); - - return GRIB_SUCCESS; + /*if (table==200) table=128;*/ + sprintf(val,"%ld.%ld",param,table); + *len = strlen(val)+1; + + return GRIB_SUCCESS; } static size_t string_length(grib_accessor* a) { return 7; } - diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 688189fae..9a4f06a63 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -140,6 +140,7 @@ list( APPEND tests_data_reqd read_any grib_dump grib_dump_debug + grib_dump_json grib_local_MeteoFrance grib_neg_fctime codes_split_file diff --git a/tests/grib_dump_json.sh b/tests/grib_dump_json.sh new file mode 100755 index 000000000..2579e5164 --- /dev/null +++ b/tests/grib_dump_json.sh @@ -0,0 +1,78 @@ +#!/bin/sh +# Copyright 2005-2018 ECMWF. +# +# This software is licensed under the terms of the Apache Licence Version 2.0 +# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. +# +# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by +# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. +# + +. ./include.sh + +REDIRECT=/dev/null + +if [ $HAVE_MEMFS -eq 1 ]; then + unset ECCODES_DEFINITION_PATH + unset ECCODES_SAMPLES_PATH +fi + +files=" +constant_field.grib1 +lfpw.grib1 +missing_field.grib1 +reduced_gaussian_lsm.grib1 +reduced_gaussian_model_level.grib1 +reduced_gaussian_pressure_level.grib1 +reduced_gaussian_pressure_level_constant.grib1 +reduced_gaussian_sub_area.grib1 +reduced_gaussian_surface.grib1 +reduced_latlon_surface.grib1 +reduced_latlon_surface_constant.grib1 +reference_ensemble_mean.grib1 +reference_stdev.grib1 +regular_gaussian_model_level.grib1 +regular_gaussian_pressure_level.grib1 +regular_gaussian_pressure_level_constant.grib1 +regular_gaussian_surface.grib1 +regular_latlon_surface.grib1 +regular_latlon_surface_constant.grib1 +second_ord_rbr.grib1 +small_ensemble.grib1 +spectral_complex.grib1 +spherical_model_level.grib1 +spherical_pressure_level.grib1 +constant_field.grib2 +gfs.c255.grib2 +jpeg.grib2 +missing.grib2 +multi.grib2 +multi_created.grib2 +reduced_gaussian_model_level.grib2 +reduced_gaussian_pressure_level.grib2 +reduced_gaussian_pressure_level_constant.grib2 +reduced_gaussian_sub_area.grib2 +reduced_gaussian_surface.grib2 +reduced_gaussian_surface_jpeg.grib2 +reduced_latlon_surface.grib2 +reduced_latlon_surface_constant.grib2 +regular_gaussian_model_level.grib2 +regular_gaussian_pressure_level.grib2 +regular_gaussian_pressure_level_constant.grib2 +regular_gaussian_surface.grib2 +regular_latlon_surface.grib2 +regular_latlon_surface_constant.grib2 +sample.grib2 +spherical_model_level.grib2 +spherical_pressure_level.grib2 +test_uuid.grib2 +tigge_af_ecmwf.grib2 +tigge_cf_ecmwf.grib2 +v.grib2 +" + +for file in $files; do + if [ -f ${data_dir}/$file ]; then + ${tools_dir}/grib_dump -j ${data_dir}/$file 2> $REDIRECT > $REDIRECT + fi +done