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 <noel.eck@intel.com>
This commit is contained in:
Noel Eck 2018-02-07 12:17:54 -08:00
parent 460fdc2eb5
commit fe7bd75c91
14 changed files with 21 additions and 24 deletions

View File

@ -104,6 +104,7 @@ endif (WERROR)
upm_add_compile_flags(C ${C_CXX_WARNING_FLAGS} upm_add_compile_flags(C ${C_CXX_WARNING_FLAGS}
-Winit-self -Winit-self
-Wimplicit -Wimplicit
-Wsign-compare
-Wmissing-parameter-type) -Wmissing-parameter-type)
# Set CXX compiler warning flags at top-level scope and emit a warning about # 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} upm_add_compile_flags(CXX ${C_CXX_WARNING_FLAGS}
-Wnon-virtual-dtor -Wnon-virtual-dtor
-Woverloaded-virtual -Woverloaded-virtual
-Wsign-compare
-Wreorder) -Wreorder)
# Allow exception error handling for Android C++ # Allow exception error handling for Android C++

View File

@ -62,8 +62,7 @@ int main(int argc, char **argv)
// update our values for all sensors // update our values for all sensors
ds18b20_update(sensor, -1); ds18b20_update(sensor, -1);
int i; for (unsigned int i=0; i<ds18b20_devices_found(sensor); i++)
for (i=0; i<ds18b20_devices_found(sensor); i++)
{ {
printf("Device %02d: Temperature: %f C\n", printf("Device %02d: Temperature: %f C\n",
i, ds18b20_get_temperature(sensor, i)); i, ds18b20_get_temperature(sensor, i));

View File

@ -318,7 +318,7 @@ upm_result_t bno055_write_regs(const bno055_context dev, uint8_t reg,
uint8_t buf[len + 1]; uint8_t buf[len + 1];
buf[0] = reg; buf[0] = reg;
for (int i=0; i<len; i++) for (size_t i=0; i<len; i++)
buf[i+1] = buffer[i]; buf[i+1] = buffer[i];
if (mraa_i2c_write(dev->i2c, buf, len + 1)) if (mraa_i2c_write(dev->i2c, buf, len + 1))

View File

@ -143,8 +143,7 @@ ds18b20_context ds18b20_init(unsigned int uart)
} }
// iterate through the found devices and query their resolutions // iterate through the found devices and query their resolutions
int i; for (unsigned int i=0; i<dev->numDevices; i++)
for (i=0; i<dev->numDevices; i++)
{ {
// read only the first 5 bytes of the scratchpad // read only the first 5 bytes of the scratchpad
static const int numScratch = 5; static const int numScratch = 5;
@ -191,7 +190,7 @@ void ds18b20_update(const ds18b20_context dev, int index)
{ {
assert(dev != NULL); assert(dev != NULL);
if (index >= dev->numDevices) if (index >= (int)dev->numDevices)
{ {
printf("%s: device index %d out of range\n", __FUNCTION__, index); printf("%s: device index %d out of range\n", __FUNCTION__, index);
return; 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 // convert command to all of them, then wait. This will be
// faster, timey-wimey wise, then converting, sleeping, and // faster, timey-wimey wise, then converting, sleeping, and
// reading each individual sensor. // reading each individual sensor.
for (unsigned int i=0; i<dev->numDevices; i++)
int i;
for (i=0; i<dev->numDevices; i++)
mraa_uart_ow_command(dev->ow, DS18B20_CMD_CONVERT, dev->devices[i].id); mraa_uart_ow_command(dev->ow, DS18B20_CMD_CONVERT, dev->devices[i].id);
} }
else else
@ -219,8 +216,7 @@ void ds18b20_update(const ds18b20_context dev, int index)
if (doAll) if (doAll)
{ {
int i; for (unsigned int i=0; i<dev->numDevices; i++)
for (i=0; i<dev->numDevices; i++)
dev->devices[i].temperature = readSingleTemp(dev, i); dev->devices[i].temperature = readSingleTemp(dev, i);
} }
else else

View File

@ -53,7 +53,7 @@ extern "C" {
mraa_uart_ow_context ow; mraa_uart_ow_context ow;
// number of devices found // number of devices found
int numDevices; unsigned int numDevices;
// list of allocated ds18b20_info_t instances // list of allocated ds18b20_info_t instances
ds18b20_info_t *devices; ds18b20_info_t *devices;

View File

@ -395,7 +395,7 @@ int ecezo_read(const ecezo_context dev, char *buffer, size_t len)
else else
{ {
// UART // UART
int bytesRead = 0; size_t bytesRead = 0;
while(bytesRead < len) 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 (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__); printf("%s: mraa_uart_write() failed.\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED; return UPM_ERROR_OPERATION_FAILED;

View File

@ -105,7 +105,7 @@ upm_result_t enc03r_calibrate(const enc03r_context dev,
float total = 0.0; float total = 0.0;
for (int i=0; i<samples; i++) for (unsigned int i=0; i<samples; i++)
{ {
int val = mraa_aio_read(dev->aio); int val = mraa_aio_read(dev->aio);
if (val < 0) if (val < 0)

View File

@ -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, upm_result_t m24lr64e_eeprom_read_byte(m24lr64e_context dev, uint32_t address,
uint8_t* data){ uint8_t* data){
uint32_t pkt_len = 2; int pkt_len = 2;
uint8_t buf[pkt_len]; uint8_t buf[pkt_len];
buf[0] = ((address >> 8) & 0xff); buf[0] = ((address >> 8) & 0xff);

View File

@ -315,7 +315,7 @@ upm_result_t mcp2515_bus_read(const mcp2515_context dev, uint8_t cmd,
if (args && arglen) if (args && arglen)
{ {
for (int i=0; i<arglen; i++) for (unsigned int i=0; i<arglen; i++)
sbuf[index++] = args[i]; sbuf[index++] = args[i];
} }

View File

@ -93,7 +93,7 @@ ppd42ns_dust_data ppd42ns_get_data(const ppd42ns_context dev)
ppd42ns_dust_data data; ppd42ns_dust_data data;
// in ms, 30 seconds // in ms, 30 seconds
const int pulse_check_time = 30000; const unsigned int pulse_check_time = 30000;
// loop timer // loop timer
upm_clock_t max_loop_time; upm_clock_t max_loop_time;

View File

@ -347,14 +347,14 @@ RN2903_RESPONSE_T rn2903_waitfor_response(const rn2903_context dev,
dev->resp_len++; dev->resp_len++;
} }
} while ( (elapsed = upm_elapsed_ms(&clock)) < wait_ms); } while ( (int)(elapsed = upm_elapsed_ms(&clock)) < wait_ms);
if (dev->debug) if (dev->debug)
printf("\tRESP (%d): '%s'\n", (int)dev->resp_len, printf("\tRESP (%d): '%s'\n", (int)dev->resp_len,
(dev->resp_len) ? dev->resp_data : ""); (dev->resp_len) ? dev->resp_data : "");
// check for and return obvious errors // check for and return obvious errors
if (elapsed >= wait_ms) if ((int)elapsed >= wait_ms)
return RN2903_RESPONSE_TIMEOUT; return RN2903_RESPONSE_TIMEOUT;
else if (rn2903_find(dev, RN2903_PHRASE_INV_PARAM)) else if (rn2903_find(dev, RN2903_PHRASE_INV_PARAM))
return RN2903_RESPONSE_INVALID_PARAM; return RN2903_RESPONSE_INVALID_PARAM;

View File

@ -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, upm_result_t es08a_calc_pulse_travelling(const es08a_context dev,
int32_t* ret_val, int32_t value){ 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; return dev->max_pulse_width;
} }

View File

@ -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, upm_result_t es9257_calc_pulse_travelling(const es9257_context dev,
int32_t* ret_val, int32_t value){ 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; return dev->max_pulse_width;
} }

View File

@ -268,7 +268,7 @@ int uartat_command_with_response(const uartat_context dev,
upm_clock_t clock; upm_clock_t clock;
upm_clock_init(&clock); upm_clock_init(&clock);
int idx = 0; size_t idx = 0;
do do
{ {
@ -323,7 +323,7 @@ bool uartat_command_waitfor(const uartat_context dev, const char *cmd,
upm_clock_t clock; upm_clock_t clock;
upm_clock_init(&clock); upm_clock_init(&clock);
int idx = 0; size_t idx = 0;
do do
{ {