[monitoring-plugins] Fixes for -Wsign-compare

RincewindsHat git at monitoring-plugins.org
Sun Oct 29 11:10:12 CET 2023


 Module: monitoring-plugins
 Branch: master
 Commit: 07f9c438f31de7a280e43c4196a32d200ad41fbe
 Author: RincewindsHat <12514511+RincewindsHat at users.noreply.github.com>
   Date: Thu Oct 19 12:10:55 2023 +0200
    URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=07f9c43

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 f86efbe..f8592f4 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 80b8743..9d4dffe 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 cfb2073..7957ec1 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 fa982ae..290c88e 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 1f5f72d..2a23b39 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<commargc; i++)
+		for (int i = 1; i < commargc; i++)
 			printf ("Argument %i: %s\n", i, commargv[i]);
 	}
 
@@ -110,9 +109,9 @@ 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("stdout: %s\n", chld_out.line[i]);
-		for(i = 0; i < chld_err.lines; i++)
+		for(size_t i = 0; i < chld_err.lines; i++)
 			printf("stderr: %s\n", chld_err.line[i]);
 	}
 
@@ -122,7 +121,7 @@ main (int argc, char **argv)
 		skip_stderr = chld_err.lines;
 
 	/* UNKNOWN or worse if (non-skipped) output found on stderr */
-	if(chld_err.lines > 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 153e492..9c0dc34 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 82dc264..5e20214 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<expected_address_cnt; i++) {
+    for (int i=0; i<expected_address_cnt; i++) {
       int j;
       /* check if we get a match on 'raw' ip or cidr */
       for (j=0; j<n_addresses; j++) {
diff --git a/plugins/check_procs.c b/plugins/check_procs.c
index 1637e3e..6e3feae 100644
--- a/plugins/check_procs.c
+++ b/plugins/check_procs.c
@@ -153,7 +153,7 @@ main (int argc, char **argv)
 	int expected_cols = PS_COLS - 1;
 	int warn = 0; /* number of processes in warn state */
 	int crit = 0; /* number of processes in crit state */
-	int i = 0, j = 0;
+	int i = 0;
 	int result = STATE_UNKNOWN;
 	int ret = 0;
 	output chld_out, chld_err;
@@ -207,7 +207,7 @@ main (int argc, char **argv)
 	}
 
 	/* flush first line: j starts at 1 */
-	for (j = 1; j < chld_out.lines; j++) {
+	for (size_t j = 1; j < chld_out.lines; j++) {
 		input_line = chld_out.line[j];
 
 		if (verbose >= 3)
diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c
index 405ede3..01bee23 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<numoids; i++) {
+	for (size_t i = 0; i < numoids; i++) {
 		char *w = th_warn ? strndup(th_warn, strcspn(th_warn, ",")) : NULL;
 		char *c = th_crit ? strndup(th_crit, strcspn(th_crit, ",")) : NULL;
 		/* translate "2:1" to "@1:2" for backwards compatibility */
@@ -333,11 +336,11 @@ main (int argc, char **argv)
 	}
 
 
-	for (i = 0; i < numcontext; i++) {
+	for (int i = 0; i < numcontext; i++) {
 		command_line[index++] = contextargs[i];
 	}
 
-	for (i = 0; i < numauthpriv; i++) {
+	for (int i = 0; i < numauthpriv; i++) {
 		command_line[index++] = authpriv[i];
 	}
 
@@ -348,7 +351,7 @@ main (int argc, char **argv)
 	 server_address,
 	 port);
 
-	for (i = 0; i < numoids; i++) {
+	for (size_t i = 0; i < numoids; i++) {
 		command_line[index++] = oids[i];
 		xasprintf(&cl_hidden_auth, "%s %s", cl_hidden_auth, oids[i]);
 	}
@@ -382,7 +385,7 @@ main (int argc, char **argv)
 	if (external_error) {
 		if (chld_err.lines > 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<total_oids; i++) {
+		for(int i = 0; i < total_oids; i++) {
 			xasprintf(&temp_string,"%.0f",response_value[i]);
 			if(temp_string==NULL)
 				die(STATE_UNKNOWN,_("Cannot asprintf()"));
@@ -687,7 +691,8 @@ process_arguments (int argc, char **argv)
 {
 	char *ptr;
 	int c = 1;
-	int j = 0, jj = 0, ii = 0;
+	int ii = 0;
+	size_t j = 0, jj = 0;
 
 	int option = 0;
 	static struct option longopts[] = {
diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c
index a1a14b4..c103612 100644
--- a/plugins/check_tcp.c
+++ b/plugins/check_tcp.c
@@ -102,7 +102,6 @@ int
 main (int argc, char **argv)
 {
 	int result = STATE_UNKNOWN;
-	int i;
 	char *status = NULL;
 	struct timeval tv;
 	struct timeval timeout;
@@ -124,7 +123,7 @@ main (int argc, char **argv)
 	len = strlen(progname);
 	if(len > 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 d1ded62..bb91c4a 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 79cca7e..745c12a 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 4f3e349..32fd6b9 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 7e14b6e..e871c5f 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;
 	}



More information about the Commits mailing list