diff --git a/examples/dctool.c b/examples/dctool.c index ddcd596..3104bc4 100644 --- a/examples/dctool.c +++ b/examples/dctool.c @@ -240,7 +240,8 @@ main (int argc, char *argv[]) } // Translate the help option into a command. - char *argv_help[] = {(char *) "help", NULL, NULL}; + char helpcmd[] = "help"; + char *argv_help[] = {helpcmd, NULL, NULL}; if (help || argv[0] == NULL) { if (argv[0]) { argv_help[1] = argv[0]; 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/include/libdivecomputer/datetime.h b/include/libdivecomputer/datetime.h index 768883d..e5b876f 100644 --- a/include/libdivecomputer/datetime.h +++ b/include/libdivecomputer/datetime.h @@ -22,11 +22,13 @@ #ifndef DC_DATETIME_H #define DC_DATETIME_H +#include + #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ -#define DC_TIMEZONE_NONE 0x80000000 +#define DC_TIMEZONE_NONE INT_MIN #if defined (_WIN32) && !defined (__GNUC__) typedef __int64 dc_ticks_t; diff --git a/src/cochran_commander.c b/src/cochran_commander.c index 63a00ba..e3677e9 100644 --- a/src/cochran_commander.c +++ b/src/cochran_commander.c @@ -41,6 +41,8 @@ #define COCHRAN_MODEL_EMC_16 4 #define COCHRAN_MODEL_EMC_20 5 +#define UNDEFINED 0xFFFFFFFF + typedef enum cochran_endian_t { ENDIAN_LE, ENDIAN_BE, @@ -57,8 +59,8 @@ typedef struct cochran_data_t { unsigned char *logbook; unsigned short int dive_count; - int fp_dive_num; - int invalid_profile_dive_num; + unsigned int fp_dive_num; + unsigned int invalid_profile_dive_num; unsigned int logbook_size; } cochran_data_t; @@ -621,18 +623,17 @@ cochran_commander_find_fingerprint(cochran_commander_device_t *device, cochran_d // We track profile ringbuffer usage to determine which dives have profile data int profile_capacity_remaining = device->layout->rb_profile_end - device->layout->rb_profile_begin; - int dive_count = -1; - data->fp_dive_num = -1; + unsigned int dive_count = 0; + data->fp_dive_num = UNDEFINED; // Start at end of log if (data->dive_count < device->layout->rb_logbook_entry_count) dive_count = data->dive_count; else dive_count = device->layout->rb_logbook_entry_count; - dive_count--; unsigned int sample_read_size = 0; - data->invalid_profile_dive_num = -1; + data->invalid_profile_dive_num = UNDEFINED; // Remove the pre-dive events that occur after the last dive unsigned int rb_head_ptr = 0; @@ -676,7 +677,7 @@ cochran_commander_find_fingerprint(cochran_commander_device_t *device, cochran_d // Loop through dives to find FP, Accumulate profile data size, // and find the last dive with invalid profile - for (unsigned int i = 0; i <= dive_count; ++i) { + for (unsigned int i = 0; i < dive_count; ++i) { unsigned int idx = (device->layout->rb_logbook_entry_count + head_dive - (i + 1)) % device->layout->rb_logbook_entry_count; unsigned char *log_entry = data->logbook + idx * device->layout->rb_logbook_entry_size; @@ -953,7 +954,7 @@ cochran_commander_device_foreach (dc_device_t *abstract, dc_dive_callback_t call } // Change tail to dive following the fingerprint dive. - if (data.fp_dive_num > -1) + if (data.fp_dive_num != UNDEFINED) tail_dive = (data.fp_dive_num + 1) % layout->rb_logbook_entry_count; // Number of dives to read diff --git a/src/context.c b/src/context.c index c7642e6..21c1610 100644 --- a/src/context.c +++ b/src/context.c @@ -27,6 +27,7 @@ #include #include #include +#include #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN @@ -77,7 +78,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 @@ -104,7 +105,7 @@ l_hexdump (char *str, size_t size, const unsigned char data[], size_t n) '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; - if (size == 0) + if (size == 0 || size > INT_MAX) return -1; /* The maximum number of bytes. */ @@ -126,11 +127,11 @@ l_hexdump (char *str, size_t size, const unsigned char data[], size_t n) /* Null terminate the hex string. */ str[length * 2] = 0; - return (n > maxlength ? -1 : length * 2); + return (n > maxlength ? -1 : (int) (length * 2)); } static void -logfunc (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *msg, void *userdata) +loghandler (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *msg, void *userdata) { const char *loglevels[] = {"NONE", "ERROR", "WARNING", "INFO", "DEBUG", "ALL"}; @@ -166,7 +167,7 @@ dc_context_new (dc_context_t **out) #ifdef ENABLE_LOGGING context->loglevel = DC_LOGLEVEL_WARNING; - context->logfunc = logfunc; + context->logfunc = loghandler; #else context->loglevel = DC_LOGLEVEL_NONE; context->logfunc = NULL; 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/hw_ostc_parser.c b/src/hw_ostc_parser.c index 117064b..714318b 100644 --- a/src/hw_ostc_parser.c +++ b/src/hw_ostc_parser.c @@ -390,7 +390,6 @@ hw_ostc_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) { hw_ostc_parser_t *parser = (hw_ostc_parser_t *) abstract; const unsigned char *data = abstract->data; - unsigned int size = abstract->size; // Cache the header data. dc_status_t rc = hw_ostc_parser_cache (parser); @@ -451,7 +450,6 @@ hw_ostc_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned { hw_ostc_parser_t *parser = (hw_ostc_parser_t *) abstract; const unsigned char *data = abstract->data; - unsigned int size = abstract->size; // Cache the header data. dc_status_t rc = hw_ostc_parser_cache (parser); diff --git a/src/irda.c b/src/irda.c index 448c57c..2cc58ab 100644 --- a/src/irda.c +++ b/src/irda.c @@ -155,7 +155,7 @@ dc_irda_iterator_new (dc_iterator_t **out, dc_context_t *context, dc_descriptor_ } // Open the socket. - int fd = socket (AF_IRDA, SOCK_STREAM, 0); + s_socket_t fd = socket (AF_IRDA, SOCK_STREAM, 0); if (fd == S_INVALID) { s_errcode_t errcode = S_ERRNO; SYSERROR (context, errcode); diff --git a/src/oceanic_atom2_parser.c b/src/oceanic_atom2_parser.c index e2a54ff..9ddb1d4 100644 --- a/src/oceanic_atom2_parser.c +++ b/src/oceanic_atom2_parser.c @@ -527,9 +527,7 @@ oceanic_atom2_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, uns { dc_status_t status = DC_STATUS_SUCCESS; oceanic_atom2_parser_t *parser = (oceanic_atom2_parser_t *) abstract; - const unsigned char *data = abstract->data; - unsigned int size = abstract->size; // Cache the header data. status = oceanic_atom2_parser_cache (parser); diff --git a/src/oceanic_vtpro.c b/src/oceanic_vtpro.c index 22d61fd..74b94ec 100644 --- a/src/oceanic_vtpro.c +++ b/src/oceanic_vtpro.c @@ -592,8 +592,8 @@ oceanic_vtpro_device_version (dc_device_t *abstract, unsigned char data[], unsig return rc; // Verify the checksum of the answer. - unsigned char crc = answer[PAGESIZE / 2]; - unsigned char ccrc = checksum_add_uint4 (answer, PAGESIZE / 2, 0x00); + crc = answer[PAGESIZE / 2]; + ccrc = checksum_add_uint4 (answer, PAGESIZE / 2, 0x00); if (crc != ccrc) { ERROR (abstract->context, "Unexpected answer checksum."); return DC_STATUS_PROTOCOL; diff --git a/src/reefnet_sensuspro_parser.c b/src/reefnet_sensuspro_parser.c index fe62668..7e68a72 100644 --- a/src/reefnet_sensuspro_parser.c +++ b/src/reefnet_sensuspro_parser.c @@ -163,8 +163,8 @@ reefnet_sensuspro_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, while (offset + sizeof (footer) <= size && memcmp (data + offset, footer, sizeof (footer)) != 0) { - unsigned int value = array_uint16_le (data + offset); - unsigned int depth = (value & 0x01FF); + unsigned int raw = array_uint16_le (data + offset); + unsigned int depth = (raw & 0x01FF); if (depth > maxdepth) maxdepth = depth; 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_d9_parser.c b/src/suunto_d9_parser.c index 6bf33b8..bd91743 100644 --- a/src/suunto_d9_parser.c +++ b/src/suunto_d9_parser.c @@ -348,9 +348,7 @@ static dc_status_t suunto_d9_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) { suunto_d9_parser_t *parser = (suunto_d9_parser_t*) abstract; - const unsigned char *data = abstract->data; - unsigned int size = abstract->size; // Cache the gas mix data. dc_status_t rc = suunto_d9_parser_cache (parser); diff --git a/src/suunto_eonsteel_parser.c b/src/suunto_eonsteel_parser.c index 7a4c032..033a2f2 100644 --- a/src/suunto_eonsteel_parser.c +++ b/src/suunto_eonsteel_parser.c @@ -95,7 +95,7 @@ typedef struct suunto_eonsteel_parser_t { } cache; } suunto_eonsteel_parser_t; -typedef int (*eon_data_cb_t)(unsigned short type, const struct type_desc *desc, const unsigned char *data, int len, void *user); +typedef int (*eon_data_cb_t)(unsigned short type, const struct type_desc *desc, const unsigned char *data, unsigned int len, void *user); typedef struct eon_event_t { const char *name; @@ -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; } @@ -366,10 +364,10 @@ static int record_type(suunto_eonsteel_parser_t *eon, unsigned short type, const return 0; } -static int traverse_entry(suunto_eonsteel_parser_t *eon, const unsigned char *p, int len, eon_data_cb_t callback, void *user) +static int traverse_entry(suunto_eonsteel_parser_t *eon, const unsigned char *p, int size, eon_data_cb_t callback, void *user) { - const unsigned char *name, *data, *end, *last, *one_past_end = p + len; - int textlen, type; + const unsigned char *name, *data, *end, *last, *one_past_end = p + size; + int textlen, id; int rc; // First two bytes: zero and text length @@ -388,7 +386,7 @@ static int traverse_entry(suunto_eonsteel_parser_t *eon, const unsigned char *p, // Two bytes of 'type' followed by the name/descriptor, followed by the data data = name + textlen; - type = array_uint16_le(name); + id = array_uint16_le(name); name += 2; if (*name != '<') { @@ -396,7 +394,7 @@ static int traverse_entry(suunto_eonsteel_parser_t *eon, const unsigned char *p, return -1; } - record_type(eon, type, (const char *) name, textlen-3); + record_type(eon, id, (const char *) name, textlen-3); end = data; last = data; @@ -860,7 +858,7 @@ static void sample_setpoint_automatic(struct sample_data *info, unsigned char va DEBUG(info->eon->base.context, "sample_setpoint_automatic(%u)", value); } -static int handle_sample_type(const struct type_desc *desc, struct sample_data *info, enum eon_sample type, const unsigned char *data) +static unsigned int handle_sample_type(const struct type_desc *desc, struct sample_data *info, enum eon_sample type, const unsigned char *data) { switch (type) { case ES_dtime: @@ -968,7 +966,7 @@ static int handle_sample_type(const struct type_desc *desc, struct sample_data * } } -static int traverse_samples(unsigned short type, const struct type_desc *desc, const unsigned char *data, int len, void *user) +static int traverse_samples(unsigned short type, const struct type_desc *desc, const unsigned char *data, unsigned int len, void *user) { struct sample_data *info = (struct sample_data *) user; suunto_eonsteel_parser_t *eon = info->eon; @@ -982,8 +980,7 @@ static int traverse_samples(unsigned short type, const struct type_desc *desc, c info->ceiling = 0.0; for (i = 0; i < EON_MAX_GROUP; i++) { - enum eon_sample type = desc->type[i]; - int bytes = handle_sample_type(desc, info, type, data); + unsigned int bytes = handle_sample_type(desc, info, desc->type[i], data); if (!bytes) break; @@ -1246,8 +1243,6 @@ static float get_le32_float(const unsigned char *src) static int traverse_device_fields(suunto_eonsteel_parser_t *eon, const struct type_desc *desc, const unsigned char *data, int len) { - const char *name = desc->desc + strlen("sml.DeviceLog.Device."); - return 0; } @@ -1429,13 +1424,15 @@ static int traverse_sample_fields(suunto_eonsteel_parser_t *eon, const struct ty set_depth_field(eon, array_uint16_le(data)); data += 2; continue; + default: + break; } break; } return 0; } -static int traverse_fields(unsigned short type, const struct type_desc *desc, const unsigned char *data, int len, void *user) +static int traverse_fields(unsigned short type, const struct type_desc *desc, const unsigned char *data, unsigned int len, void *user) { suunto_eonsteel_parser_t *eon = (suunto_eonsteel_parser_t *) user; diff --git a/src/suunto_solution_parser.c b/src/suunto_solution_parser.c index 9b2ac9d..34aab7d 100644 --- a/src/suunto_solution_parser.c +++ b/src/suunto_solution_parser.c @@ -110,10 +110,10 @@ suunto_solution_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, u unsigned int depth = 0, maxdepth = 0; unsigned int offset = 3; while (offset < size && data[offset] != 0x80) { - unsigned char value = data[offset++]; - if (value < 0x7e || value > 0x82) { - depth += (signed char) value; - if (value == 0x7D || value == 0x83) { + unsigned char raw = data[offset++]; + if (raw < 0x7e || raw > 0x82) { + depth += (signed char) raw; + if (raw == 0x7D || raw == 0x83) { if (offset + 1 > size) return DC_STATUS_DATAFORMAT; depth += (signed char) data[offset++]; 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/suunto_vyper_parser.c b/src/suunto_vyper_parser.c index 80f7d54..369ad5c 100644 --- a/src/suunto_vyper_parser.c +++ b/src/suunto_vyper_parser.c @@ -234,9 +234,7 @@ static dc_status_t suunto_vyper_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) { suunto_vyper_parser_t *parser = (suunto_vyper_parser_t *) abstract; - const unsigned char *data = abstract->data; - unsigned int size = abstract->size; dc_gasmix_t *gas = (dc_gasmix_t *) value; dc_tank_t *tank = (dc_tank_t *) value; 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; }