ECC-991: Strict mode added

This commit is contained in:
Shahram Najm 2019-11-18 15:40:14 +00:00
parent 4d858df125
commit 54ba4cf72d
4 changed files with 19 additions and 6 deletions

View File

@ -548,7 +548,7 @@ static int bufr_decode_header(grib_context* c, const void* message, off_t offset
return err;
}
static int count_bufr_messages(grib_context* c, FILE* f, int* n)
static int count_bufr_messages(grib_context* c, FILE* f, int* n, int strict_mode)
{
int err=0;
void* mesg=NULL;
@ -567,6 +567,7 @@ static int count_bufr_messages(grib_context* c, FILE* f, int* n)
done = 1; /* reached the end */
break;
}
if (strict_mode) return GRIB_DECODING_ERROR;
}
if (mesg && !err) {
grib_context_free(c,mesg);
@ -578,7 +579,7 @@ static int count_bufr_messages(grib_context* c, FILE* f, int* n)
return err;
}
int codes_bufr_extract_headers_malloc(grib_context* c, const char* filename, codes_bufr_header** result, int* num_messages)
int codes_bufr_extract_headers_malloc(grib_context* c, const char* filename, codes_bufr_header** result, int* num_messages, int strict_mode)
{
int err = 0, i = 0;
FILE* fp = NULL;
@ -593,7 +594,7 @@ int codes_bufr_extract_headers_malloc(grib_context* c, const char* filename, cod
perror(filename);
return GRIB_IO_PROBLEM;
}
err = count_bufr_messages(c, fp, num_messages);
err = count_bufr_messages(c, fp, num_messages, strict_mode);
if (err) {
grib_context_log(c, GRIB_LOG_ERROR, "codes_bufr_extract_headers_malloc: Unable to count BUFR messages in file \"%s\"", filename);
fclose(fp);
@ -622,10 +623,20 @@ int codes_bufr_extract_headers_malloc(grib_context* c, const char* filename, cod
}
grib_context_free(c, mesg);
}
if (mesg && err) {
if (strict_mode) {
fclose(fp);
return GRIB_DECODING_ERROR;
}
}
if (!mesg) {
if (err != GRIB_END_OF_FILE && err != GRIB_PREMATURE_END_OF_FILE) {
/* An error occurred */
grib_context_log(c, GRIB_LOG_ERROR, "codes_bufr_extract_headers_malloc: Unable to read BUFR message");
if (strict_mode) {
fclose(fp);
return GRIB_DECODING_ERROR;
}
}
}
++i;

View File

@ -1267,9 +1267,10 @@ codes_handle *codes_grib_util_set_spec(codes_handle *h,
* result = array of 'codes_bufr_header' structs with 'num_messages' elements.
* This array should be freed by the caller.
* num_messages = number of messages found in the input file.
* strict = If 1 means fail if any message is invalid.
* returns 0 if OK, integer value on error.
*/
int codes_bufr_extract_headers_malloc(codes_context* c, const char* filename, codes_bufr_header** result, int* num_messages);
int codes_bufr_extract_headers_malloc(codes_context* c, const char* filename, codes_bufr_header** result, int* num_messages, int strict_mode);
/* --------------------------------------- */
#ifdef __cplusplus

View File

@ -1485,7 +1485,7 @@ size_t sum_of_pl_array(const long* pl, size_t plsize);
int compute_bufr_key_rank(grib_handle *h, grib_string_list *keys, const char *key);
char **codes_bufr_copy_data_return_copied_keys(grib_handle *hin, grib_handle *hout, size_t *nkeys, int *err);
int codes_bufr_copy_data(grib_handle *hin, grib_handle *hout);
int codes_bufr_extract_headers_malloc(grib_context* c, const char* filename, codes_bufr_header** result, int* num_messages);
int codes_bufr_extract_headers_malloc(grib_context* c, const char* filename, codes_bufr_header** result, int* num_messages, int strict_mode);
/* string_util.c */

View File

@ -39,13 +39,14 @@ int main(int argc, char* argv[])
int num_messages = 0;
codes_bufr_header* header_array = NULL;
codes_context* c = codes_context_get_default();
const int strict_mode = 1;
if (argc != 3) return 1;
keys = argv[1]; /* comma-separated like bufr_ls/bufr_get */
filename = argv[2];
err = codes_bufr_extract_headers_malloc(c, filename, &header_array, &num_messages);
err = codes_bufr_extract_headers_malloc(c, filename, &header_array, &num_messages, strict_mode);
if (err) {
printf("ERROR: %s\n",grib_get_error_message(err));
return 1;