From b97acabb01446553acd76195174f2118eca6f389 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Fri, 17 Jul 2020 14:11:15 +0200 Subject: [PATCH 01/11] Remove unused variables --- src/hw_ostc_parser.c | 2 -- src/oceanic_atom2_parser.c | 2 -- src/suunto_d9_parser.c | 2 -- src/suunto_eonsteel_parser.c | 2 -- src/suunto_vyper_parser.c | 2 -- 5 files changed, 10 deletions(-) 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/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/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..3c4040b 100644 --- a/src/suunto_eonsteel_parser.c +++ b/src/suunto_eonsteel_parser.c @@ -1246,8 +1246,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; } 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; From 548fce69f8a21525a934e2ef050dc0d394b374f4 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Fri, 17 Jul 2020 14:24:49 +0200 Subject: [PATCH 02/11] Fix -Wswitch compiler warning Add a default label to prevent warnings for all enum values not handled in the switch statement. It's intentional in this piece of code. --- src/suunto_eonsteel_parser.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/suunto_eonsteel_parser.c b/src/suunto_eonsteel_parser.c index 3c4040b..0d519cd 100644 --- a/src/suunto_eonsteel_parser.c +++ b/src/suunto_eonsteel_parser.c @@ -1427,6 +1427,8 @@ 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; } From 7c9726da64db1477e71c16b3a93353fd5826c41e Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Sat, 18 Jul 2020 00:26:42 +0200 Subject: [PATCH 03/11] Fix -Wcast-qual compiler warning String literals have the type 'char[N]' by default. Allthough they are not really 'const', modifying a string literal is undefined behaviour. Therefore, to avoid mistakes, libdivecomputer uses the -Wwrite-strings compiler option to change the default type to 'const char[N]'. The cast that triggers the -Wcast-qual warning can be avoided by using a character array instead of string literal. --- examples/dctool.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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]; From 8f383ac5314126d68afd71265a0c1bf866aa9ab7 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Sat, 18 Jul 2020 00:28:18 +0200 Subject: [PATCH 04/11] Fix -Wshadow compiler warnings Rename a few variables, parameters and functions to avoid shadowing others. --- src/context.c | 4 ++-- src/oceanic_vtpro.c | 4 ++-- src/reefnet_sensuspro_parser.c | 4 ++-- src/suunto_eonsteel_parser.c | 13 ++++++------- src/suunto_solution_parser.c | 8 ++++---- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/context.c b/src/context.c index c7642e6..88fd9b5 100644 --- a/src/context.c +++ b/src/context.c @@ -130,7 +130,7 @@ l_hexdump (char *str, size_t size, const unsigned char data[], size_t n) } 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 +166,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/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_eonsteel_parser.c b/src/suunto_eonsteel_parser.c index 0d519cd..d8dcdcc 100644 --- a/src/suunto_eonsteel_parser.c +++ b/src/suunto_eonsteel_parser.c @@ -366,10 +366,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 +388,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 +396,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; @@ -982,8 +982,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); + int bytes = handle_sample_type(desc, info, desc->type[i], data); if (!bytes) break; 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++]; From 1130b7eadef1c7688c8b0386b4dd479f216553cb Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Mon, 20 Jul 2020 00:06:12 +0200 Subject: [PATCH 05/11] 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; } From 0688b74099bc8550cfed55ad3a6aa8a06e4af17c Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Mon, 20 Jul 2020 00:24:17 +0200 Subject: [PATCH 06/11] Use an unsigned integer for the length This avoids -Wsign-compare compiler warnings due to comparison of integer expressions of different signedness. --- src/suunto_eonsteel_parser.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/suunto_eonsteel_parser.c b/src/suunto_eonsteel_parser.c index a941518..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; @@ -858,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: @@ -966,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; @@ -980,7 +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++) { - int bytes = handle_sample_type(desc, info, desc->type[i], data); + unsigned int bytes = handle_sample_type(desc, info, desc->type[i], data); if (!bytes) break; @@ -1432,7 +1432,7 @@ static int traverse_sample_fields(suunto_eonsteel_parser_t *eon, const struct ty 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; From 10a4ec0b08af7fc7ae29d69c8598a3d1f9eca5b0 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Wed, 22 Jul 2020 00:12:24 +0200 Subject: [PATCH 07/11] Define DC_TIMEZONE_NONE as a signed integer The hexadecimal value 0x80000000 is too large to be represented as a signed 32bit integer. Therefore the default type for the constant is an unsigned 32bit integer. This is a bit annoying because the timezone field is actually defined as a signed integer, and thus comparisions produce -Wsign-compare compiler warnings. Fixed by switching to INT_MIN, which is the same underlying value but interpreted as a signed integer. --- include/libdivecomputer/datetime.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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; From b0cce363f1e39a55924d5379e22a668185793fef Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Wed, 22 Jul 2020 00:26:07 +0200 Subject: [PATCH 08/11] Limit the size to INT_MAX MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allthough the input buffer size has type 'size_t', the return value of the function has only type 'int'. Hence the function can't support input buffers larger than INT_MAX. This allows to fix a -Wsign-compare compiler warning: operand of ?: changes signedness from ‘int’ to ‘size_t’ due to unsignedness of other operand. --- src/context.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/context.c b/src/context.c index cab6f33..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 @@ -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,7 +127,7 @@ 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 From fc76b4a25872d71341203fd69c7d72524e628ecf Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Mon, 24 Aug 2020 13:24:54 +0200 Subject: [PATCH 09/11] Use the cross-platform socket file descriptor type Windows and unix use a diferent data type for representing socket file descriptors (SOCKET vs int). This mismatch results in a compiler warning when comparing to S_INVALID. --- src/irda.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); From 5eddaeade6c4574a93516b1183e3283382324179 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Fri, 4 Sep 2020 07:58:03 +0200 Subject: [PATCH 10/11] Use an unsigned integer for the number of dives Because the dive_count variable is decremented, it doesn't contain the total number of dives, but the index of the last dive. A negative number indicates no dives. The same result can be obtained by using the total number of dives directly. That's not only more intuitive, but also fixes a -Wsign-compare compiler warning. --- src/cochran_commander.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/cochran_commander.c b/src/cochran_commander.c index 63a00ba..7e8348e 100644 --- a/src/cochran_commander.c +++ b/src/cochran_commander.c @@ -621,7 +621,7 @@ 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; + unsigned int dive_count = 0; data->fp_dive_num = -1; // Start at end of log @@ -629,7 +629,6 @@ cochran_commander_find_fingerprint(cochran_commander_device_t *device, cochran_d 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; @@ -676,7 +675,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; From ba6a8a43f722bd0b2f3fca953e119d0002ec2733 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Fri, 4 Sep 2020 08:10:00 +0200 Subject: [PATCH 11/11] Use an unsigned value to represent the undefined state This fixes some more -Wsign-compare compiler warnings. --- src/cochran_commander.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/cochran_commander.c b/src/cochran_commander.c index 7e8348e..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; @@ -622,7 +624,7 @@ cochran_commander_find_fingerprint(cochran_commander_device_t *device, cochran_d int profile_capacity_remaining = device->layout->rb_profile_end - device->layout->rb_profile_begin; unsigned int dive_count = 0; - data->fp_dive_num = -1; + data->fp_dive_num = UNDEFINED; // Start at end of log if (data->dive_count < device->layout->rb_logbook_entry_count) @@ -631,7 +633,7 @@ cochran_commander_find_fingerprint(cochran_commander_device_t *device, cochran_d dive_count = device->layout->rb_logbook_entry_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; @@ -952,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