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:
parent
8f383ac531
commit
1130b7eade
@ -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) {
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
@ -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.");
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user