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.
This commit is contained in:
Jef Driesen 2020-07-20 00:06:12 +02:00
parent 8f383ac531
commit 1130b7eade
8 changed files with 12 additions and 12 deletions

View File

@ -152,7 +152,7 @@ dctool_parse_run (int argc, char *argv[], dc_context_t *context, dc_descriptor_t
goto cleanup; goto cleanup;
} }
for (unsigned int i = 0; i < argc; ++i) { for (int i = 0; i < argc; ++i) {
// Read the input file. // Read the input file.
buffer = dctool_file_read (argv[i]); buffer = dctool_file_read (argv[i]);
if (buffer == NULL) { if (buffer == NULL) {

View File

@ -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", n = snprintf (buffer, size, "%04i%02i%02iT%02i%02i%02i",
datetime.year, datetime.month, datetime.day, datetime.year, datetime.month, datetime.day,
datetime.hour, datetime.minute, datetime.second); datetime.hour, datetime.minute, datetime.second);
if (n < 0 || n >= size) if (n < 0 || (size_t) n >= size)
return -1; return -1;
return n; return n;
@ -92,7 +92,7 @@ mktemplate_number (char *buffer, size_t size, unsigned int number)
int n = 0; int n = 0;
n = snprintf (buffer, size, "%04u", number); n = snprintf (buffer, size, "%04u", number);
if (n < 0 || n >= size) if (n < 0 || (size_t) n >= size)
return -1; return -1;
return n; return n;

View File

@ -77,7 +77,7 @@ l_vsnprintf (char *str, size_t size, const char *format, va_list ap)
* enough. * enough.
*/ */
n = vsnprintf (str, size, format, ap); n = vsnprintf (str, size, format, ap);
if (n >= size) if (n >= 0 && (size_t) n >= size)
n = -1; n = -1;
#endif #endif

View File

@ -1024,7 +1024,7 @@ hw_ostc3_firmware_readline (FILE *fp, dc_context_t *context, unsigned int addr,
unsigned char ascii[39]; unsigned char ascii[39];
unsigned char faddr_byte[3]; unsigned char faddr_byte[3];
unsigned int faddr = 0; unsigned int faddr = 0;
int n = 0; size_t n = 0;
if (size > 16) { if (size > 16) {
ERROR (context, "Invalid arguments."); ERROR (context, "Invalid arguments.");

View File

@ -261,7 +261,8 @@ suunto_d9_device_packet (dc_device_t *abstract, const unsigned char command[], u
} }
// Verify the size of the package. // 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."); ERROR (abstract->context, "Unexpected answer size.");
return DC_STATUS_PROTOCOL; return DC_STATUS_PROTOCOL;
} }

View File

@ -137,7 +137,6 @@ static const struct {
static enum eon_sample lookup_descriptor_type(suunto_eonsteel_parser_t *eon, struct type_desc *desc) static enum eon_sample lookup_descriptor_type(suunto_eonsteel_parser_t *eon, struct type_desc *desc)
{ {
int i;
const char *name = desc->desc; const char *name = desc->desc;
// Not a sample type? Skip it // 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; name += 8;
// .. and look it up in the table of sample type strings // .. 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)) if (!strcmp(name, type_translation[i].name))
return type_translation[i].type; 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) static const char *desc_type_name(enum eon_sample type)
{ {
int i; for (size_t i = 0; i < C_ARRAY_SIZE(type_translation); i++) {
for (i = 0; i < C_ARRAY_SIZE(type_translation); i++) {
if (type == type_translation[i].type) if (type == type_translation[i].type)
return type_translation[i].name; return type_translation[i].name;
} }

View File

@ -252,7 +252,8 @@ suunto_vyper2_device_packet (dc_device_t *abstract, const unsigned char command[
} }
// Verify the size of the package. // 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."); ERROR (abstract->context, "Unexpected answer size.");
return DC_STATUS_PROTOCOL; return DC_STATUS_PROTOCOL;
} }

View File

@ -777,7 +777,7 @@ dc_usbhid_write (dc_iostream_t *abstract, const void *data, size_t size, size_t
out: out:
#ifdef _WIN32 #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); WARNING (abstract->context, "Number of bytes exceeds the buffer size (" DC_PRINTF_SIZE " > " DC_PRINTF_SIZE ")!", nbytes, size);
nbytes = size; nbytes = size;
} }