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.

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz committed Apr 27, 2020
1 parent a753665 commit 7c4840c
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 12 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 @@ -117,7 +117,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 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 @@ -1600,7 +1600,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_readdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ int main(int argc, char **argv)
const char *arg_ip = NULL;
int c, option_index = 0;
struct iio_device *dev;
size_t sample_size;
ssize_t sample_size;
int timeout = -1;
bool scan_for_context = false;
ssize_t ret;
Expand Down
2 changes: 1 addition & 1 deletion tests/iio_stresstest.c
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,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

0 comments on commit 7c4840c

Please sign in to comment.