Skip to content

Commit

Permalink
warnings: fix multiple signed and unsigned integer expressions
Browse files Browse the repository at this point in the history
This fixes many many instances of the following warnings.

warning: comparison between signed and unsigned integer expressions
[-Wsign-compare]

Unfortunately the fix includes lots of type casts.  Each of them was
considered from overflow point of view, and will hopefully not cause any
issues.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
  • Loading branch information
kerolasa committed Oct 3, 2018
1 parent 81221ac commit d16648c
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 48 deletions.
8 changes: 4 additions & 4 deletions arping.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ void print_hex(unsigned char *p, int len)
}
}

int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM,
int recv_pack(unsigned char *buf, ssize_t len, struct sockaddr_ll *FROM,
struct in_addr src, struct in_addr dst)
{
struct timespec ts;
Expand Down Expand Up @@ -431,7 +431,7 @@ int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM,
return 0;
if (ah->ar_hln != ((struct sockaddr_ll *)&me)->sll_halen)
return 0;
if (len < sizeof(*ah) + 2*(4 + ah->ar_hln))
if (len < (ssize_t) sizeof(*ah) + 2*(4 + ah->ar_hln))
return 0;
memcpy(&src_ip, p+ah->ar_hln, 4);
memcpy(&dst_ip, p+ah->ar_hln+4+ah->ar_hln, 4);
Expand Down Expand Up @@ -704,7 +704,7 @@ static int sysfs_devattr_ulong_hex(char *ptr, struct sysfs_devattr_values *v, un
static int sysfs_devattr_macaddr(char *ptr, struct sysfs_devattr_values *v, unsigned int idx)
{
unsigned char *m;
int i;
unsigned int i;
unsigned int addrlen;

if (!ptr || !v)
Expand Down Expand Up @@ -1210,7 +1210,7 @@ main(int argc, char **argv)
unsigned char packet[4096];
struct sockaddr_storage from;
socklen_t alen = sizeof(from);
int cc;
ssize_t cc;

sigemptyset(&sset);
sigaddset(&sset, SIGALRM);
Expand Down
12 changes: 6 additions & 6 deletions ninfod/ni_ifaddrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ struct rtmaddr_ifamap {
#endif

/* ====================================================================== */
static int nl_sendreq(int sd, int request, int flags, int *seq)
static int nl_sendreq(int sd, int request, int flags, uint32_t *seq)
{
char reqbuf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))];
struct sockaddr_nl nladdr;
Expand Down Expand Up @@ -160,7 +160,7 @@ static int nl_recvmsg(int sd, int request, int seq, void *buf, size_t buflen, in
return read_len;
}

static int nl_getmsg(int sd, int request, int seq, struct nlmsghdr **nlhp, int *done)
static int nl_getmsg(int sd, int request, uint32_t seq, struct nlmsghdr **nlhp, int *done)
{
struct nlmsghdr *nh;
size_t bufsize = 65536, lastbufsize = 0;
Expand All @@ -186,7 +186,7 @@ static int nl_getmsg(int sd, int request, int seq, struct nlmsghdr **nlhp, int *
break;
nh = (struct nlmsghdr *) buff;
for (nh = (struct nlmsghdr *) buff; NLMSG_OK(nh, read_size); nh = (struct nlmsghdr *) NLMSG_NEXT(nh, read_size)) {
if (nh->nlmsg_pid != pid || nh->nlmsg_seq != seq)
if ((pid_t) nh->nlmsg_pid != pid || nh->nlmsg_seq != seq)
continue;
if (nh->nlmsg_type == NLMSG_DONE) {
(*done)++;
Expand Down Expand Up @@ -215,7 +215,7 @@ static int nl_getmsg(int sd, int request, int seq, struct nlmsghdr **nlhp, int *
return result;
}

static int nl_getlist(int sd, int seq, int request, struct nlmsg_list **nlm_list, struct nlmsg_list **nlm_end)
static int nl_getlist(int sd, uint32_t seq, int request, struct nlmsg_list **nlm_list, struct nlmsg_list **nlm_end)
{
struct nlmsghdr *nlh = NULL;
int status;
Expand Down Expand Up @@ -252,7 +252,7 @@ static int nl_getlist(int sd, int seq, int request, struct nlmsg_list **nlm_list
}
}
}
return status >= 0 ? seq : status;
return status >= 0 ? (int) seq : status;
}

/* ---------------------------------------------------------------------- */
Expand Down Expand Up @@ -392,7 +392,7 @@ int ni_ifaddrs(struct ni_ifaddrs **ifap, sa_family_t family)
#endif

/* check if the message is what we want */
if (nlh->nlmsg_pid != pid || nlh->nlmsg_seq != nlm->seq)
if ((pid_t) nlh->nlmsg_pid != pid || nlh->nlmsg_seq != nlm->seq)
continue;
if (nlh->nlmsg_type == NLMSG_DONE) {
break; /* ok */
Expand Down
2 changes: 1 addition & 1 deletion ninfod/ninfod.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct packetcontext {
socklen_t addrlen;
struct in6_pktinfo pktinfo;
char query[MAX_QUERY_SIZE];
int querylen;
size_t querylen;

/* reply info */
struct icmp6_nodeinfo reply; /* common */
Expand Down
6 changes: 3 additions & 3 deletions ninfod/ninfod_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static struct subjinfo subjinfo_null = {
.checksubj = pr_nodeinfo_noop,
};

static __inline__ struct subjinfo *subjinfo_lookup(int code)
static __inline__ struct subjinfo *subjinfo_lookup(size_t code)
{
if (code >= ARRAY_SIZE(subjinfo_table))
return NULL;
Expand Down Expand Up @@ -224,7 +224,7 @@ static struct qtypeinfo qtypeinfo_refused = {
.flags = QTYPEINFO_F_RATELIMIT,
};

static __inline__ struct qtypeinfo *qtypeinfo_lookup(int qtype)
static __inline__ struct qtypeinfo *qtypeinfo_lookup(size_t qtype)
{
if (qtype >= ARRAY_SIZE(qtypeinfo_table))
return &qtypeinfo_unknown;
Expand Down Expand Up @@ -387,7 +387,7 @@ static int ni_policy(struct packetcontext *p)
/* ---------- */
void init_core(int forced)
{
int i;
size_t i;

DEBUG(LOG_DEBUG, "%s()\n", __func__);

Expand Down
2 changes: 1 addition & 1 deletion ninfod/ninfod_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static int encode_dnsname(const char *name,
int fqdn)
{
size_t namelen;
int i;
size_t i;

namelen = strlen(name);
if (namelen == 0)
Expand Down
8 changes: 4 additions & 4 deletions ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ int ping4_run(int argc, char **argv, struct addrinfo *ai, socket_st *sock)
exit(2);
}

if (datalen >= sizeof(struct timeval)) /* can we time transfer */
if (datalen >= (int) sizeof(struct timeval)) /* can we time transfer */
timing = 1;
packlen = datalen + MAXIPLEN + MAXICMPLEN;
if (!(packet = (unsigned char *)malloc((unsigned int)packlen))) {
Expand All @@ -886,7 +886,7 @@ int ping4_run(int argc, char **argv, struct addrinfo *ai, socket_st *sock)

int ping4_receive_error_msg(socket_st *sock)
{
int res;
ssize_t res;
char cbuf[512];
struct iovec iov;
struct msghdr msg;
Expand Down Expand Up @@ -936,7 +936,7 @@ int ping4_receive_error_msg(socket_st *sock)
} else if (e->ee_origin == SO_EE_ORIGIN_ICMP) {
struct sockaddr_in *sin = (struct sockaddr_in*)(e+1);

if (res < sizeof(icmph) ||
if (res < (ssize_t) sizeof(icmph) ||
target.sin_addr.s_addr != whereto.sin_addr.s_addr ||
icmph.type != ICMP_ECHO ||
!is_ours(sock, icmph.un.echo.id)) {
Expand Down Expand Up @@ -1110,7 +1110,7 @@ ping4_parse_reply(struct socket_st *sock, struct msghdr *msg, int cc, void *addr
struct iphdr * iph = (struct iphdr *)(&icp[1]);
struct icmphdr *icp1 = (struct icmphdr*)((unsigned char *)iph + iph->ihl*4);
int error_pkt;
if (cc < 8+sizeof(struct iphdr)+8 ||
if (cc < (int) (8 + sizeof(struct iphdr) + 8) ||
cc < 8+iph->ihl*4+8)
return 1;
if (icp1->type != ICMP_ECHO ||
Expand Down
18 changes: 9 additions & 9 deletions ping6_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static struct sockaddr_in6 whereto;
static struct sockaddr_in6 firsthop;

static unsigned char cmsgbuf[4096];
static int cmsglen = 0;
static size_t cmsglen = 0;

static int pr_icmph(__u8 type, __u8 code, __u32 info);
void ping6_usage(unsigned) __attribute((noreturn));
Expand Down Expand Up @@ -444,7 +444,7 @@ static int niquery_option_subject_name_handler(int index, const char *name)
static char nigroup_buf[INET6_ADDRSTRLEN + 1 + IFNAMSIZ];
unsigned char *dnptrs[2], **dpp, **lastdnptr;
int n;
int i;
size_t i;
char *p;
char *canonname = NULL, *idn = NULL;
unsigned char *buf = NULL;
Expand Down Expand Up @@ -517,7 +517,7 @@ static int niquery_option_subject_name_handler(int index, const char *name)
if (n < 0) {
fprintf(stderr, "ping6: Inappropriate subject name: %s\n", canonname);
goto errexit;
} else if (n >= buflen) {
} else if ((size_t) n >= buflen) {
fprintf(stderr, "ping6: dn_comp() returned too long result.\n");
goto errexit;
}
Expand Down Expand Up @@ -617,7 +617,7 @@ int ping6_run(int argc, char **argv, struct addrinfo *ai, struct socket_st *sock
struct in6_addr *addr;

if (srcrt == NULL) {
int space;
size_t space;

fprintf(stderr, "ping6: Warning: "
"Source routing is deprecated by RFC5095.\n");
Expand Down Expand Up @@ -868,7 +868,7 @@ int ping6_run(int argc, char **argv, struct addrinfo *ai, struct socket_st *sock
exit(2);
}

if (datalen >= sizeof(struct timeval) && (ni_query < 0)) {
if ((ssize_t) datalen >= (ssize_t) sizeof(struct timeval) && (ni_query < 0)) {
/* can we time transfer */
timing = 1;
}
Expand Down Expand Up @@ -1034,7 +1034,7 @@ int ping6_run(int argc, char **argv, struct addrinfo *ai, struct socket_st *sock

int ping6_receive_error_msg(socket_st *sock)
{
int res;
ssize_t res;
char cbuf[512];
struct iovec iov;
struct msghdr msg;
Expand Down Expand Up @@ -1084,7 +1084,7 @@ int ping6_receive_error_msg(socket_st *sock)
} else if (e->ee_origin == SO_EE_ORIGIN_ICMP6) {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)(e+1);

if (res < sizeof(icmph) ||
if ((size_t) res < sizeof(icmph) ||
memcmp(&target.sin6_addr, &whereto.sin6_addr, 16) ||
icmph.icmp6_type != ICMP6_ECHO_REQUEST ||
!is_ours(sock, icmph.icmp6_id)) {
Expand Down Expand Up @@ -1234,7 +1234,7 @@ void pr_niquery_reply_name(struct ni_hdr *nih, int len)
}
while (p < end) {
int fqdn = 1;
int i;
size_t i;

memset(buf, 0xff, sizeof(buf));

Expand Down Expand Up @@ -1420,7 +1420,7 @@ ping6_parse_reply(socket_st *sock, struct msghdr *msg, int cc, void *addr, struc
* using RECVRERR. :-)
*/

if (cc < 8+sizeof(struct ip6_hdr)+8)
if (cc < (int) (8 + sizeof(struct ip6_hdr) + 8))
return 1;

if (memcmp(&iph1->ip6_dst, &whereto.sin6_addr, 16))
Expand Down
9 changes: 5 additions & 4 deletions ping_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ void drop_capabilities(void)
*/
void fill(char *patp, void *packet, unsigned packet_size)
{
int ii, jj, kk;
int ii, jj;
int pat[16];
char *cp;
unsigned char *bp = packet+8;
Expand All @@ -256,6 +256,7 @@ void fill(char *patp, void *packet, unsigned packet_size)
&pat[13], &pat[14], &pat[15]);

if (ii > 0) {
unsigned kk;
for (kk = 0; kk <= packet_size - (8 + ii); kk += ii)
for (jj = 0; jj < ii; ++jj)
bp[jj + kk] = pat[jj];
Expand Down Expand Up @@ -295,12 +296,12 @@ int __schedule_exit(int next)

if (nreceived) {
waittime = 2 * tmax;
if (waittime < 1000*interval)
if (waittime < (unsigned long) (1000*interval))
waittime = 1000*interval;
} else
waittime = lingertime*1000;

if (next < 0 || next < waittime/1000)
if (next < 0 || (unsigned long)next < waittime/1000)
next = waittime/1000;

it.it_interval.tv_sec = 0;
Expand Down Expand Up @@ -782,7 +783,7 @@ int gather_statistics(__u8 *icmph, int icmplen,
if (!csfailed)
acknowledge(seq);

if (timing && cc >= 8+sizeof(struct timeval)) {
if (timing && cc >= (int) (8+sizeof(struct timeval))) {
struct timeval tmp_tv;
memcpy(&tmp_tv, ptr, sizeof(tmp_tv));

Expand Down
10 changes: 5 additions & 5 deletions rarpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ void serve_it(int fd)
struct arphdr *a = (struct arphdr*)buf;
struct rarp_map *rmap;
unsigned char *ptr;
int n;
ssize_t n;

n = recvfrom(fd, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr*)&sll, &sll_len);
if (n<0) {
Expand All @@ -438,8 +438,8 @@ void serve_it(int fd)
if (ifidx && sll.sll_ifindex != ifidx)
return;

if (n<sizeof(*a)) {
syslog(LOG_ERR, "truncated arp packet; len=%d", n);
if ((size_t)n<sizeof(*a)) {
syslog(LOG_ERR, "truncated arp packet; len=%zu", n);
return;
}

Expand Down Expand Up @@ -493,8 +493,8 @@ void serve_it(int fd)
return;
}
/* 4. Check packet length */
if (sizeof(*a) + 2*4 + 2*a->ar_hln > n) {
syslog(LOG_ERR, "truncated rarp request; len=%d", n);
if (sizeof(*a) + 2*4 + 2*a->ar_hln > (size_t) n) {
syslog(LOG_ERR, "truncated rarp request; len=%zu", n);
return;
}
/* 5. Silly check: if this guy set different source
Expand Down
10 changes: 5 additions & 5 deletions rdisc.c
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ pr_pack(char *buf, int cc, struct sockaddr_in *from)
rap->icmp_wpa);
return;
}
if ((unsigned)cc <
if (cc <
8 + rap->icmp_num_addrs * rap->icmp_wpa * 4) {
if (verbose)
logtrace("ICMP %s from %s: Too short %d, %d\n",
Expand Down Expand Up @@ -1363,7 +1363,7 @@ age_table(int time)
if (recalculate_max) {
int max_pref = max_preference();

if (max_pref != INELIGIBLE_PREF) {
if (max_pref != (int) INELIGIBLE_PREF) {
tp = table;
while (tp) {
if (tp->preference == max_pref && !tp->in_kernel) {
Expand Down Expand Up @@ -1433,18 +1433,18 @@ record_router(struct in_addr router, int pref, int ttl)
}
if (!tp->in_kernel &&
(!best_preference || tp->preference == max_preference()) &&
tp->preference != INELIGIBLE_PREF) {
tp->preference != (int) INELIGIBLE_PREF) {
add_route(tp->router);
tp->in_kernel++;
}
if (tp->preference == INELIGIBLE_PREF && tp->in_kernel) {
if (tp->preference == (int) INELIGIBLE_PREF && tp->in_kernel) {
del_route(tp->router);
tp->in_kernel = 0;
}
if (best_preference && changed_down) {
/* Check if we should add routes */
int new_max = max_preference();
if (new_max != INELIGIBLE_PREF) {
if (new_max != (int) INELIGIBLE_PREF) {
tp = table;
while (tp) {
if (tp->preference == new_max &&
Expand Down
Loading

0 comments on commit d16648c

Please sign in to comment.