Skip to content

Commit

Permalink
fix issues pointed out by no-sign-compare removal
Browse files Browse the repository at this point in the history
By default comparing signed with unsigned, the compiler converts the
signed value to unsigned, this can cause problems sometimes, as that
would mean -1 > 2U.

In channel.c, context.c, device.c the lengths of strings to a negative
number, so cast to a signed number in all cases.

In dns_sd.c, local.c, tests/iio_stresstest.c we had an iterator, that
was signed, that should have been unsigned (now it is).

In local.c:get_rel_timeout_ms we move the comparision to signed.

in local.c, while pointers are unsigned integers, subtraction of two
pointers are ptrdiff_t, or a signed integer. Since we are just measuring
length of a string, cast to unsigned.

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz committed May 4, 2020
1 parent 15bba24 commit 4ca68fe
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,14 @@ char * iio_channel_get_xml(const struct iio_channel *chn, size_t *length)
len = eptr - ptr;
}

if (chn->is_scan_element && len > scan_element_len) {
if (chn->is_scan_element && len > (ssize_t) scan_element_len) {
memcpy(ptr, scan_element, scan_element_len); /* Flawfinder: ignore */
ptr += scan_element_len;
len -= scan_element_len;
}

for (i = 0; i < chn->nb_attrs; i++) {
if (len > attrs_len[i]) {
if (len > (ssize_t) attrs_len[i]) {
memcpy(ptr, attrs[i], attrs_len[i]); /* Flawfinder: ignore */
ptr += attrs_len[i];
len -= attrs_len[i];
Expand Down
2 changes: 1 addition & 1 deletion context.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ char * iio_context_create_xml(const struct iio_context *ctx)
}

for (i = 0; i < ctx->nb_devices; i++) {
if (len > devices_len[i]) {
if (len > (ssize_t) devices_len[i]) {
memcpy(ptr, devices[i], devices_len[i]); /* Flawfinder: ignore */
ptr += devices_len[i];
len -= devices_len[i];
Expand Down
8 changes: 4 additions & 4 deletions device.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ char * iio_device_get_xml(const struct iio_device *dev, size_t *length)
}

for (i = 0; i < dev->nb_channels; i++) {
if (len > channels_len[i]) {
if (len > (ssize_t) channels_len[i]) {
memcpy(ptr, channels[i], channels_len[i]); /* Flawfinder: ignore */
ptr += channels_len[i];
len -= channels_len[i];
Expand All @@ -185,7 +185,7 @@ char * iio_device_get_xml(const struct iio_device *dev, size_t *length)
free(channels_len);

for (i = 0; i < dev->nb_attrs; i++) {
if (len > attrs_len[i]) {
if (len > (ssize_t) attrs_len[i]) {
memcpy(ptr, attrs[i], attrs_len[i]); /* Flawfinder: ignore */
ptr += attrs_len[i];
len -= attrs_len[i];
Expand All @@ -197,7 +197,7 @@ char * iio_device_get_xml(const struct iio_device *dev, size_t *length)
free(attrs_len);

for (i = 0; i < dev->nb_buffer_attrs; i++) {
if (len > buffer_attrs_len[i]) {
if (len > (ssize_t) buffer_attrs_len[i]) {
memcpy(ptr, buffer_attrs[i], buffer_attrs_len[i]); /* Flawfinder: ignore */
ptr += buffer_attrs_len[i];
len -= buffer_attrs_len[i];
Expand All @@ -209,7 +209,7 @@ char * iio_device_get_xml(const struct iio_device *dev, size_t *length)
free(buffer_attrs_len);

for (i = 0; i < dev->nb_debug_attrs; i++) {
if (len > debug_attrs_len[i]) {
if (len > (ssize_t) debug_attrs_len[i]) {
memcpy(ptr, debug_attrs[i], debug_attrs_len[i]); /* Flawfinder: ignore */
ptr += debug_attrs_len[i];
len -= debug_attrs_len[i];
Expand Down
2 changes: 1 addition & 1 deletion dns_sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static int dnssd_fill_context_info(struct iio_context_info *info,
char uri[sizeof("ip:") + MAXHOSTNAMELEN + sizeof (":65535") + 1];
char description[255], *p;
const char *hw_model, *serial;
int i;
unsigned int i;

ctx = network_create_context(addr_str);
if (!ctx) {
Expand Down
4 changes: 2 additions & 2 deletions iiod/ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ static void rw_thd(struct thread_pool *pool, void *d)

if (ret < 0 || thd->nb < sample_size)
signal_thread(thd, (ret < 0) ?
ret : thd->nb);
ret : (ssize_t) thd->nb);
}

pthread_mutex_unlock(&entry->thdlist_lock);
Expand Down Expand Up @@ -820,7 +820,7 @@ static ssize_t rw_buffer(struct parser_pdata *pdata,
ret = thd->err;
pthread_mutex_unlock(&entry->thdlist_lock);

if (ret > 0 && ret < nb)
if (ret > 0 && ret < (ssize_t) nb)
print_value(thd->pdata, 0);

IIO_DEBUG("Exiting rw_buffer with code %li\n", (long) ret);
Expand Down
6 changes: 3 additions & 3 deletions local.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ static int get_rel_timeout_ms(struct timespec *start, unsigned int timeout_rel)
diff_ms = (now.tv_sec - start->tv_sec) * 1000;
diff_ms += (now.tv_nsec - start->tv_nsec) / 1000000;

if (diff_ms >= timeout_rel) /* Expired */
if (diff_ms >= (int) timeout_rel) /* Expired */
return 0;
if (diff_ms > 0) /* Should never be false, but lets be safe */
timeout_rel -= diff_ms;
Expand Down Expand Up @@ -1499,7 +1499,7 @@ static unsigned int is_global_attr(struct iio_channel *chn, const char *attr)
unsigned int len2 = ptr - dashptr - 1;
const char* iddashptr = strchr(chn->id, '-');
if (iddashptr && strlen(iddashptr + 1) > len2 &&
iddashptr - chn->id > len1 &&
(unsigned int)(iddashptr - chn->id) > len1 &&
chn->id[len1] >= '0' && chn->id[len1] <= '9' &&
!strncmp(chn->id, attr, len1) &&
iddashptr[len2 + 1] >= '0' && iddashptr[len2 + 1] <= '9' &&
Expand Down Expand Up @@ -1612,7 +1612,7 @@ static int add_buffer_attr(void *d, const char *path)
struct iio_device *dev = (struct iio_device *) d;
const char *name = strrchr(path, '/') + 1;
char **attrs, *attr;
int i;
unsigned int i;

for (i = 0; i < ARRAY_SIZE(buffer_attrs_reserved); i++)
if (!strcmp(buffer_attrs_reserved[i], name))
Expand Down
2 changes: 1 addition & 1 deletion tests/iio_stresstest.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ int main(int argc, char **argv)
}

/* Calculate some stats about the threads */
int a =0, b = 0;
unsigned int a =0, b = 0;
c = 0;
for (i = 0; i < info.num_threads; i++) {
a+= info.starts[i];
Expand Down
2 changes: 1 addition & 1 deletion usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ struct iio_context * usb_create_context_from_uri(const char *uri)
if (bus < 0 || address < 0 || interface < 0)
goto err_bad_uri;

if (bus > UINT_MAX || address > UINT8_MAX || interface > UINT8_MAX)
if (bus > (long) UINT_MAX || address > UINT8_MAX || interface > UINT8_MAX)
goto err_bad_uri;

return usb_create_context((unsigned int) bus,
Expand Down

0 comments on commit 4ca68fe

Please sign in to comment.