Skip to content

Commit

Permalink
Merge pull request #84 in MONITOR/naemon from bugfix/MON-10862-naemon…
Browse files Browse the repository at this point in the history
…-doesnt-compile-with-gcc7 to master

* commit 'b73ef00e50ca33e6c63116bcc7d0ed5beaba9675':
  Check snprintf for encoding errors and truncation
  Mark intentional fallthroughs in switch statements
  • Loading branch information
Robin Engstrom committed Apr 4, 2018
2 parents cae8193 + b73ef00 commit 7e74c38
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/runcmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ int runcmd_cmd2strv(const char *str, int *out_argc, char **out_argv)
set_state(STATE_INSQ | STATE_INARG);
continue;
}
/* FALLTHROUGH */
case '"':
if (have_state(STATE_INSQ))
break;
Expand Down
2 changes: 2 additions & 0 deletions lib/snprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
break;
case 'X':
cnk->flags |= DP_F_UP;
/* FALLTHROUGH */
case 'x':
cnk->type = CNK_HEX;
cnk->flags |= DP_F_UNSIGNED;
Expand All @@ -503,6 +504,7 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
case 'G':
case 'F':
cnk->flags |= DP_F_UP;
/* FALLTHROUGH */
case 'a':
/* hex float not supported yet */
case 'e':
Expand Down
22 changes: 18 additions & 4 deletions src/naemon/checks.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,28 @@ int process_check_result_queue(char *dirname)
break;
}

/* create /path/to/file */
snprintf(file, sizeof(file), "%s/%s", dirname, dirfile->d_name);
file[sizeof(file) - 1] = '\x0';

/* process this if it's a check result file... */
x = strlen(dirfile->d_name);
if (x == 7 && dirfile->d_name[0] == 'c') {

/* create /path/to/file */
int written_size = snprintf(file, sizeof(file), "%s/%s", dirname, dirfile->d_name);
file[sizeof(file) - 1] = '\x0';

/* Check for encoding errors */
if (written_size < 0) {
nm_log(NSLOG_RUNTIME_WARNING,
"Warning: encoding error on check result file path '`%s'.\n", file);
continue;
}

/* Check if the filename was truncated */
if (written_size > 0 && (size_t)written_size >= sizeof(file)) {
nm_log(NSLOG_RUNTIME_WARNING,
"Warning: truncated path to check result file '%s'.\n", file);
continue;
}

if (stat(file, &stat_buf) == -1) {
nm_log(NSLOG_RUNTIME_WARNING,
"Warning: Could not stat() check result file '%s'.\n", file);
Expand Down
17 changes: 16 additions & 1 deletion src/naemon/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,7 @@ read_config_file(const char *main_config_file, nagios_macros *mac)
break;
} else {
while ((dirfile = readdir(dirp)) != NULL) {
int written_size;
char file[MAX_FILENAME_LENGTH];

/* skip hidden files and directories, current and parent dir, and non-config files */
Expand All @@ -1022,9 +1023,23 @@ read_config_file(const char *main_config_file, nagios_macros *mac)
continue;

/* create /path/to/file */
snprintf(file, sizeof(file), "%s/%s", include_dir, dirfile->d_name);
written_size = snprintf(file, sizeof(file), "%s/%s", include_dir, dirfile->d_name);
file[sizeof(file) - 1] = '\x0';

/* Check for encoding errors */
if (written_size < 0) {
nm_log(NSLOG_RUNTIME_WARNING,
"Warning: encoding error on config file path '`%s'.\n", file);
continue;
}

/* Check if the filename was truncated. */
if (written_size > 0 && (size_t)written_size >= sizeof(file)) {
nm_log(NSLOG_RUNTIME_WARNING,
"Warning: truncated path to config file '%s'.\n", file);
continue;
}

error |= read_config_file(file, mac);
}
closedir(dirp);
Expand Down
6 changes: 6 additions & 0 deletions src/naemon/macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,7 @@ static int grab_macrox_value_r(nagios_macros *mac, int macro_type, char *arg1, c
/***************/
case MACRO_HOSTGROUPNAMES:
*free_macro = TRUE;
/* FALLTHROUGH */
case MACRO_HOSTNAME:
case MACRO_HOSTALIAS:
case MACRO_HOSTADDRESS:
Expand Down Expand Up @@ -1612,6 +1613,7 @@ static int grab_macrox_value_r(nagios_macros *mac, int macro_type, char *arg1, c
/********************/
case MACRO_HOSTGROUPMEMBERS:
*free_macro = TRUE;
/* FALLTHROUGH */
case MACRO_HOSTGROUPNAME:
case MACRO_HOSTGROUPALIAS:
case MACRO_HOSTGROUPNOTES:
Expand Down Expand Up @@ -1640,6 +1642,7 @@ static int grab_macrox_value_r(nagios_macros *mac, int macro_type, char *arg1, c
/******************/
case MACRO_SERVICEGROUPNAMES:
*free_macro = TRUE;
/* FALLTHROUGH */
case MACRO_SERVICEDESC:
case MACRO_SERVICESTATE:
case MACRO_SERVICESTATEID:
Expand Down Expand Up @@ -1757,6 +1760,7 @@ static int grab_macrox_value_r(nagios_macros *mac, int macro_type, char *arg1, c
case MACRO_SERVICEGROUPNOTESURL:
case MACRO_SERVICEGROUPACTIONURL:
*free_macro = TRUE;
/* FALLTHROUGH */
case MACRO_SERVICEGROUPNAME:
case MACRO_SERVICEGROUPALIAS:
/* a standard servicegroup macro */
Expand All @@ -1781,6 +1785,7 @@ static int grab_macrox_value_r(nagios_macros *mac, int macro_type, char *arg1, c
/******************/
case MACRO_CONTACTGROUPNAMES:
*free_macro = TRUE;
/* FALLTHROUGH */
case MACRO_CONTACTNAME:
case MACRO_CONTACTALIAS:
case MACRO_CONTACTEMAIL:
Expand Down Expand Up @@ -1841,6 +1846,7 @@ static int grab_macrox_value_r(nagios_macros *mac, int macro_type, char *arg1, c
/***********************/
case MACRO_CONTACTGROUPMEMBERS:
*free_macro = TRUE;
/* FALLTHROUGH */
case MACRO_CONTACTGROUPNAME:
case MACRO_CONTACTGROUPALIAS:
/* a standard contactgroup macro */
Expand Down
17 changes: 16 additions & 1 deletion src/naemon/xodtemplate.c
Original file line number Diff line number Diff line change
Expand Up @@ -8456,15 +8456,30 @@ static int xodtemplate_process_config_dir(char *dir_name)

/* process all files in the directory... */
while ((dirfile = readdir(dirp)) != NULL) {
int written_size;

/* skip hidden files and directories, and current and parent dir */
if (dirfile->d_name[0] == '.')
continue;

/* create /path/to/file */
snprintf(file, sizeof(file), "%s/%s", dir_name, dirfile->d_name);
written_size = snprintf(file, sizeof(file), "%s/%s", dir_name, dirfile->d_name);
file[sizeof(file) - 1] = '\x0';

/* Check for encoding errors */
if (written_size < 0) {
nm_log(NSLOG_RUNTIME_WARNING,
"Warning: xodtemplate encoding error on config file path '`%s'.\n", file);
continue;
}

/* Check if the filename was truncated. */
if (written_size > 0 && (size_t)written_size >= sizeof(file)) {
nm_log(NSLOG_RUNTIME_WARNING,
"Warning: xodtemplate truncated path to config file '`%s'.\n", file);
continue;
}

/* process this if it's a non-hidden config file... */
if (stat(file, &stat_buf) == -1) {
nm_log(NSLOG_RUNTIME_ERROR, "Error: Could not open config directory member '%s' for reading.\n", file);
Expand Down

0 comments on commit 7e74c38

Please sign in to comment.