Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ test_python: python
env TEST_NAME=Test/test_cmor_frequency_required.py make test_a_python
env TEST_NAME=Test/test_cmor_parent_attrs.py make test_a_python
env TEST_NAME=Test/test_cmor_variable_attr_comment.py make test_a_python
env TEST_NAME=Test/test_cmor_cv_string_too_long.py make test_a_python
test_cmip6_cv: python
env TEST_NAME=Test/test_python_CMIP6_CV_sub_experimentnotset.py make test_a_python
env TEST_NAME=Test/test_python_CMIP6_CV_sub_experimentbad.py make test_a_python
Expand Down
67 changes: 26 additions & 41 deletions Src/cmor.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ int cmor_have_NetCDF41min(void)
}

/************************************************************************/
/* cmor_handle_error_internal() */
/* cmor_handle_error_internal() */
/************************************************************************/
void cmor_handle_error_internal(char *error_msg, int level)
{
Expand All @@ -732,83 +732,68 @@ void cmor_handle_error_internal(char *error_msg, int level)
if (output_logfile == NULL)
output_logfile = stderr;

#ifdef COLOREDOUTPUT
int use_color = isatty(fileno(output_logfile));
#define ANSI_COLOR(f, ...) do { if (use_color) fprintf(f, __VA_ARGS__); } while(0)
#else
#define ANSI_COLOR(f, ...) do {} while(0)
#endif

if (CMOR_VERBOSITY != CMOR_QUIET) {
fprintf(output_logfile, "\n");
}

if (level == CMOR_WARNING) {
cmor_nwarnings++;
if (CMOR_VERBOSITY != CMOR_QUIET) {

#ifdef COLOREDOUTPUT
fprintf(output_logfile, "%c[%d;%d;%dm", 0X1B, 2, 34, 47);
#endif

ANSI_COLOR(output_logfile, "%c[%d;%d;%dm", 0x1B, 2, 34, 47);
fprintf(output_logfile, "C Traceback:\nIn function: %s",
cmor_traceback_info);

#ifdef COLOREDOUTPUT
fprintf(output_logfile, "%c[%dm", 0X1B, 0);
#endif

ANSI_COLOR(output_logfile, "%c[%dm", 0x1B, 0);
fprintf(output_logfile, "\n\n");

#ifdef COLOREDOUTPUT
fprintf(output_logfile, "%c[%d;%d;%dm", 0X1B, 1, 34, 47);
#endif
}
} else {
cmor_nerrors++;

#ifdef COLOREDOUTPUT
fprintf(output_logfile, "%c[%d;%d;%dm", 0X1B, 2, 31, 47);
#endif

ANSI_COLOR(output_logfile, "%c[%d;%d;%dm", 0x1B, 2, 31, 47);
fprintf(output_logfile, "C Traceback:\n! In function: %s",
cmor_traceback_info);

#ifdef COLOREDOUTPUT
fprintf(output_logfile, "%c[%dm", 0X1B, 0);
#endif

ANSI_COLOR(output_logfile, "%c[%dm", 0x1B, 0);
fprintf(output_logfile, "\n\n");

#ifdef COLOREDOUTPUT
fprintf(output_logfile, "%c[%d;%d;%dm", 0X1B, 1, 31, 47);
#endif
}
// fprintf(stderr, "%s ERROR LEVEL %d\n", error_msg, level);

if (CMOR_VERBOSITY != CMOR_QUIET || level != CMOR_WARNING) {
for (i = 0; i < 25; i++) {
fprintf(output_logfile, "!");
/* Color the entire box: borders and message in one region */
if (level == CMOR_WARNING) {
ANSI_COLOR(output_logfile, "%c[%d;%d;%dm", 0x1B, 1, 34, 47);
} else {
ANSI_COLOR(output_logfile, "%c[%d;%d;%dm", 0x1B, 1, 31, 47);
}

for (i = 0; i < 25; i++)
fprintf(output_logfile, "!");
fprintf(output_logfile, "\n");
fprintf(output_logfile, "!\n");

if (level == CMOR_WARNING)
fprintf(output_logfile, "! Warning: %s\n", error_msg);
else
fprintf(output_logfile, "! Error: %s\n", error_msg);

fprintf(output_logfile, "!\n");

for (i = 0; i < 25; i++)
fprintf(output_logfile, "!");

#ifdef COLOREDOUTPUT
fprintf(output_logfile, "%c[%dm", 0X1B, 0);
#endif

ANSI_COLOR(output_logfile, "%c[%dm", 0x1B, 0);
fprintf(output_logfile, "\n\n");
}

CV_ERROR = 1;
if (level == CMOR_NOT_SETUP) {
exit(1);

}
if ((CMOR_MODE == CMOR_EXIT_ON_WARNING) || (level == CMOR_CRITICAL)) {
fflush(stdout);
fflush(output_logfile);
fflush(stdout);
fflush(output_logfile);
kill(getpid(), SIGTERM);
}
fflush(output_logfile);
Expand Down
76 changes: 71 additions & 5 deletions Src/cmor_tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,10 @@ int cmor_load_table_internal(char szTable[CMOR_MAX_STRING], int *table_id,
done = 1;
} else if (strncmp(key, JSON_KEY_CV_ENTRY, 2) == 0) {

cmor_validate_cv(value, NULL);
if (cmor_validate_cv(value, NULL) == TABLE_ERROR) {
cmor_pop_traceback();
return (TABLE_ERROR);
}

if (cmor_CV_set_entry(&cmor_tables[cmor_ntables], value) == 1) {
cmor_pop_traceback();
Expand Down Expand Up @@ -1203,18 +1206,53 @@ int cmor_validate_json(json_object *json)
/************************************************************************/
/* cmor_validate_cv() */
/************************************************************************/
void cmor_validate_cv(json_object *cv, char *parent_attr)
int cmor_validate_cv(json_object *cv, char *parent_attr)
{
array_list *array;
json_object *array_obj;
size_t length, i;
size_t length, str_len, i;
int single_value_pairs;

size_t partial_len = 25;
char partial_str[partial_len];

cmor_add_traceback("cmor_validate_cv");

json_object_object_foreach(cv, attr, value) {
single_value_pairs = 0;

// String values must be 1023 or less characters long
str_len = strlen(attr);
if (str_len >= CMOR_MAX_STRING) {
strncpy(partial_str, attr, partial_len);
partial_str[partial_len - 1] = '\0';
cmor_handle_error_variadic(
"Attribute \"%s...\" has a length of %d characters, "
"which exceeds the %d character limit.",
CMOR_CRITICAL,
partial_str, str_len, CMOR_MAX_STRING - 1);
cmor_pop_traceback();
return TABLE_ERROR;
}

if (json_object_is_type(value, json_type_string)) {
str_len = json_object_get_string_len(value);
if (str_len >= CMOR_MAX_STRING) {
strncpy(partial_str,
json_object_get_string(value),
partial_len);
partial_str[partial_len - 1] = '\0';
cmor_handle_error_variadic(
"Attribute \"%s\" has value \"%s...\" "
"with a length of %d characters, which "
"exceeds the %d character limit.",
CMOR_CRITICAL,
attr, partial_str, str_len, CMOR_MAX_STRING - 1);
cmor_pop_traceback();
return TABLE_ERROR;
}
}

if (parent_attr == NULL) {
if (strcmp(attr, CV_KEY_BRANDING_TEMPLATE) == 0) {
if (!json_object_is_type(value, json_type_string)) {
Expand Down Expand Up @@ -1334,11 +1372,39 @@ void cmor_validate_cv(json_object *cv, char *parent_attr)
CMOR_WARNING,
attr);
break;
} else {
str_len = json_object_get_string_len(array_obj);
if (str_len >= CMOR_MAX_STRING) {
strncpy(partial_str,
json_object_get_string(array_obj),
partial_len);
partial_str[partial_len - 1] = '\0';
cmor_handle_error_variadic(
"Attribute \"%s\" has value \"%s...\" in its "
"array with a length of %d characters, which "
"exceeds the %d character limit.",
CMOR_CRITICAL,
attr, partial_str, str_len, CMOR_MAX_STRING - 1);
cmor_pop_traceback();
return TABLE_ERROR;
}
}
}
} else if (json_object_is_type(value, json_type_object)) {
json_object_object_foreach(value, k, v) {
if (json_object_is_type(v, json_type_array)) {
str_len = strlen(k);
if (str_len >= CMOR_MAX_STRING) {
strncpy(partial_str, k, partial_len);
partial_str[partial_len - 1] = '\0';
cmor_handle_error_variadic(
"Key value \"%s...\" in attribute \"%s\" "
"has a length of %d characters, which "
"exceeds the %d character limit.",
CMOR_CRITICAL,
partial_str, attr, str_len, CMOR_MAX_STRING - 1);
cmor_pop_traceback();
return TABLE_ERROR;
} else if (json_object_is_type(v, json_type_array)) {
cmor_handle_error_variadic(
"Value for \"%s\" in attribute \"%s\" "
"cannot be an array",
Expand All @@ -1360,5 +1426,5 @@ void cmor_validate_cv(json_object *cv, char *parent_attr)
}

cmor_pop_traceback();
return;
return TABLE_SUCCESS;
}
Loading
Loading