Skip to content

Commit 4ca68fe

Browse files
committed
fix issues pointed out by no-sign-compare removal
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]>
1 parent 15bba24 commit 4ca68fe

File tree

8 files changed

+15
-15
lines changed

8 files changed

+15
-15
lines changed

channel.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,14 @@ char * iio_channel_get_xml(const struct iio_channel *chn, size_t *length)
293293
len = eptr - ptr;
294294
}
295295

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

302302
for (i = 0; i < chn->nb_attrs; i++) {
303-
if (len > attrs_len[i]) {
303+
if (len > (ssize_t) attrs_len[i]) {
304304
memcpy(ptr, attrs[i], attrs_len[i]); /* Flawfinder: ignore */
305305
ptr += attrs_len[i];
306306
len -= attrs_len[i];

context.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ char * iio_context_create_xml(const struct iio_context *ctx)
116116
}
117117

118118
for (i = 0; i < ctx->nb_devices; i++) {
119-
if (len > devices_len[i]) {
119+
if (len > (ssize_t) devices_len[i]) {
120120
memcpy(ptr, devices[i], devices_len[i]); /* Flawfinder: ignore */
121121
ptr += devices_len[i];
122122
len -= devices_len[i];

device.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ char * iio_device_get_xml(const struct iio_device *dev, size_t *length)
173173
}
174174

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

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

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

211211
for (i = 0; i < dev->nb_debug_attrs; i++) {
212-
if (len > debug_attrs_len[i]) {
212+
if (len > (ssize_t) debug_attrs_len[i]) {
213213
memcpy(ptr, debug_attrs[i], debug_attrs_len[i]); /* Flawfinder: ignore */
214214
ptr += debug_attrs_len[i];
215215
len -= debug_attrs_len[i];

dns_sd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static int dnssd_fill_context_info(struct iio_context_info *info,
7474
char uri[sizeof("ip:") + MAXHOSTNAMELEN + sizeof (":65535") + 1];
7575
char description[255], *p;
7676
const char *hw_model, *serial;
77-
int i;
77+
unsigned int i;
7878

7979
ctx = network_create_context(addr_str);
8080
if (!ctx) {

iiod/ops.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ static void rw_thd(struct thread_pool *pool, void *d)
674674

675675
if (ret < 0 || thd->nb < sample_size)
676676
signal_thread(thd, (ret < 0) ?
677-
ret : thd->nb);
677+
ret : (ssize_t) thd->nb);
678678
}
679679

680680
pthread_mutex_unlock(&entry->thdlist_lock);
@@ -820,7 +820,7 @@ static ssize_t rw_buffer(struct parser_pdata *pdata,
820820
ret = thd->err;
821821
pthread_mutex_unlock(&entry->thdlist_lock);
822822

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

826826
IIO_DEBUG("Exiting rw_buffer with code %li\n", (long) ret);

local.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ static int get_rel_timeout_ms(struct timespec *start, unsigned int timeout_rel)
266266
diff_ms = (now.tv_sec - start->tv_sec) * 1000;
267267
diff_ms += (now.tv_nsec - start->tv_nsec) / 1000000;
268268

269-
if (diff_ms >= timeout_rel) /* Expired */
269+
if (diff_ms >= (int) timeout_rel) /* Expired */
270270
return 0;
271271
if (diff_ms > 0) /* Should never be false, but lets be safe */
272272
timeout_rel -= diff_ms;
@@ -1499,7 +1499,7 @@ static unsigned int is_global_attr(struct iio_channel *chn, const char *attr)
14991499
unsigned int len2 = ptr - dashptr - 1;
15001500
const char* iddashptr = strchr(chn->id, '-');
15011501
if (iddashptr && strlen(iddashptr + 1) > len2 &&
1502-
iddashptr - chn->id > len1 &&
1502+
(unsigned int)(iddashptr - chn->id) > len1 &&
15031503
chn->id[len1] >= '0' && chn->id[len1] <= '9' &&
15041504
!strncmp(chn->id, attr, len1) &&
15051505
iddashptr[len2 + 1] >= '0' && iddashptr[len2 + 1] <= '9' &&
@@ -1612,7 +1612,7 @@ static int add_buffer_attr(void *d, const char *path)
16121612
struct iio_device *dev = (struct iio_device *) d;
16131613
const char *name = strrchr(path, '/') + 1;
16141614
char **attrs, *attr;
1615-
int i;
1615+
unsigned int i;
16161616

16171617
for (i = 0; i < ARRAY_SIZE(buffer_attrs_reserved); i++)
16181618
if (!strcmp(buffer_attrs_reserved[i], name))

tests/iio_stresstest.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ int main(int argc, char **argv)
604604
}
605605

606606
/* Calculate some stats about the threads */
607-
int a =0, b = 0;
607+
unsigned int a =0, b = 0;
608608
c = 0;
609609
for (i = 0; i < info.num_threads; i++) {
610610
a+= info.starts[i];

usb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ struct iio_context * usb_create_context_from_uri(const char *uri)
11061106
if (bus < 0 || address < 0 || interface < 0)
11071107
goto err_bad_uri;
11081108

1109-
if (bus > UINT_MAX || address > UINT8_MAX || interface > UINT8_MAX)
1109+
if (bus > (long) UINT_MAX || address > UINT8_MAX || interface > UINT8_MAX)
11101110
goto err_bad_uri;
11111111

11121112
return usb_create_context((unsigned int) bus,

0 commit comments

Comments
 (0)