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 --- 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 +++++++++- 11 files changed, 66 insertions(+), 55 deletions(-) (limited to 'plugins') 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