From 6eb42de19981faaad9f6fde9fd39c16192a77fbb Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Fri, 22 Feb 2019 18:07:46 +0000 Subject: [PATCH] GCC 8.3.0: fix all sprintf buffer size issues --- src/action_class_concept.c | 4 +- src/action_class_hash_array.c | 4 +- src/grib_accessor_class_bufr_elements_table.c | 6 +- src/grib_accessor_class_codetable.c | 4 +- src/grib_accessor_class_dictionary.c | 6 +- src/grib_accessor_class_g1step_range.c | 2 +- src/grib_accessor_class_g2_mars_labeling.c | 399 +++++++++--------- src/grib_accessor_class_lookup.c | 2 +- src/grib_accessor_class_smart_table.c | 6 +- src/grib_accessor_class_sprintf.c | 2 +- tools/grib_merge.c | 2 +- 11 files changed, 219 insertions(+), 218 deletions(-) diff --git a/src/action_class_concept.c b/src/action_class_concept.c index c3f3c1055..b714a08cf 100644 --- a/src/action_class_concept.c +++ b/src/action_class_concept.c @@ -218,12 +218,12 @@ static void destroy(grib_context* context,grib_action* act) static grib_concept_value* get_concept_impl(grib_handle* h,grib_action_concept* self) { - char buf[1024]={0,}; + char buf[4096]={0,}; char master[1024]={0,}; char local[1024]={0,}; char masterDir[1024]={0,}; size_t lenMasterDir=1024; - char key[1024]={0,}; + char key[4096]={0,}; char* full=0; int id; diff --git a/src/action_class_hash_array.c b/src/action_class_hash_array.c index 2443b0046..dbf976c84 100644 --- a/src/action_class_hash_array.c +++ b/src/action_class_hash_array.c @@ -213,7 +213,7 @@ static void destroy(grib_context* context,grib_action* act) static grib_hash_array_value* get_hash_array_impl(grib_handle* h,grib_action* a) { - char buf[1024]={0,}; + char buf[4096]={0,}; char master[1024]={0,}; char local[1024]={0,}; char ecmf[1024]={0,}; @@ -223,7 +223,7 @@ static grib_hash_array_value* get_hash_array_impl(grib_handle* h,grib_action* a) size_t lenLocalDir=1024; char ecmfDir[1024]={0,}; size_t lenEcmfDir=1024; - char key[1024]={0,}; + char key[4096]={0,}; char* full=0; int id; int err; diff --git a/src/grib_accessor_class_bufr_elements_table.c b/src/grib_accessor_class_bufr_elements_table.c index ae9b438eb..a589a1658 100644 --- a/src/grib_accessor_class_bufr_elements_table.c +++ b/src/grib_accessor_class_bufr_elements_table.c @@ -208,8 +208,8 @@ static grib_trie* load_bufr_elements_table(grib_accessor* a, int* err) GRIB_MUTEX_LOCK(&mutex1); if (*masterDir!=0) { - char name[1024]={0,}; - char recomposed[1024]={0,}; + char name[4096]={0,}; + char recomposed[4096]={0,}; sprintf(name,"%s/%s",masterDir,self->dictionary); grib_recompose_name(h, NULL,name, recomposed,0); filename=grib_context_full_defs_path(c,recomposed); @@ -219,7 +219,7 @@ static grib_trie* load_bufr_elements_table(grib_accessor* a, int* err) if (*localDir!=0) { char localRecomposed[1024]={0,}; - char localName[1024]={0,}; + char localName[2048]={0,}; sprintf(localName,"%s/%s",localDir,self->dictionary); grib_recompose_name(h, NULL,localName, localRecomposed,0); localFilename=grib_context_full_defs_path(c,localRecomposed); diff --git a/src/grib_accessor_class_codetable.c b/src/grib_accessor_class_codetable.c index 8c3d7d86a..48a420557 100644 --- a/src/grib_accessor_class_codetable.c +++ b/src/grib_accessor_class_codetable.c @@ -306,7 +306,7 @@ static grib_codetable* load_table(grib_accessor_codetable* self) grib_get_string(h,self->localDir,localDir,&len); if (*masterDir!=0) { - char name[1024]={0,}; + char name[2048]={0,}; sprintf(name,"%s/%s",masterDir,self->tablename); grib_recompose_name(h, NULL,name, recomposed,0); filename=grib_context_full_defs_path(c,recomposed); @@ -316,7 +316,7 @@ static grib_codetable* load_table(grib_accessor_codetable* self) } if (*localDir!=0) { - char localName[1024]={0,}; + char localName[2048]={0,}; sprintf(localName,"%s/%s",localDir,self->tablename); grib_recompose_name(h, NULL,localName, localRecomposed,0); localFilename=grib_context_full_defs_path(c,localRecomposed); diff --git a/src/grib_accessor_class_dictionary.c b/src/grib_accessor_class_dictionary.c index ff20b440a..96497b706 100644 --- a/src/grib_accessor_class_dictionary.c +++ b/src/grib_accessor_class_dictionary.c @@ -185,8 +185,8 @@ static grib_trie* load_dictionary(grib_context* c,grib_accessor* a, int* err) if (self->localDir != NULL) grib_get_string(h,self->localDir,localDir,&len); if (*masterDir!=0) { - char name[1024]={0,}; - char recomposed[1024]={0,}; + char name[2048]={0,}; + char recomposed[2048]={0,}; sprintf(name,"%s/%s",masterDir,self->dictionary); grib_recompose_name(h, NULL,name, recomposed,0); filename=grib_context_full_defs_path(c,recomposed); @@ -195,7 +195,7 @@ static grib_trie* load_dictionary(grib_context* c,grib_accessor* a, int* err) } if (*localDir!=0) { - char localName[1024]={0,}; + char localName[2048]={0,}; char localRecomposed[1024]={0,}; sprintf(localName,"%s/%s",localDir,self->dictionary); grib_recompose_name(h, NULL,localName, localRecomposed,0); diff --git a/src/grib_accessor_class_g1step_range.c b/src/grib_accessor_class_g1step_range.c index 09540778b..4a00b13ef 100644 --- a/src/grib_accessor_class_g1step_range.c +++ b/src/grib_accessor_class_g1step_range.c @@ -614,7 +614,7 @@ static size_t string_length(grib_accessor* a) static int pack_long(grib_accessor* a, const long* val, size_t *len) { - char buff[100]; + char buff[256]; size_t bufflen=100; char sval[100] = {0}; char* p=sval; diff --git a/src/grib_accessor_class_g2_mars_labeling.c b/src/grib_accessor_class_g2_mars_labeling.c index 12cd4a8cf..62643499e 100644 --- a/src/grib_accessor_class_g2_mars_labeling.c +++ b/src/grib_accessor_class_g2_mars_labeling.c @@ -225,210 +225,211 @@ static int unpack_string(grib_accessor* a, char* val, size_t *len) static int extra_set(grib_accessor* a,long val) { -/*TODO chemicals*/ - int ret=0; - grib_accessor_g2_mars_labeling* self = (grib_accessor_g2_mars_labeling*)a; - char stepType[30]={0,}; - size_t stepTypelen=30; - long derivedForecast=-1; - long productDefinitionTemplateNumberNew=-1; - long productDefinitionTemplateNumber; - long typeOfProcessedData=-1; - long typeOfGeneratingProcess=-1; + /*TODO chemicals*/ + int ret=0; + grib_accessor_g2_mars_labeling* self = (grib_accessor_g2_mars_labeling*)a; + char stepType[30]={0,}; + size_t stepTypelen=30; + long derivedForecast=-1; + long productDefinitionTemplateNumberNew=-1; + long productDefinitionTemplateNumber; + long typeOfProcessedData=-1; + long typeOfGeneratingProcess=-1; - switch (self->index) { - case 0: - /* class */ - return ret; - break; - case 1: - /* type */ - switch (val) { - case 0: /* Unknown (0) */ - typeOfProcessedData=255; - typeOfGeneratingProcess=255; - break; - case 1: /* First guess (fg) */ - case 3: /* Initialised analysis (ia) */ - typeOfProcessedData=0; - typeOfGeneratingProcess=1; - break; - case 2: /* Analysis (an) */ - case 4: /* Oi analysis (oi) */ - case 5: /* 3d variational analysis (3v) */ - case 6: /* 4d variational analysis (4v) */ - case 7: /* 3d variational gradients (3g) */ - case 8: /* 4d variational gradients (4g) */ - typeOfProcessedData=0; - typeOfGeneratingProcess=0; - break; - case 9: /* Forecast (fc) */ - typeOfProcessedData=1; - typeOfGeneratingProcess=2; - break; - case 10: /* Control forecast (cf) */ - typeOfProcessedData=3; - typeOfGeneratingProcess=4; - break; - case 11: /* Perturbed forecast (pf) */ - typeOfProcessedData=4; - typeOfGeneratingProcess=4; - break; - case 12: /* Errors in first guess (ef) */ - case 13: /* Errors in analysis (ea) */ - typeOfProcessedData=255; - typeOfGeneratingProcess=7; - break; - case 14: /* Cluster means (cm) */ - case 15: /* Cluster std deviations (cs) */ - typeOfProcessedData=255; - typeOfGeneratingProcess=4; - break; - case 16: /* Forecast probability (fp) */ - typeOfProcessedData=8; - typeOfGeneratingProcess=5; - break; - case 17: /* Ensemble mean (em) */ - derivedForecast=0; - grib_get_string(grib_handle_of_accessor(a),self->stepType,stepType,&stepTypelen); - if (!strcmp(stepType,"instant")) { - productDefinitionTemplateNumberNew=2; - } else { - productDefinitionTemplateNumberNew=12; - } - typeOfProcessedData=255; - typeOfGeneratingProcess=4; - break; - case 18: /* Ensemble standard deviation (es) */ - derivedForecast=4; - grib_get_string(grib_handle_of_accessor(a),self->stepType,stepType,&stepTypelen); - if (!strcmp(stepType,"instant")) { - productDefinitionTemplateNumberNew=2; - } else { - productDefinitionTemplateNumberNew=12; - } - typeOfProcessedData=255; - typeOfGeneratingProcess=4; - break; - case 19: /* Forecast accumulation (fa) */ - case 20: /* Climatology (cl) */ - case 21: /* Climate simulation (si) */ - case 22: /* Climate 30 days simulation (s3) */ - case 23: /* Empirical distribution (ed) */ - case 24: /* Tubes (tu) */ - case 25: /* Flux forcing realtime (ff) */ - case 26: /* Ocean forward (of) */ - case 27: /* Extreme forecast index (efi) */ - case 28: /* Extreme forecast index control (efic)*/ - case 29: /* Probability boundaries (pb) */ - typeOfProcessedData=255; - typeOfGeneratingProcess=255; - break; - case 30: /* Event probability (ep) */ - typeOfProcessedData=8; - typeOfGeneratingProcess=5; - break; - case 31: /* Bias-corrected forecast (bf) */ - typeOfProcessedData=1; - typeOfGeneratingProcess=3; - break; - case 32: /* Climate distribution (cd) */ - case 33: /* 4D analysis increments (4i) */ - case 34: /* Gridded observations (go) */ - case 35: /* Model errors (me) */ - case 36: /* Probability distribution (pd) */ - case 37: /* Cluster information (ci) */ - case 38: /* Shift of Tail (sot) */ - case 40: /* Images (im) */ - case 42: /* Simulated images (sim) */ - typeOfProcessedData=255; - typeOfGeneratingProcess=255; - break; - case 43: /* Weighted ensemble mean (wem) */ - case 44: /* Weighted ensemble standard deviation (wes) */ - case 45: /* Cluster representative (cr) */ - case 46: /* Scaled ensemble standard deviation (ses) */ - case 47: /* Time average ensemble mean (taem) */ - case 48: /* Time average ensemble standard deviation (taes) */ - typeOfProcessedData=255; - typeOfGeneratingProcess=4; - break; - case 50: /* Sensitivity gradient (sg) */ - case 52: /* Sensitivity forecast (sf) */ - case 60: /* Perturbed analysis (pa) */ - case 61: /* Initial condition perturbation (icp) */ - case 62: /* Singular vector (sv) */ - case 63: /* Adjoint singular vector (as) */ - case 64: /* Signal variance (svar) */ - typeOfProcessedData=255; - typeOfGeneratingProcess=255; - break; - case 65: /* Calibration/Validation forecast (cv) */ - typeOfProcessedData=5; - typeOfGeneratingProcess=4; - break; - case 70: /* Ocean reanalysis (or) */ - case 71: /* Flux forcing (fx) */ - case 72: /* Fill-up (fu) */ - case 73: /* Simulation forced with observations (sfo) */ - case 80: /* Forecast mean (fcmean) */ - case 81: /* Forecast maximum (fcmax) */ - case 82: /* Forecast minimum (fcmin) */ - case 83: /* Forecast standard deviation (fcstdev) */ - case 86: /* Hindcast climate mean (hcmean) */ - case 87: /* Simulated satellite data */ - case 88: /* Gridded satellite data */ - case 89: /* GFAS analysis */ - typeOfProcessedData=255; - typeOfGeneratingProcess=255; - break; - case 84: /* Ensemble mean of temporal mean (emtm) */ - case 85: /* Ensemble standard deviation of temporal mean (estdtm) */ - typeOfProcessedData=255; - typeOfGeneratingProcess=4; - break; - default : - grib_context_log(a->context,GRIB_LOG_ERROR,"unknown mars.type %d",(int)val); - return GRIB_ENCODING_ERROR; - } - case 2: - /* stream */ - switch (val) { - case 1030: /* enda */ - case 1249: /* elda */ - case 1250: /* ewla */ - grib_get_string(grib_handle_of_accessor(a),self->stepType,stepType,&stepTypelen); - if (!strcmp(stepType,"instant")) { - productDefinitionTemplateNumberNew=1; - } else { - productDefinitionTemplateNumberNew=11; - } - break; - } - break; - default : - grib_context_log(a->context,GRIB_LOG_ERROR, - "invalid first argument of g2_mars_labeling in %s",a->name); - return GRIB_INTERNAL_ERROR; - break; - } + switch (self->index) { + case 0: + /* class */ + return ret; + break; + case 1: + /* type */ + switch (val) { + case 0: /* Unknown (0) */ + typeOfProcessedData=255; + typeOfGeneratingProcess=255; + break; + case 1: /* First guess (fg) */ + case 3: /* Initialised analysis (ia) */ + typeOfProcessedData=0; + typeOfGeneratingProcess=1; + break; + case 2: /* Analysis (an) */ + case 4: /* Oi analysis (oi) */ + case 5: /* 3d variational analysis (3v) */ + case 6: /* 4d variational analysis (4v) */ + case 7: /* 3d variational gradients (3g) */ + case 8: /* 4d variational gradients (4g) */ + typeOfProcessedData=0; + typeOfGeneratingProcess=0; + break; + case 9: /* Forecast (fc) */ + typeOfProcessedData=1; + typeOfGeneratingProcess=2; + break; + case 10: /* Control forecast (cf) */ + typeOfProcessedData=3; + typeOfGeneratingProcess=4; + break; + case 11: /* Perturbed forecast (pf) */ + typeOfProcessedData=4; + typeOfGeneratingProcess=4; + break; + case 12: /* Errors in first guess (ef) */ + case 13: /* Errors in analysis (ea) */ + typeOfProcessedData=255; + typeOfGeneratingProcess=7; + break; + case 14: /* Cluster means (cm) */ + case 15: /* Cluster std deviations (cs) */ + typeOfProcessedData=255; + typeOfGeneratingProcess=4; + break; + case 16: /* Forecast probability (fp) */ + typeOfProcessedData=8; + typeOfGeneratingProcess=5; + break; + case 17: /* Ensemble mean (em) */ + derivedForecast=0; + grib_get_string(grib_handle_of_accessor(a),self->stepType,stepType,&stepTypelen); + if (!strcmp(stepType,"instant")) { + productDefinitionTemplateNumberNew=2; + } else { + productDefinitionTemplateNumberNew=12; + } + typeOfProcessedData=255; + typeOfGeneratingProcess=4; + break; + case 18: /* Ensemble standard deviation (es) */ + derivedForecast=4; + grib_get_string(grib_handle_of_accessor(a),self->stepType,stepType,&stepTypelen); + if (!strcmp(stepType,"instant")) { + productDefinitionTemplateNumberNew=2; + } else { + productDefinitionTemplateNumberNew=12; + } + typeOfProcessedData=255; + typeOfGeneratingProcess=4; + break; + case 19: /* Forecast accumulation (fa) */ + case 20: /* Climatology (cl) */ + case 21: /* Climate simulation (si) */ + case 22: /* Climate 30 days simulation (s3) */ + case 23: /* Empirical distribution (ed) */ + case 24: /* Tubes (tu) */ + case 25: /* Flux forcing realtime (ff) */ + case 26: /* Ocean forward (of) */ + case 27: /* Extreme forecast index (efi) */ + case 28: /* Extreme forecast index control (efic)*/ + case 29: /* Probability boundaries (pb) */ + typeOfProcessedData=255; + typeOfGeneratingProcess=255; + break; + case 30: /* Event probability (ep) */ + typeOfProcessedData=8; + typeOfGeneratingProcess=5; + break; + case 31: /* Bias-corrected forecast (bf) */ + typeOfProcessedData=1; + typeOfGeneratingProcess=3; + break; + case 32: /* Climate distribution (cd) */ + case 33: /* 4D analysis increments (4i) */ + case 34: /* Gridded observations (go) */ + case 35: /* Model errors (me) */ + case 36: /* Probability distribution (pd) */ + case 37: /* Cluster information (ci) */ + case 38: /* Shift of Tail (sot) */ + case 40: /* Images (im) */ + case 42: /* Simulated images (sim) */ + typeOfProcessedData=255; + typeOfGeneratingProcess=255; + break; + case 43: /* Weighted ensemble mean (wem) */ + case 44: /* Weighted ensemble standard deviation (wes) */ + case 45: /* Cluster representative (cr) */ + case 46: /* Scaled ensemble standard deviation (ses) */ + case 47: /* Time average ensemble mean (taem) */ + case 48: /* Time average ensemble standard deviation (taes) */ + typeOfProcessedData=255; + typeOfGeneratingProcess=4; + break; + case 50: /* Sensitivity gradient (sg) */ + case 52: /* Sensitivity forecast (sf) */ + case 60: /* Perturbed analysis (pa) */ + case 61: /* Initial condition perturbation (icp) */ + case 62: /* Singular vector (sv) */ + case 63: /* Adjoint singular vector (as) */ + case 64: /* Signal variance (svar) */ + typeOfProcessedData=255; + typeOfGeneratingProcess=255; + break; + case 65: /* Calibration/Validation forecast (cv) */ + typeOfProcessedData=5; + typeOfGeneratingProcess=4; + break; + case 70: /* Ocean reanalysis (or) */ + case 71: /* Flux forcing (fx) */ + case 72: /* Fill-up (fu) */ + case 73: /* Simulation forced with observations (sfo) */ + case 80: /* Forecast mean (fcmean) */ + case 81: /* Forecast maximum (fcmax) */ + case 82: /* Forecast minimum (fcmin) */ + case 83: /* Forecast standard deviation (fcstdev) */ + case 86: /* Hindcast climate mean (hcmean) */ + case 87: /* Simulated satellite data */ + case 88: /* Gridded satellite data */ + case 89: /* GFAS analysis */ + typeOfProcessedData=255; + typeOfGeneratingProcess=255; + break; + case 84: /* Ensemble mean of temporal mean (emtm) */ + case 85: /* Ensemble standard deviation of temporal mean (estdtm) */ + typeOfProcessedData=255; + typeOfGeneratingProcess=4; + break; + default : + grib_context_log(a->context,GRIB_LOG_ERROR,"unknown mars.type %d",(int)val); + return GRIB_ENCODING_ERROR; + } + break; + case 2: + /* stream */ + switch (val) { + case 1030: /* enda */ + case 1249: /* elda */ + case 1250: /* ewla */ + grib_get_string(grib_handle_of_accessor(a),self->stepType,stepType,&stepTypelen); + if (!strcmp(stepType,"instant")) { + productDefinitionTemplateNumberNew=1; + } else { + productDefinitionTemplateNumberNew=11; + } + break; + } + break; + default : + grib_context_log(a->context,GRIB_LOG_ERROR, + "invalid first argument of g2_mars_labeling in %s",a->name); + return GRIB_INTERNAL_ERROR; + break; + } - if (productDefinitionTemplateNumberNew>=0) { - grib_get_long(grib_handle_of_accessor(a),self->productDefinitionTemplateNumber,&productDefinitionTemplateNumber); - if (productDefinitionTemplateNumber!=productDefinitionTemplateNumberNew) - grib_set_long(grib_handle_of_accessor(a),self->productDefinitionTemplateNumber,productDefinitionTemplateNumberNew); - } + if (productDefinitionTemplateNumberNew>=0) { + grib_get_long(grib_handle_of_accessor(a),self->productDefinitionTemplateNumber,&productDefinitionTemplateNumber); + if (productDefinitionTemplateNumber!=productDefinitionTemplateNumberNew) + grib_set_long(grib_handle_of_accessor(a),self->productDefinitionTemplateNumber,productDefinitionTemplateNumberNew); + } - if (derivedForecast>=0) { - grib_set_long(grib_handle_of_accessor(a),self->derivedForecast,derivedForecast); - } + if (derivedForecast>=0) { + grib_set_long(grib_handle_of_accessor(a),self->derivedForecast,derivedForecast); + } - if (typeOfProcessedData>0) - grib_set_long(grib_handle_of_accessor(a),self->typeOfProcessedData,typeOfProcessedData); - if (typeOfGeneratingProcess>0) - grib_set_long(grib_handle_of_accessor(a),self->typeOfGeneratingProcess,typeOfGeneratingProcess); + if (typeOfProcessedData>0) + grib_set_long(grib_handle_of_accessor(a),self->typeOfProcessedData,typeOfProcessedData); + if (typeOfGeneratingProcess>0) + grib_set_long(grib_handle_of_accessor(a),self->typeOfGeneratingProcess,typeOfGeneratingProcess); - return ret; + return ret; } static int pack_string(grib_accessor* a, const char* val, size_t *len) diff --git a/src/grib_accessor_class_lookup.c b/src/grib_accessor_class_lookup.c index f98ab513e..f5d07cfeb 100644 --- a/src/grib_accessor_class_lookup.c +++ b/src/grib_accessor_class_lookup.c @@ -162,7 +162,7 @@ static void dump(grib_accessor* a,grib_dumper* dumper) grib_accessor_lookup* self = (grib_accessor_lookup*)a; unsigned char bytes[1024] = {0,}; char msg[1024]= {0,}; - char buf[1024]; + char buf[2048]; int i; unsigned long v = 0; diff --git a/src/grib_accessor_class_smart_table.c b/src/grib_accessor_class_smart_table.c index 54bf927c4..823d4472d 100644 --- a/src/grib_accessor_class_smart_table.c +++ b/src/grib_accessor_class_smart_table.c @@ -219,17 +219,17 @@ static grib_smart_table* load_table(grib_accessor_smart_table* self) grib_smart_table* t = NULL; grib_smart_table* next=NULL ; char *filename=0; - char name[1024]={0,}; + char name[2048]={0,}; char recomposed[1024]={0,}; char localRecomposed[1024]={0,}; char *localFilename=0; char extraRecomposed[1024]={0,}; char *extraFilename=0; - char localName[1024]={0,}; + char localName[2048]={0,}; char masterDir[1024]={0,}; char localDir[1024]={0,}; char extraDir[1024]={0,}; - char extraTable[1024]={0,}; + char extraTable[2048]={0,}; size_t len=1024; if (self->masterDir != NULL) { diff --git a/src/grib_accessor_class_sprintf.c b/src/grib_accessor_class_sprintf.c index 23a1af4a2..be279edab 100644 --- a/src/grib_accessor_class_sprintf.c +++ b/src/grib_accessor_class_sprintf.c @@ -145,7 +145,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t *len) { grib_accessor_sprintf* self = (grib_accessor_sprintf*)a; char result[1024] ; - char tempBuffer[1024]; + char tempBuffer[2048]; char sres[1024] ; long ires = 0; double dres= 0; diff --git a/tools/grib_merge.c b/tools/grib_merge.c index 8b941659e..34a1a0ade 100644 --- a/tools/grib_merge.c +++ b/tools/grib_merge.c @@ -306,7 +306,7 @@ int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h) int err=0; grib_handle* hm=0; char md5[200]={0,}; - char fname[210]={0,}; + char fname[256]={0,}; size_t lmd5=32; if (!hh) { hh=grib_handle_clone(h); return 0; }