From 1130b7eadef1c7688c8b0386b4dd479f216553cb Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Mon, 20 Jul 2020 00:06:12 +0200 Subject: [PATCH] Fix -Wsign-compare compiler warnings Comparing signed and unsigned integer expressions can have unexpected results because the signed integer will get promoted to an unsigned integer. To avoid the warning, add an explicit cast to the unsigned type, along with a check to catch negative values. --- examples/dctool_parse.c | 2 +- examples/output_raw.c | 4 ++-- src/context.c | 2 +- src/hw_ostc3.c | 2 +- src/suunto_d9.c | 3 ++- src/suunto_eonsteel_parser.c | 6 ++---- src/suunto_vyper2.c | 3 ++- src/usbhid.c | 2 +- 8 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/dctool_parse.c b/examples/dctool_parse.c index 6ea8550..6b7da08 100644 --- a/examples/dctool_parse.c +++ b/examples/dctool_parse.c @@ -152,7 +152,7 @@ dctool_parse_run (int argc, char *argv[], dc_context_t *context, dc_descriptor_t goto cleanup; } - for (unsigned int i = 0; i < argc; ++i) { + for (int i = 0; i < argc; ++i) { // Read the input file. buffer = dctool_file_read (argv[i]); if (buffer == NULL) { diff --git a/examples/output_raw.c b/examples/output_raw.c index 951531e..9e71780 100644 --- a/examples/output_raw.c +++ b/examples/output_raw.c @@ -80,7 +80,7 @@ mktemplate_datetime (char *buffer, size_t size, dc_parser_t *parser) n = snprintf (buffer, size, "%04i%02i%02iT%02i%02i%02i", datetime.year, datetime.month, datetime.day, datetime.hour, datetime.minute, datetime.second); - if (n < 0 || n >= size) + if (n < 0 || (size_t) n >= size) return -1; return n; @@ -92,7 +92,7 @@ mktemplate_number (char *buffer, size_t size, unsigned int number) int n = 0; n = snprintf (buffer, size, "%04u", number); - if (n < 0 || n >= size) + if (n < 0 || (size_t) n >= size) return -1; return n; diff --git a/src/context.c b/src/context.c index 88fd9b5..cab6f33 100644 --- a/src/context.c +++ b/src/context.c @@ -77,7 +77,7 @@ l_vsnprintf (char *str, size_t size, const char *format, va_list ap) * enough. */ n = vsnprintf (str, size, format, ap); - if (n >= size) + if (n >= 0 && (size_t) n >= size) n = -1; #endif diff --git a/src/hw_ostc3.c b/src/hw_ostc3.c index c669ea2..3ed6bc7 100644 --- a/src/hw_ostc3.c +++ b/src/hw_ostc3.c @@ -1024,7 +1024,7 @@ hw_ostc3_firmware_readline (FILE *fp, dc_context_t *context, unsigned int addr, unsigned char ascii[39]; unsigned char faddr_byte[3]; unsigned int faddr = 0; - int n = 0; + size_t n = 0; if (size > 16) { ERROR (context, "Invalid arguments."); diff --git a/src/suunto_d9.c b/src/suunto_d9.c index ab6eee1..8b8320e 100644 --- a/src/suunto_d9.c +++ b/src/suunto_d9.c @@ -261,7 +261,8 @@ suunto_d9_device_packet (dc_device_t *abstract, const unsigned char command[], u } // Verify the size of the package. - if (array_uint16_be (answer + 1) + 4 != asize) { + unsigned int len = array_uint16_be (answer + 1); + if (len + 4 != asize) { ERROR (abstract->context, "Unexpected answer size."); return DC_STATUS_PROTOCOL; } diff --git a/src/suunto_eonsteel_parser.c b/src/suunto_eonsteel_parser.c index d8dcdcc..a941518 100644 --- a/src/suunto_eonsteel_parser.c +++ b/src/suunto_eonsteel_parser.c @@ -137,7 +137,6 @@ static const struct { static enum eon_sample lookup_descriptor_type(suunto_eonsteel_parser_t *eon, struct type_desc *desc) { - int i; const char *name = desc->desc; // Not a sample type? Skip it @@ -160,7 +159,7 @@ static enum eon_sample lookup_descriptor_type(suunto_eonsteel_parser_t *eon, str name += 8; // .. and look it up in the table of sample type strings - for (i = 0; i < C_ARRAY_SIZE(type_translation); i++) { + for (size_t i = 0; i < C_ARRAY_SIZE(type_translation); i++) { if (!strcmp(name, type_translation[i].name)) return type_translation[i].type; } @@ -179,8 +178,7 @@ static parser_sample_event_t lookup_event(const char *name, const eon_event_t ev static const char *desc_type_name(enum eon_sample type) { - int i; - for (i = 0; i < C_ARRAY_SIZE(type_translation); i++) { + for (size_t i = 0; i < C_ARRAY_SIZE(type_translation); i++) { if (type == type_translation[i].type) return type_translation[i].name; } diff --git a/src/suunto_vyper2.c b/src/suunto_vyper2.c index 02f38ed..7a9f7ce 100644 --- a/src/suunto_vyper2.c +++ b/src/suunto_vyper2.c @@ -252,7 +252,8 @@ suunto_vyper2_device_packet (dc_device_t *abstract, const unsigned char command[ } // Verify the size of the package. - if (array_uint16_be (answer + 1) + 4 != asize) { + unsigned int len = array_uint16_be (answer + 1); + if (len + 4 != asize) { ERROR (abstract->context, "Unexpected answer size."); return DC_STATUS_PROTOCOL; } diff --git a/src/usbhid.c b/src/usbhid.c index 9231e64..b3805ca 100644 --- a/src/usbhid.c +++ b/src/usbhid.c @@ -777,7 +777,7 @@ dc_usbhid_write (dc_iostream_t *abstract, const void *data, size_t size, size_t out: #ifdef _WIN32 - if (nbytes > size) { + if ((size_t) nbytes > size) { WARNING (abstract->context, "Number of bytes exceeds the buffer size (" DC_PRINTF_SIZE " > " DC_PRINTF_SIZE ")!", nbytes, size); nbytes = size; }