Skip to content

Commit

Permalink
all: fix and enable "-Wsign-compare" warning
Browse files Browse the repository at this point in the history
  • Loading branch information
thom311 committed May 29, 2024
1 parent 9451842 commit 5248e1a
Show file tree
Hide file tree
Showing 35 changed files with 103 additions and 74 deletions.
1 change: 0 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ warn_cppflags = \
-Wpointer-arith \
-Wvla \
\
-Wno-sign-compare \
-Wno-unused-parameter \
$(NULL)

Expand Down
4 changes: 2 additions & 2 deletions lib/addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1042,8 +1042,8 @@ char *nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)

prefix:
if (addr->a_family != AF_MPLS &&
addr->a_prefixlen != (8 * addr->a_len)) {
snprintf(tmp, sizeof(tmp), "/%u", addr->a_prefixlen);
(unsigned)addr->a_prefixlen != (8u * ((size_t)addr->a_len))) {
snprintf(tmp, sizeof(tmp), "/%d", addr->a_prefixlen);
strncat(buf, tmp, size - strlen(buf) - 1);
}

Expand Down
10 changes: 5 additions & 5 deletions lib/attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ static int validate_nla(const struct nlattr *nla, int maxtype,
else if (pt->type != NLA_UNSPEC)
minlen = nla_attr_minlen[pt->type];

if (nla_len(nla) < minlen)
if (_nla_len(nla) < minlen)
return -NLE_RANGE;

if (pt->maxlen && nla_len(nla) > pt->maxlen)
Expand Down Expand Up @@ -459,14 +459,14 @@ int nla_strcmp(const struct nlattr *nla, const char *str)
struct nlattr *nla_reserve(struct nl_msg *msg, int attrtype, int attrlen)
{
struct nlattr *nla;
int tlen;
size_t tlen;

if (attrlen < 0)
return NULL;

tlen = NLMSG_ALIGN(msg->nm_nlh->nlmsg_len) + nla_total_size(attrlen);

if (tlen > msg->nm_size)
if (tlen > msg->nm_size || tlen > UINT32_MAX)
return NULL;

nla = (struct nlattr *) nlmsg_tail(msg->nm_nlh);
Expand Down Expand Up @@ -738,7 +738,7 @@ int64_t nla_get_s64(const struct nlattr *nla)
{
int64_t tmp = 0;

if (nla && nla_len(nla) >= sizeof(tmp))
if (nla && _nla_len(nla) >= sizeof(tmp))
memcpy(&tmp, nla_data(nla), sizeof(tmp));

return tmp;
Expand Down Expand Up @@ -768,7 +768,7 @@ uint64_t nla_get_u64(const struct nlattr *nla)
{
uint64_t tmp = 0;

if (nla && nla_len(nla) >= sizeof(tmp))
if (nla && _nla_len(nla) >= sizeof(tmp))
memcpy(&tmp, nla_data(nla), sizeof(tmp));

return tmp;
Expand Down
2 changes: 1 addition & 1 deletion lib/fib_lookup/lookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ int flnl_lookup_build_request(struct flnl_request *req, int flags,
fr.fl_fwmark = fwmark != UINT_LEAST64_MAX ? fwmark : 0;
fr.fl_tos = tos >= 0 ? tos : 0;
fr.fl_scope = scope >= 0 ? scope : RT_SCOPE_UNIVERSE;
fr.tb_id_in = table >= 0 ? table : RT_TABLE_UNSPEC;
fr.tb_id_in = table >= 0 ? (unsigned)table : (unsigned)RT_TABLE_UNSPEC;

addr = flnl_request_get_addr(req);
if (!addr)
Expand Down
4 changes: 3 additions & 1 deletion lib/genl/genl.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,14 @@ int genl_send_simple(struct nl_sock *sk, int family, int cmd,
int genlmsg_valid_hdr(struct nlmsghdr *nlh, int hdrlen)
{
struct genlmsghdr *ghdr;
int l;

if (!nlmsg_valid_hdr(nlh, GENL_HDRLEN))
return 0;

ghdr = nlmsg_data(nlh);
if (genlmsg_len(ghdr) < NLMSG_ALIGN(hdrlen))
l = genlmsg_len(ghdr);
if (l < 0 || ((unsigned)l) < NLMSG_ALIGN(hdrlen))
return 0;

return 1;
Expand Down
2 changes: 1 addition & 1 deletion lib/genl/mngt.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ int genl_register(struct nl_cache_ops *ops)
goto errout;
}

if (ops->co_hdrsize < GENL_HDRSIZE(0)) {
if (ops->co_hdrsize < (int)GENL_HDRSIZE(0)) {
err = -NLE_INVAL;
goto errout;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/mpls.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static const char *mpls_ntop1(const struct mpls_label *addr,
uint32_t label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
int len = snprintf(dest, destlen, "%u", label);

if (len >= destlen)
if (len < 0 || (unsigned)len >= destlen)
break;

/* Is this the end? */
Expand Down
22 changes: 15 additions & 7 deletions lib/msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "nl-priv-dynamic-core/cache-api.h"
#include "nl-aux-core/nl-core.h"

static size_t default_msg_size;
static size_t default_msg_size; /* GLOBAL! */

static void _nl_init init_msg_size(void)
{
Expand Down Expand Up @@ -168,7 +168,10 @@ int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)

int nlmsg_valid_hdr(const struct nlmsghdr *nlh, int hdrlen)
{
if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
int s;

s = nlmsg_msg_size(hdrlen);
if (s < 0 || nlh->nlmsg_len < ((unsigned)s))
return 0;

return 1;
Expand All @@ -183,7 +186,7 @@ int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
{
return (remaining >= (int)sizeof(struct nlmsghdr) &&
nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
nlh->nlmsg_len <= remaining);
nlh->nlmsg_len <= ((unsigned)remaining));
}

/**
Expand Down Expand Up @@ -369,8 +372,11 @@ struct nl_msg *nlmsg_alloc_simple(int nlmsgtype, int flags)
*/
void nlmsg_set_default_size(size_t max)
{
if (max < nlmsg_total_size(0))
max = nlmsg_total_size(0);
size_t s;

s = nlmsg_total_size(0);
if (max < s)
max = s;

default_msg_size = max;
}
Expand Down Expand Up @@ -841,7 +847,7 @@ static void *print_genl_msg(struct nl_msg *msg, FILE *ofd, struct nlmsghdr *hdr,
{
char *data = nlmsg_data(hdr);

if (*payloadlen < GENL_HDRLEN)
if (*payloadlen < (int)GENL_HDRLEN)
return data;

print_genl_hdr(ofd, data);
Expand Down Expand Up @@ -917,10 +923,12 @@ static void dump_error_msg(struct nl_msg *msg, FILE *ofd)
{
struct nlmsghdr *hdr = nlmsg_hdr(msg);
struct nlmsgerr *err = nlmsg_data(hdr);
int l;

fprintf(ofd, " [ERRORMSG] %zu octets\n", sizeof(*err));

if (nlmsg_len(hdr) >= sizeof(*err)) {
l = nlmsg_len(hdr);
if (l >= 0 && ((unsigned)l) >= sizeof(*err)) {
struct nl_msg *errmsg;

fprintf(ofd, " .error = %d \"%s\"\n", err->error,
Expand Down
4 changes: 3 additions & 1 deletion lib/netfilter/log_msg_obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,9 @@ uint32_t nfnl_log_msg_get_physoutdev(const struct nfnl_log_msg *msg)

void nfnl_log_msg_set_hwaddr(struct nfnl_log_msg *msg, uint8_t *hwaddr, int len)
{
if (len > sizeof(msg->log_msg_hwaddr))
if (len < 0)
len = 0;
else if (((unsigned)len) > sizeof(msg->log_msg_hwaddr))
len = sizeof(msg->log_msg_hwaddr);
msg->log_msg_hwaddr_len = len;
memcpy(msg->log_msg_hwaddr, hwaddr, len);
Expand Down
5 changes: 3 additions & 2 deletions lib/netfilter/queue_msg_obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,10 @@ uint32_t nfnl_queue_msg_get_physoutdev(const struct nfnl_queue_msg *msg)
void nfnl_queue_msg_set_hwaddr(struct nfnl_queue_msg *msg, uint8_t *hwaddr,
int len)
{
if (len > sizeof(msg->queue_msg_hwaddr))
if (len < 0)
len = 0;
else if (((unsigned)len) > sizeof(msg->queue_msg_hwaddr))
len = sizeof(msg->queue_msg_hwaddr);

msg->queue_msg_hwaddr_len = len;
memcpy(msg->queue_msg_hwaddr, hwaddr, len);
msg->ce_mask |= QUEUE_MSG_ATTR_HWADDR;
Expand Down
9 changes: 5 additions & 4 deletions lib/nl.c
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla,
{
ssize_t n;
int flags = 0;
static int page_size = 0;
static int page_size = 0; /* GLOBAL! */
struct iovec iov;
struct msghdr msg = {
.msg_name = (void *) nla,
Expand All @@ -680,7 +680,7 @@ int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla,
if (page_size == 0)
page_size = getpagesize() * 4;

iov.iov_len = sk->s_bufsize ? sk->s_bufsize : page_size;
iov.iov_len = sk->s_bufsize ? sk->s_bufsize : ((size_t)page_size);
iov.iov_base = malloc(iov.iov_len);

if (!iov.iov_base) {
Expand Down Expand Up @@ -734,7 +734,7 @@ int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla,
goto retry;
}

if (iov.iov_len < n || (msg.msg_flags & MSG_TRUNC)) {
if (iov.iov_len < ((size_t)n) || (msg.msg_flags & MSG_TRUNC)) {
void *tmp;

/* respond with error to an incomplete message */
Expand Down Expand Up @@ -964,7 +964,8 @@ static int recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
else if (hdr->nlmsg_type == NLMSG_ERROR) {
struct nlmsgerr *e = nlmsg_data(hdr);

if (hdr->nlmsg_len < nlmsg_size(sizeof(*e))) {
if (hdr->nlmsg_len <
((unsigned)nlmsg_size(sizeof(*e)))) {
/* Truncated error message, the default action
* is to stop parsing. The user may overrule
* this action by returning NL_SKIP or
Expand Down
2 changes: 1 addition & 1 deletion lib/route/addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ struct rtnl_addr *rtnl_addr_get(struct nl_cache *cache, int ifindex,
return NULL;

nl_list_for_each_entry(a, &cache->c_items, ce_list) {
if (ifindex && a->a_ifindex != ifindex)
if (ifindex != 0 && a->a_ifindex != ((unsigned)ifindex))
continue;

if (a->ce_mask & ADDR_ATTR_LOCAL &&
Expand Down
6 changes: 4 additions & 2 deletions lib/route/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ struct rtnl_class *rtnl_class_get(struct nl_cache *cache, int ifindex,
return NULL;

nl_list_for_each_entry(class, &cache->c_items, ce_list) {
if (class->c_handle == handle && class->c_ifindex == ifindex) {
if (class->c_handle == handle &&
class->c_ifindex == ((unsigned)ifindex)) {
nl_object_get((struct nl_object *) class);
return class;
}
Expand Down Expand Up @@ -390,7 +391,8 @@ struct rtnl_class *rtnl_class_get_by_parent(struct nl_cache *cache, int ifindex,
return NULL;

nl_list_for_each_entry(class, &cache->c_items, ce_list) {
if (class->c_parent == parent && class->c_ifindex == ifindex) {
if (class->c_parent == parent &&
class->c_ifindex == ((unsigned)ifindex)) {
nl_object_get((struct nl_object *) class);
return class;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/route/cls.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ struct rtnl_cls *rtnl_cls_find_by_handle(struct nl_cache *cache, int ifindex, ui

nl_list_for_each_entry(cls, &cache->c_items, ce_list) {
if ((cls->c_parent == parent) &&
(cls->c_ifindex == ifindex)&&
cls->c_ifindex == ((unsigned)ifindex) &&
(cls->c_handle == handle)) {
nl_object_get((struct nl_object *) cls);
return cls;
Expand Down Expand Up @@ -429,9 +429,9 @@ struct rtnl_cls *rtnl_cls_find_by_prio(struct nl_cache *cache, int ifindex,

nl_list_for_each_entry(cls, &cache->c_items, ce_list) {
if ((cls->c_parent == parent) &&
(cls->c_ifindex == ifindex) &&
cls->c_ifindex == ((unsigned)ifindex) &&
(cls->c_prio == prio)) {
nl_object_get((struct nl_object *) cls);
nl_object_get((struct nl_object *)cls);
return cls;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/route/cls/ematch.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ int rtnl_ematch_parse_attr(struct nlattr *attr, struct rtnl_ematch_tree **result
NL_DBG(3, "parsing ematch attribute %d, len=%u\n",
nmatches+1, nla_len(a));

if (nla_len(a) < sizeof(*hdr)) {
if (_nla_len(a) < sizeof(*hdr)) {
err = -NLE_INVAL;
goto errout;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/route/cls/u32.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static int u32_msg_parser(struct rtnl_tc *tc, void *data)
sel = u->cu_selector->d_data;
pcnt_size = sizeof(struct tc_u32_pcnt) +
(sel->nkeys * sizeof(uint64_t));
if (nla_len(tb[TCA_U32_PCNT]) < pcnt_size) {
if (_nla_len(tb[TCA_U32_PCNT]) < pcnt_size) {
err = -NLE_INVAL;
goto errout;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/route/link.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ int rtnl_link_info_parse(struct rtnl_link *link, struct nlattr **tb)
/* beware: @st might not be the full struct, only fields up to
* tx_compressed are present. See _nl_offsetofend() above. */

if (nla_len(tb[IFLA_STATS]) >= _nl_offsetofend (struct rtnl_link_stats, rx_nohandler))
if (_nla_len(tb[IFLA_STATS]) >= _nl_offsetofend (struct rtnl_link_stats, rx_nohandler))
link->l_stats[RTNL_LINK_RX_NOHANDLER] = st->rx_nohandler;
else
link->l_stats[RTNL_LINK_RX_NOHANDLER] = 0;
Expand Down Expand Up @@ -1320,7 +1320,7 @@ struct rtnl_link *rtnl_link_get(struct nl_cache *cache, int ifindex)
return NULL;

nl_list_for_each_entry(link, &cache->c_items, ce_list) {
if (link->l_index == ifindex) {
if (link->l_index == ((unsigned)ifindex)) {
nl_object_get((struct nl_object *) link);
return link;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/route/link/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static int can_parse(struct rtnl_link *link, struct nlattr *data,
ci->ci_mask |= CAN_HAS_DATA_BITTIMING_CONST;
}

if (xstats && nla_len(xstats) >= sizeof(ci->ci_device_stats)) {
if (xstats && _nla_len(xstats) >= sizeof(ci->ci_device_stats)) {
nla_memcpy(&ci->ci_device_stats, xstats, sizeof(ci->ci_device_stats));
ci->ci_mask |= CAN_HAS_DEVICE_STATS;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/route/link/macsec.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ static int macsec_compare(struct rtnl_link *link_a, struct rtnl_link *link_b,
struct macsec_info *a = link_a->l_info;
struct macsec_info *b = link_b->l_info;
int diff = 0;
uint32_t attrs = flags & LOOSE_COMPARISON ? b->ce_mask : ~0;
uint32_t attrs = flags & LOOSE_COMPARISON ? b->ce_mask :
~((uint32_t)0u);

#define _DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ATTR, a, b, EXPR)
if (a->ce_mask & MACSEC_ATTR_SCI && b->ce_mask & MACSEC_ATTR_SCI)
Expand Down
7 changes: 5 additions & 2 deletions lib/route/link/macvlan.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ static int macvlan_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
{
struct macvlan_info *mvi = link->l_info;
struct nlattr *data, *datamac = NULL;
int i, ret;
int ret;

if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
return -NLE_MSGSIZE;
Expand All @@ -239,6 +239,8 @@ static int macvlan_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
NLA_PUT_U16(msg, IFLA_MACVLAN_FLAGS, mvi->mvi_flags);

if (mvi->mvi_mask & MACVLAN_HAS_MACADDR) {
uint32_t i;

NLA_PUT_U32(msg, IFLA_MACVLAN_MACADDR_MODE, mvi->mvi_macmode);
datamac = nla_nest_start(msg, IFLA_MACVLAN_MACADDR_DATA);
if (!datamac)
Expand Down Expand Up @@ -345,14 +347,15 @@ int rtnl_link_is_macvlan(struct rtnl_link *link)
int rtnl_link_macvlan_set_mode(struct rtnl_link *link, uint32_t mode)
{
struct macvlan_info *mvi = link->l_info;
int i;

IS_MACVLAN_LINK_ASSERT(link);

mvi->mvi_mode = mode;
mvi->mvi_mask |= MACVLAN_HAS_MODE;

if (mode != MACVLAN_MODE_SOURCE) {
uint32_t i;

for (i = 0; i < mvi->mvi_maccount; i++)
nl_addr_put(mvi->mvi_macaddr[i]);
free(mvi->mvi_macaddr);
Expand Down
Loading

0 comments on commit 5248e1a

Please sign in to comment.