From fe7bd75c9170c1db5dc2bc4f54d80b29cad87676 Mon Sep 17 00:00:00 2001 From: Noel Eck Date: Wed, 7 Feb 2018 12:17:54 -0800 Subject: [PATCH] C: Fixes for sign compares in C libraries Added explicit error for sign compares to CMake. Updated a handful of C source which compared unsigned vs signed. Signed-off-by: Noel Eck --- CMakeLists.txt | 2 ++ examples/c/ds18b20.c | 3 +-- src/bno055/bno055.c | 2 +- src/ds18b20/ds18b20.c | 12 ++++-------- src/ds18b20/ds18b20.h | 2 +- src/ecezo/ecezo.c | 4 ++-- src/enc03r/enc03r.c | 2 +- src/m24lr64e/m24lr64e.c | 2 +- src/mcp2515/mcp2515.c | 2 +- src/ppd42ns/ppd42ns.c | 2 +- src/rn2903/rn2903.c | 4 ++-- src/servo/es08a.c | 2 +- src/servo/es9257.c | 2 +- src/uartat/uartat.c | 4 ++-- 14 files changed, 21 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 73922976..2b6584d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,6 +104,7 @@ endif (WERROR) upm_add_compile_flags(C ${C_CXX_WARNING_FLAGS} -Winit-self -Wimplicit + -Wsign-compare -Wmissing-parameter-type) # Set CXX compiler warning flags at top-level scope and emit a warning about @@ -111,6 +112,7 @@ upm_add_compile_flags(C ${C_CXX_WARNING_FLAGS} upm_add_compile_flags(CXX ${C_CXX_WARNING_FLAGS} -Wnon-virtual-dtor -Woverloaded-virtual + -Wsign-compare -Wreorder) # Allow exception error handling for Android C++ diff --git a/examples/c/ds18b20.c b/examples/c/ds18b20.c index 483f366e..4efc4426 100644 --- a/examples/c/ds18b20.c +++ b/examples/c/ds18b20.c @@ -62,8 +62,7 @@ int main(int argc, char **argv) // update our values for all sensors ds18b20_update(sensor, -1); - int i; - for (i=0; ii2c, buf, len + 1)) diff --git a/src/ds18b20/ds18b20.c b/src/ds18b20/ds18b20.c index 872c50c0..cc2fec9c 100644 --- a/src/ds18b20/ds18b20.c +++ b/src/ds18b20/ds18b20.c @@ -143,8 +143,7 @@ ds18b20_context ds18b20_init(unsigned int uart) } // iterate through the found devices and query their resolutions - int i; - for (i=0; inumDevices; i++) + for (unsigned int i=0; inumDevices; i++) { // read only the first 5 bytes of the scratchpad static const int numScratch = 5; @@ -191,7 +190,7 @@ void ds18b20_update(const ds18b20_context dev, int index) { assert(dev != NULL); - if (index >= dev->numDevices) + if (index >= (int)dev->numDevices) { printf("%s: device index %d out of range\n", __FUNCTION__, index); return; @@ -206,9 +205,7 @@ void ds18b20_update(const ds18b20_context dev, int index) // convert command to all of them, then wait. This will be // faster, timey-wimey wise, then converting, sleeping, and // reading each individual sensor. - - int i; - for (i=0; inumDevices; i++) + for (unsigned int i=0; inumDevices; i++) mraa_uart_ow_command(dev->ow, DS18B20_CMD_CONVERT, dev->devices[i].id); } else @@ -219,8 +216,7 @@ void ds18b20_update(const ds18b20_context dev, int index) if (doAll) { - int i; - for (i=0; inumDevices; i++) + for (unsigned int i=0; inumDevices; i++) dev->devices[i].temperature = readSingleTemp(dev, i); } else diff --git a/src/ds18b20/ds18b20.h b/src/ds18b20/ds18b20.h index f0010cb4..15988c42 100644 --- a/src/ds18b20/ds18b20.h +++ b/src/ds18b20/ds18b20.h @@ -53,7 +53,7 @@ extern "C" { mraa_uart_ow_context ow; // number of devices found - int numDevices; + unsigned int numDevices; // list of allocated ds18b20_info_t instances ds18b20_info_t *devices; diff --git a/src/ecezo/ecezo.c b/src/ecezo/ecezo.c index 222bc26b..6a195fc1 100644 --- a/src/ecezo/ecezo.c +++ b/src/ecezo/ecezo.c @@ -395,7 +395,7 @@ int ecezo_read(const ecezo_context dev, char *buffer, size_t len) else { // UART - int bytesRead = 0; + size_t bytesRead = 0; while(bytesRead < len) { @@ -437,7 +437,7 @@ upm_result_t ecezo_write(const ecezo_context dev, char *buffer, size_t len) if (dev->uart) { - if (mraa_uart_write(dev->uart, buffer, len) != len) + if (mraa_uart_write(dev->uart, buffer, len) != (int)len) { printf("%s: mraa_uart_write() failed.\n", __FUNCTION__); return UPM_ERROR_OPERATION_FAILED; diff --git a/src/enc03r/enc03r.c b/src/enc03r/enc03r.c index b01a11a0..5b73b90b 100644 --- a/src/enc03r/enc03r.c +++ b/src/enc03r/enc03r.c @@ -105,7 +105,7 @@ upm_result_t enc03r_calibrate(const enc03r_context dev, float total = 0.0; - for (int i=0; iaio); if (val < 0) diff --git a/src/m24lr64e/m24lr64e.c b/src/m24lr64e/m24lr64e.c index 3f5d8f6f..a6d39bf3 100644 --- a/src/m24lr64e/m24lr64e.c +++ b/src/m24lr64e/m24lr64e.c @@ -303,7 +303,7 @@ upm_result_t m24lr64e_eeprom_write_bytes(m24lr64e_context dev, upm_result_t m24lr64e_eeprom_read_byte(m24lr64e_context dev, uint32_t address, uint8_t* data){ - uint32_t pkt_len = 2; + int pkt_len = 2; uint8_t buf[pkt_len]; buf[0] = ((address >> 8) & 0xff); diff --git a/src/mcp2515/mcp2515.c b/src/mcp2515/mcp2515.c index 082ed5fb..376077f1 100644 --- a/src/mcp2515/mcp2515.c +++ b/src/mcp2515/mcp2515.c @@ -315,7 +315,7 @@ upm_result_t mcp2515_bus_read(const mcp2515_context dev, uint8_t cmd, if (args && arglen) { - for (int i=0; iresp_len++; } - } while ( (elapsed = upm_elapsed_ms(&clock)) < wait_ms); + } while ( (int)(elapsed = upm_elapsed_ms(&clock)) < wait_ms); if (dev->debug) printf("\tRESP (%d): '%s'\n", (int)dev->resp_len, (dev->resp_len) ? dev->resp_data : ""); // check for and return obvious errors - if (elapsed >= wait_ms) + if ((int)elapsed >= wait_ms) return RN2903_RESPONSE_TIMEOUT; else if (rn2903_find(dev, RN2903_PHRASE_INV_PARAM)) return RN2903_RESPONSE_INVALID_PARAM; diff --git a/src/servo/es08a.c b/src/servo/es08a.c index bf055fc6..97b5725b 100644 --- a/src/servo/es08a.c +++ b/src/servo/es08a.c @@ -86,7 +86,7 @@ upm_result_t es08a_set_angle(es08a_context dev, int32_t angle){ upm_result_t es08a_calc_pulse_travelling(const es08a_context dev, int32_t* ret_val, int32_t value){ - if (value > dev->max_pulse_width) { + if (value > (int)dev->max_pulse_width) { return dev->max_pulse_width; } diff --git a/src/servo/es9257.c b/src/servo/es9257.c index 4ba2bc7e..c55cc44f 100644 --- a/src/servo/es9257.c +++ b/src/servo/es9257.c @@ -87,7 +87,7 @@ upm_result_t es9257_set_angle(es9257_context dev, int32_t angle){ upm_result_t es9257_calc_pulse_travelling(const es9257_context dev, int32_t* ret_val, int32_t value){ - if (value > dev->max_pulse_width) { + if (value > (int)dev->max_pulse_width) { return dev->max_pulse_width; } diff --git a/src/uartat/uartat.c b/src/uartat/uartat.c index 5597504b..51516d62 100644 --- a/src/uartat/uartat.c +++ b/src/uartat/uartat.c @@ -268,7 +268,7 @@ int uartat_command_with_response(const uartat_context dev, upm_clock_t clock; upm_clock_init(&clock); - int idx = 0; + size_t idx = 0; do { @@ -323,7 +323,7 @@ bool uartat_command_waitfor(const uartat_context dev, const char *cmd, upm_clock_t clock; upm_clock_init(&clock); - int idx = 0; + size_t idx = 0; do {