ECC-1489: Crash on MacOS (release build): printing the tool man page

This commit is contained in:
Shahram Najm 2022-12-05 15:34:35 +00:00
parent da401ba7b8
commit fe1f32d9bc
2 changed files with 18 additions and 7 deletions

View File

@ -16,6 +16,13 @@ rm -f $tempLog
cd ${data_dir}
infile=regular_gaussian_model_level.grib1
# Basic test of man page
set +e
${tools_dir}/grib_ls > /dev/null
status=$?
set -e
[ $status -eq 1 ]
${tools_dir}/grib_ls -P count $infile > $tempLog
${tools_dir}/grib_ls -p count,step $infile >> $tempLog
${tools_dir}/grib_ls $infile >> $tempLog

View File

@ -125,11 +125,12 @@ void usage(void)
printf("USAGE \n\t%s %s\n\n", tool_name, tool_usage);
printf("OPTIONS\n");
for (i = 0; i < grib_options_count; i++) {
if (grib_options[i].command_line)
if (grib_options[i].command_line) {
printf("\t-%c %s\t%s", grib_options[i].id[0],
grib_options_get_args(grib_options[i].id),
grib_options_get_help(grib_options[i].id));
}
}
printf("\n\n");
exit(1);
}
@ -406,8 +407,9 @@ const char* grib_options_get_help(const char* id)
{
int i = 0;
char msg[] = "ERROR: help not found for option ";
char* err = (char*)calloc(1, sizeof(msg) + 3);
snprintf(err, 1024, "%s%c\n", msg, *id);
const size_t msize = sizeof(msg) + 3;
char* err = (char*)calloc(1, msize);
snprintf(err, msize, "%s%c\n", msg, *id);
for (i = 0; i < grib_options_count; i++) {
if (!strcmp(id, grib_options[i].id)) {
if (grib_options[i].help != NULL)
@ -418,7 +420,7 @@ const char* grib_options_get_help(const char* id)
}
for (i = 0; i < grib_options_help_count; i++) {
if (!strcmp(id, grib_options_help_list[i].id)) {
return grib_options_help_list[i].help != NULL ? (char*)grib_options_help_list[i].help : err;
return grib_options_help_list[i].help != NULL ? grib_options_help_list[i].help : err;
}
}
return err;
@ -429,11 +431,12 @@ const char* grib_options_get_args(const char* id)
int i = 0;
char empty[] = "";
char msg[] = "ERROR: help not found for option -";
const size_t msize = sizeof(msg) + 3;
char* err = NULL;
if (id[1] != ':')
return strdup(empty);
err = (char*)calloc(1, sizeof(msg) + 3);
snprintf(err, 1024, "%s%c\n", msg, *id);
err = (char*)calloc(1, msize);
snprintf(err, msize, "%s%c\n", msg, *id);
for (i = 0; i < grib_options_count; i++) {
if (!strcmp(id, grib_options[i].id)) {
if (grib_options[i].args != NULL) {
@ -445,11 +448,12 @@ const char* grib_options_get_args(const char* id)
}
}
}
for (i = 0; i < grib_options_help_count; i++) {
if (!strcmp(id, grib_options_help_list[i].id)) {
if (grib_options_help_list[i].args != NULL) {
free(err);
return (char*)grib_options_help_list[i].args;
return grib_options_help_list[i].args;
}
else {
return err;