From 07f9c438f31de7a280e43c4196a32d200ad41fbe Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 19 Oct 2023 12:10:55 +0200 Subject: Fixes for -Wsign-compare --- lib/utils_base.c | 2 +- lib/utils_base.h | 2 +- lib/utils_cmd.c | 3 +-- plugins/check_apt.c | 17 +++++++++-------- plugins/check_by_ssh.c | 15 +++++++-------- plugins/check_curl.c | 13 ++++++------- plugins/check_dns.c | 6 +++--- plugins/check_procs.c | 4 ++-- plugins/check_snmp.c | 31 ++++++++++++++++++------------- plugins/check_tcp.c | 14 +++++++------- plugins/check_ups.c | 3 ++- plugins/negate.c | 5 ++--- plugins/runcmd.c | 3 +-- plugins/utils.c | 10 +++++++++- 14 files changed, 69 insertions(+), 59 deletions(-) diff --git a/lib/utils_base.c b/lib/utils_base.c index f86efbeb..f8592f41 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c @@ -37,7 +37,7 @@ monitoring_plugin *this_monitoring_plugin=NULL; -unsigned int timeout_state = STATE_CRITICAL; +int timeout_state = STATE_CRITICAL; unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT; bool _np_state_read_file(FILE *); diff --git a/lib/utils_base.h b/lib/utils_base.h index 80b87435..9d4dffed 100644 --- a/lib/utils_base.h +++ b/lib/utils_base.h @@ -65,7 +65,7 @@ bool check_range(double, range *); int get_status(double, thresholds *); /* Handle timeouts */ -extern unsigned int timeout_state; +extern int timeout_state; extern unsigned int timeout_interval; /* All possible characters in a threshold range */ diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index cfb2073c..7957ec14 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c @@ -390,13 +390,12 @@ cmd_file_read ( char *filename, output *out, int flags) void timeout_alarm_handler (int signo) { - size_t i; if (signo == SIGALRM) { printf (_("%s - Plugin timed out after %d seconds\n"), state_text(timeout_state), timeout_interval); long maxfd = mp_open_max(); - if(_cmd_pids) for(i = 0; i < maxfd; i++) { + if(_cmd_pids) for(long int i = 0; i < maxfd; i++) { if(_cmd_pids[i] != 0) kill(_cmd_pids[i], SIGKILL); } diff --git a/plugins/check_apt.c b/plugins/check_apt.c index fa982ae3..290c88ed 100644 --- a/plugins/check_apt.c +++ b/plugins/check_apt.c @@ -94,7 +94,7 @@ static int stderr_warning = 0; /* if a cmd issued output on stderr */ static int exec_warning = 0; /* if a cmd exited non-zero */ int main (int argc, char **argv) { - int result=STATE_UNKNOWN, packages_available=0, sec_count=0, i=0; + int result=STATE_UNKNOWN, packages_available=0, sec_count=0; char **packages_list=NULL, **secpackages_list=NULL; /* Parse extra opts if any */ @@ -142,10 +142,11 @@ int main (int argc, char **argv) { qsort(secpackages_list, sec_count, sizeof(char*), cmpstringp); qsort(packages_list, packages_available-sec_count, sizeof(char*), cmpstringp); - for(i = 0; i < sec_count; i++) + for(int i = 0; i < sec_count; i++) printf("%s (security)\n", secpackages_list[i]); + if (only_critical == false) { - for(i = 0; i < packages_available - sec_count; i++) + for(int i = 0; i < packages_available - sec_count; i++) printf("%s\n", packages_list[i]); } } @@ -320,7 +321,7 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg * we may need to switch to the --print-uris output format, * in which case the logic here will slightly change. */ - for(i = 0; i < chld_out.lines; i++) { + for(size_t i = 0; i < chld_out.lines; i++) { if(verbose){ printf("%s\n", chld_out.line[i]); } @@ -353,7 +354,7 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg stderr_warning=1; result = max_state(result, STATE_WARNING); if(verbose){ - for(i = 0; i < chld_err.lines; i++) { + for(size_t i = 0; i < chld_err.lines; i++) { fprintf(stderr, "%s\n", chld_err.line[i]); } } @@ -367,7 +368,7 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg /* run an apt-get update (needs root) */ int run_update(void){ - int i=0, result=STATE_UNKNOWN; + int result=STATE_UNKNOWN; struct output chld_out, chld_err; char *cmdline; @@ -385,7 +386,7 @@ int run_update(void){ } if(verbose){ - for(i = 0; i < chld_out.lines; i++) { + for(size_t i = 0; i < chld_out.lines; i++) { printf("%s\n", chld_out.line[i]); } } @@ -395,7 +396,7 @@ int run_update(void){ stderr_warning=1; result = max_state(result, STATE_WARNING); if(verbose){ - for(i = 0; i < chld_err.lines; i++) { + for(size_t i = 0; i < chld_err.lines; i++) { fprintf(stderr, "%s\n", chld_err.line[i]); } } diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c index 1f5f72d4..2a23b397 100644 --- a/plugins/check_by_ssh.c +++ b/plugins/check_by_ssh.c @@ -68,7 +68,6 @@ main (int argc, char **argv) char *status_text; int cresult; int result = STATE_UNKNOWN; - int i; time_t local_time; FILE *fp = NULL; output chld_out, chld_err; @@ -96,7 +95,7 @@ main (int argc, char **argv) /* run the command */ if (verbose) { printf ("Command: %s\n", commargv[0]); - for (i=1; i skip_stderr) { + if(chld_err.lines > (size_t)skip_stderr) { printf (_("Remote command execution failed: %s\n"), chld_err.line[skip_stderr]); if ( warn_on_stderr ) @@ -134,8 +133,8 @@ main (int argc, char **argv) /* this is simple if we're not supposed to be passive. * Wrap up quickly and keep the tricks below */ if(!passive) { - if (chld_out.lines > skip_stdout) - for (i = skip_stdout; i < chld_out.lines; i++) + if (chld_out.lines > (size_t)skip_stdout) + for (size_t i = skip_stdout; i < chld_out.lines; i++) puts (chld_out.line[i]); else printf (_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), @@ -156,7 +155,7 @@ main (int argc, char **argv) local_time = time (NULL); commands = 0; - for(i = skip_stdout; i < chld_out.lines; i++) { + for(size_t i = skip_stdout; i < chld_out.lines; i++) { status_text = chld_out.line[i++]; if (i == chld_out.lines || strstr (chld_out.line[i], "STATUS CODE: ") == NULL) die (STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); diff --git a/plugins/check_curl.c b/plugins/check_curl.c index 153e492f..9c0dc349 100644 --- a/plugins/check_curl.c +++ b/plugins/check_curl.c @@ -1186,16 +1186,16 @@ int uri_strcmp (const UriTextRangeA range, const char* s) { if (!range.first) return -1; - if (range.afterLast - range.first < strlen (s)) return -1; - return strncmp (s, range.first, min( range.afterLast - range.first, strlen (s))); + if ( (size_t)(range.afterLast - range.first) < strlen (s) ) return -1; + return strncmp (s, range.first, min( (size_t)(range.afterLast - range.first), strlen (s))); } char* uri_string (const UriTextRangeA range, char* buf, size_t buflen) { if (!range.first) return "(null)"; - strncpy (buf, range.first, max (buflen-1, range.afterLast - range.first)); - buf[max (buflen-1, range.afterLast - range.first)] = '\0'; + strncpy (buf, range.first, max (buflen-1, (size_t)(range.afterLast - range.first))); + buf[max (buflen-1, (size_t)(range.afterLast - range.first))] = '\0'; buf[range.afterLast - range.first] = '\0'; return buf; } @@ -2368,8 +2368,7 @@ remove_newlines (char *s) char * get_header_value (const struct phr_header* headers, const size_t nof_headers, const char* header) { - int i; - for( i = 0; i < nof_headers; i++ ) { + for(size_t i = 0; i < nof_headers; i++ ) { if(headers[i].name != NULL && strncasecmp( header, headers[i].name, max( headers[i].name_len, 4 ) ) == 0 ) { return strndup( headers[i].value, headers[i].value_len ); } @@ -2471,7 +2470,7 @@ check_document_dates (const curlhelp_write_curlbuf *header_buf, char (*msg)[DEFA int get_content_length (const curlhelp_write_curlbuf* header_buf, const curlhelp_write_curlbuf* body_buf) { - int content_length = 0; + size_t content_length = 0; struct phr_header headers[255]; size_t nof_headers = 255; size_t msglen; diff --git a/plugins/check_dns.c b/plugins/check_dns.c index 82dc264a..5e20214c 100644 --- a/plugins/check_dns.c +++ b/plugins/check_dns.c @@ -122,7 +122,7 @@ main (int argc, char **argv) } /* scan stdout */ - for(i = 0; i < chld_out.lines; i++) { + for(size_t i = 0; i < chld_out.lines; i++) { if (addresses == NULL) addresses = malloc(sizeof(*addresses)*10); else if (!(n_addresses % 10)) @@ -197,7 +197,7 @@ main (int argc, char **argv) } /* scan stderr */ - for(i = 0; i < chld_err.lines; i++) { + for(size_t i = 0; i < chld_err.lines; i++) { if (verbose) puts(chld_err.line[i]); @@ -241,7 +241,7 @@ main (int argc, char **argv) unsigned long expect_match = (1 << expected_address_cnt) - 1; unsigned long addr_match = (1 << n_addresses) - 1; - for (i=0; i= 3) diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c index 405ede3a..01bee231 100644 --- a/plugins/check_snmp.c +++ b/plugins/check_snmp.c @@ -131,7 +131,7 @@ size_t nlabels = 0; size_t labels_size = OID_COUNT_STEP; size_t nunits = 0; size_t unitv_size = OID_COUNT_STEP; -int numoids = 0; +size_t numoids = 0; int numauthpriv = 0; int numcontext = 0; int verbose = 0; @@ -187,7 +187,8 @@ static char *fix_snmp_range(char *th) int main (int argc, char **argv) { - int i, len, line, total_oids; + int len, total_oids; + size_t line; unsigned int bk_count = 0, dq_count = 0; int iresult = STATE_UNKNOWN; int result = STATE_UNKNOWN; @@ -253,7 +254,9 @@ main (int argc, char **argv) if(calculate_rate) { if (!strcmp(label, "SNMP")) label = strdup("SNMP RATE"); - i=0; + + size_t i = 0; + previous_state = np_state_read(); if(previous_state!=NULL) { /* Split colon separated values */ @@ -273,7 +276,7 @@ main (int argc, char **argv) /* Populate the thresholds */ th_warn=warning_thresholds; th_crit=critical_thresholds; - for (i=0; i 0) { printf (_("External command error: %s\n"), chld_err.line[0]); - for (i = 1; i < chld_err.lines; i++) { + for (size_t i = 1; i < chld_err.lines; i++) { printf ("%s\n", chld_err.line[i]); } } else { @@ -392,12 +395,14 @@ main (int argc, char **argv) } if (verbose) { - for (i = 0; i < chld_out.lines; i++) { + for (size_t i = 0; i < chld_out.lines; i++) { printf ("%s\n", chld_out.line[i]); } } - for (line=0, i=0; line < chld_out.lines && i < numoids ; line++, i++) { + line = 0; + total_oids = 0; + for (size_t i = 0; line < chld_out.lines && i < numoids ; line++, i++, total_oids++) { if(calculate_rate) conv = "%.10g"; else @@ -634,7 +639,6 @@ main (int argc, char **argv) strncat(perfstr, " ", sizeof(perfstr)-strlen(perfstr)-1); } } - total_oids=i; /* Save state data, as all data collected now */ if(calculate_rate) { @@ -644,7 +648,7 @@ main (int argc, char **argv) die(STATE_UNKNOWN, _("Cannot malloc")); current_length=0; - for(i=0; i 6 && !memcmp(progname, "check_", 6)) { SERVICE = strdup(progname + 6); - for(i = 0; i < len - 6; i++) + for(size_t i = 0; i < len - 6; i++) SERVICE[i] = toupper(SERVICE[i]); } @@ -275,7 +274,7 @@ main (int argc, char **argv) printf("Quit string: %s\n", server_quit); } printf("server_expect_count: %d\n", (int)server_expect_count); - for(i = 0; i < server_expect_count; i++) + for(size_t i = 0; i < server_expect_count; i++) printf("\t%d: %s\n", i, server_expect[i]); } @@ -284,10 +283,11 @@ main (int argc, char **argv) if (server_expect_count) { /* watch for the expect string */ - while ((i = my_recv(buffer, sizeof(buffer))) > 0) { - status = realloc(status, len + i + 1); - memcpy(&status[len], buffer, i); - len += i; + size_t received = 0; + while ((received = my_recv(buffer, sizeof(buffer))) > 0) { + status = realloc(status, len + received + 1); + memcpy(&status[len], buffer, received); + len += received; status[len] = '\0'; /* stop reading if user-forced */ diff --git a/plugins/check_ups.c b/plugins/check_ups.c index d1ded620..bb91c4ad 100644 --- a/plugins/check_ups.c +++ b/plugins/check_ups.c @@ -402,7 +402,8 @@ get_ups_variable (const char *varname, char *buf) /* create the command string to send to the UPS daemon */ /* Add LOGOUT to avoid read failure logs */ - if (snprintf (send_buffer, sizeof(send_buffer), "GET VAR %s %s\nLOGOUT\n", ups_name, varname) >= sizeof(send_buffer)) { + int res = snprintf (send_buffer, sizeof(send_buffer), "GET VAR %s %s\nLOGOUT\n", ups_name, varname); + if ( (res > 0) && ((size_t)res >= sizeof(send_buffer))) { printf("%s\n", _("UPS name to long for buffer")); return ERROR; } diff --git a/plugins/negate.c b/plugins/negate.c index 79cca7e0..745c12ac 100644 --- a/plugins/negate.c +++ b/plugins/negate.c @@ -63,7 +63,6 @@ main (int argc, char **argv) char *sub; char **command_line; output chld_out, chld_err; - int i; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); @@ -86,7 +85,7 @@ main (int argc, char **argv) result = cmd_run_array (command_line, &chld_out, &chld_err, 0); } if (chld_err.lines > 0) { - for (i = 0; i < chld_err.lines; i++) { + for (size_t i = 0; i < chld_err.lines; i++) { fprintf (stderr, "%s\n", chld_err.line[i]); } } @@ -95,7 +94,7 @@ main (int argc, char **argv) if (chld_out.lines == 0) die (max_state_alt (result, STATE_UNKNOWN), _("No data returned from command\n")); - for (i = 0; i < chld_out.lines; i++) { + for (size_t i = 0; i < chld_out.lines; i++) { if (subst_text && result >= 0 && result <= 4 && result != state[result]) { /* Loop over each match found */ while ((sub = strstr (chld_out.line[i], state_text (result)))) { diff --git a/plugins/runcmd.c b/plugins/runcmd.c index 4f3e349b..32fd6b96 100644 --- a/plugins/runcmd.c +++ b/plugins/runcmd.c @@ -240,13 +240,12 @@ np_runcmd_close(int fd) void runcmd_timeout_alarm_handler (int signo) { - size_t i; if (signo == SIGALRM) puts(_("CRITICAL - Plugin timed out while executing system call")); long maxfd = mp_open_max(); - if(np_pids) for(i = 0; i < maxfd; i++) { + if(np_pids) for(long int i = 0; i < maxfd; i++) { if(np_pids[i] != 0) kill(np_pids[i], SIGKILL); } diff --git a/plugins/utils.c b/plugins/utils.c index 7e14b6e4..e871c5f7 100644 --- a/plugins/utils.c +++ b/plugins/utils.c @@ -230,13 +230,21 @@ bool is_intnonneg (char *number) { */ bool is_int64(char *number, int64_t *target) { errno = 0; - uint64_t tmp = strtoll(number, NULL, 10); + char *endptr = { 0 }; + + int64_t tmp = strtoll(number, &endptr, 10); if (errno != 0) { return false; } + + if (*endptr == '\0') { + return 0; + } + if (tmp < INT64_MIN || tmp > INT64_MAX) { return false; } + if (target != NULL) { *target = tmp; } -- cgit v1.2.3-74-g34f1 From 81f3b4165146eedd2da60afc5677ec44a0db9829 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 19 Oct 2023 12:20:27 +0200 Subject: Fix fallout of the previous changes --- plugins/check_apt.c | 2 +- plugins/check_http.c | 2 +- plugins/check_snmp.c | 4 ++-- plugins/check_tcp.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/check_apt.c b/plugins/check_apt.c index 290c88ed..5c0f6e28 100644 --- a/plugins/check_apt.c +++ b/plugins/check_apt.c @@ -252,7 +252,7 @@ int process_arguments (int argc, char **argv) { /* run an apt-get upgrade */ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist){ - int i=0, result=STATE_UNKNOWN, regres=0, pc=0, spc=0; + int result=STATE_UNKNOWN, regres=0, pc=0, spc=0; struct output chld_out, chld_err; regex_t ireg, ereg, sreg; char *cmdline=NULL, rerrbuf[64]; diff --git a/plugins/check_http.c b/plugins/check_http.c index b9d8145c..110f1188 100644 --- a/plugins/check_http.c +++ b/plugins/check_http.c @@ -471,7 +471,7 @@ bool process_arguments (int argc, char **argv) free(http_method); http_method = strdup (optarg); char *tmp; - if ((tmp = strstr(http_method, ":")) > 0) { + if ((tmp = strstr(http_method, ":")) != NULL) { tmp[0] = '\0'; http_method = http_method; http_method_proxy = ++tmp; diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c index 01bee231..7ee9d0ca 100644 --- a/plugins/check_snmp.c +++ b/plugins/check_snmp.c @@ -263,7 +263,7 @@ main (int argc, char **argv) previous_string = strdup((char *) previous_state->data); while((ap = strsep(&previous_string, ":")) != NULL) { if(verbose>2) - printf("State for %d=%s\n", i, ap); + printf("State for %zd=%s\n", i, ap); while (i >= previous_size) { previous_size += OID_COUNT_STEP; previous_value = realloc(previous_value, previous_size * sizeof(*previous_value)); @@ -415,7 +415,7 @@ main (int argc, char **argv) break; if (verbose > 2) { - printf("Processing oid %i (line %i)\n oidname: %s\n response: %s\n", i+1, line+1, oidname, response); + printf("Processing oid %zi (line %zi)\n oidname: %s\n response: %s\n", i+1, line+1, oidname, response); } /* Clean up type array - Sol10 does not necessarily zero it out */ diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c index c103612a..054f1a39 100644 --- a/plugins/check_tcp.c +++ b/plugins/check_tcp.c @@ -275,7 +275,7 @@ main (int argc, char **argv) } printf("server_expect_count: %d\n", (int)server_expect_count); for(size_t i = 0; i < server_expect_count; i++) - printf("\t%d: %s\n", i, server_expect[i]); + printf("\t%zd: %s\n", i, server_expect[i]); } /* if(len) later on, we know we have a non-NULL response */ -- cgit v1.2.3-74-g34f1 From eead88edda047843b911afd7b63e7decfee306ce Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 19 Oct 2023 13:46:15 +0200 Subject: check_tcp: Fixes an error with using the wrong type for a variable --- plugins/check_tcp.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c index 054f1a39..01dd35eb 100644 --- a/plugins/check_tcp.c +++ b/plugins/check_tcp.c @@ -70,7 +70,7 @@ static char *server_send = NULL; static char *server_quit = NULL; static char **server_expect; static size_t server_expect_count = 0; -static size_t maxbytes = 0; +static ssize_t maxbytes = 0; static char **warn_codes = NULL; static size_t warn_codes_count = 0; static char **crit_codes = NULL; @@ -105,7 +105,6 @@ main (int argc, char **argv) char *status = NULL; struct timeval tv; struct timeval timeout; - size_t len; int match = -1; fd_set rfds; @@ -120,10 +119,10 @@ main (int argc, char **argv) if(progname != NULL) progname++; else progname = argv[0]; - len = strlen(progname); - if(len > 6 && !memcmp(progname, "check_", 6)) { + size_t prog_name_len = strlen(progname); + if(prog_name_len > 6 && !memcmp(progname, "check_", 6)) { SERVICE = strdup(progname + 6); - for(size_t i = 0; i < len - 6; i++) + for(size_t i = 0; i < prog_name_len - 6; i++) SERVICE[i] = toupper(SERVICE[i]); } @@ -279,11 +278,12 @@ main (int argc, char **argv) } /* if(len) later on, we know we have a non-NULL response */ - len = 0; + ssize_t len = 0; + if (server_expect_count) { + ssize_t received = 0; /* watch for the expect string */ - size_t received = 0; while ((received = my_recv(buffer, sizeof(buffer))) > 0) { status = realloc(status, len + received + 1); memcpy(&status[len], buffer, received); @@ -307,6 +307,7 @@ main (int argc, char **argv) if(select(sd + 1, &rfds, NULL, NULL, &timeout) <= 0) break; } + if (match == NP_MATCH_RETRY) match = NP_MATCH_FAILURE; -- cgit v1.2.3-74-g34f1