Skip to content

Commit 7e74c38

Browse files
author
Robin Engstrom
committed
Merge pull request #84 in MONITOR/naemon from bugfix/MON-10862-naemon-doesnt-compile-with-gcc7 to master
* commit 'b73ef00e50ca33e6c63116bcc7d0ed5beaba9675': Check snprintf for encoding errors and truncation Mark intentional fallthroughs in switch statements
2 parents cae8193 + b73ef00 commit 7e74c38

File tree

6 files changed

+59
-6
lines changed

6 files changed

+59
-6
lines changed

lib/runcmd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ int runcmd_cmd2strv(const char *str, int *out_argc, char **out_argv)
199199
set_state(STATE_INSQ | STATE_INARG);
200200
continue;
201201
}
202+
/* FALLTHROUGH */
202203
case '"':
203204
if (have_state(STATE_INSQ))
204205
break;

lib/snprintf.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
493493
break;
494494
case 'X':
495495
cnk->flags |= DP_F_UP;
496+
/* FALLTHROUGH */
496497
case 'x':
497498
cnk->type = CNK_HEX;
498499
cnk->flags |= DP_F_UNSIGNED;
@@ -503,6 +504,7 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
503504
case 'G':
504505
case 'F':
505506
cnk->flags |= DP_F_UP;
507+
/* FALLTHROUGH */
506508
case 'a':
507509
/* hex float not supported yet */
508510
case 'e':

src/naemon/checks.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,28 @@ int process_check_result_queue(char *dirname)
239239
break;
240240
}
241241

242-
/* create /path/to/file */
243-
snprintf(file, sizeof(file), "%s/%s", dirname, dirfile->d_name);
244-
file[sizeof(file) - 1] = '\x0';
245-
246242
/* process this if it's a check result file... */
247243
x = strlen(dirfile->d_name);
248244
if (x == 7 && dirfile->d_name[0] == 'c') {
249245

246+
/* create /path/to/file */
247+
int written_size = snprintf(file, sizeof(file), "%s/%s", dirname, dirfile->d_name);
248+
file[sizeof(file) - 1] = '\x0';
249+
250+
/* Check for encoding errors */
251+
if (written_size < 0) {
252+
nm_log(NSLOG_RUNTIME_WARNING,
253+
"Warning: encoding error on check result file path '`%s'.\n", file);
254+
continue;
255+
}
256+
257+
/* Check if the filename was truncated */
258+
if (written_size > 0 && (size_t)written_size >= sizeof(file)) {
259+
nm_log(NSLOG_RUNTIME_WARNING,
260+
"Warning: truncated path to check result file '%s'.\n", file);
261+
continue;
262+
}
263+
250264
if (stat(file, &stat_buf) == -1) {
251265
nm_log(NSLOG_RUNTIME_WARNING,
252266
"Warning: Could not stat() check result file '%s'.\n", file);

src/naemon/configuration.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,7 @@ read_config_file(const char *main_config_file, nagios_macros *mac)
10131013
break;
10141014
} else {
10151015
while ((dirfile = readdir(dirp)) != NULL) {
1016+
int written_size;
10161017
char file[MAX_FILENAME_LENGTH];
10171018

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

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

1029+
/* Check for encoding errors */
1030+
if (written_size < 0) {
1031+
nm_log(NSLOG_RUNTIME_WARNING,
1032+
"Warning: encoding error on config file path '`%s'.\n", file);
1033+
continue;
1034+
}
1035+
1036+
/* Check if the filename was truncated. */
1037+
if (written_size > 0 && (size_t)written_size >= sizeof(file)) {
1038+
nm_log(NSLOG_RUNTIME_WARNING,
1039+
"Warning: truncated path to config file '%s'.\n", file);
1040+
continue;
1041+
}
1042+
10281043
error |= read_config_file(file, mac);
10291044
}
10301045
closedir(dirp);

src/naemon/macros.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,7 @@ static int grab_macrox_value_r(nagios_macros *mac, int macro_type, char *arg1, c
15181518
/***************/
15191519
case MACRO_HOSTGROUPNAMES:
15201520
*free_macro = TRUE;
1521+
/* FALLTHROUGH */
15211522
case MACRO_HOSTNAME:
15221523
case MACRO_HOSTALIAS:
15231524
case MACRO_HOSTADDRESS:
@@ -1612,6 +1613,7 @@ static int grab_macrox_value_r(nagios_macros *mac, int macro_type, char *arg1, c
16121613
/********************/
16131614
case MACRO_HOSTGROUPMEMBERS:
16141615
*free_macro = TRUE;
1616+
/* FALLTHROUGH */
16151617
case MACRO_HOSTGROUPNAME:
16161618
case MACRO_HOSTGROUPALIAS:
16171619
case MACRO_HOSTGROUPNOTES:
@@ -1640,6 +1642,7 @@ static int grab_macrox_value_r(nagios_macros *mac, int macro_type, char *arg1, c
16401642
/******************/
16411643
case MACRO_SERVICEGROUPNAMES:
16421644
*free_macro = TRUE;
1645+
/* FALLTHROUGH */
16431646
case MACRO_SERVICEDESC:
16441647
case MACRO_SERVICESTATE:
16451648
case MACRO_SERVICESTATEID:
@@ -1757,6 +1760,7 @@ static int grab_macrox_value_r(nagios_macros *mac, int macro_type, char *arg1, c
17571760
case MACRO_SERVICEGROUPNOTESURL:
17581761
case MACRO_SERVICEGROUPACTIONURL:
17591762
*free_macro = TRUE;
1763+
/* FALLTHROUGH */
17601764
case MACRO_SERVICEGROUPNAME:
17611765
case MACRO_SERVICEGROUPALIAS:
17621766
/* a standard servicegroup macro */
@@ -1781,6 +1785,7 @@ static int grab_macrox_value_r(nagios_macros *mac, int macro_type, char *arg1, c
17811785
/******************/
17821786
case MACRO_CONTACTGROUPNAMES:
17831787
*free_macro = TRUE;
1788+
/* FALLTHROUGH */
17841789
case MACRO_CONTACTNAME:
17851790
case MACRO_CONTACTALIAS:
17861791
case MACRO_CONTACTEMAIL:
@@ -1841,6 +1846,7 @@ static int grab_macrox_value_r(nagios_macros *mac, int macro_type, char *arg1, c
18411846
/***********************/
18421847
case MACRO_CONTACTGROUPMEMBERS:
18431848
*free_macro = TRUE;
1849+
/* FALLTHROUGH */
18441850
case MACRO_CONTACTGROUPNAME:
18451851
case MACRO_CONTACTGROUPALIAS:
18461852
/* a standard contactgroup macro */

src/naemon/xodtemplate.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8456,15 +8456,30 @@ static int xodtemplate_process_config_dir(char *dir_name)
84568456

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

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

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

8469+
/* Check for encoding errors */
8470+
if (written_size < 0) {
8471+
nm_log(NSLOG_RUNTIME_WARNING,
8472+
"Warning: xodtemplate encoding error on config file path '`%s'.\n", file);
8473+
continue;
8474+
}
8475+
8476+
/* Check if the filename was truncated. */
8477+
if (written_size > 0 && (size_t)written_size >= sizeof(file)) {
8478+
nm_log(NSLOG_RUNTIME_WARNING,
8479+
"Warning: xodtemplate truncated path to config file '`%s'.\n", file);
8480+
continue;
8481+
}
8482+
84688483
/* process this if it's a non-hidden config file... */
84698484
if (stat(file, &stat_buf) == -1) {
84708485
nm_log(NSLOG_RUNTIME_ERROR, "Error: Could not open config directory member '%s' for reading.\n", file);

0 commit comments

Comments
 (0)