[PATCH netifd 2/5] netifd: Fix multiple -Wsign-compare warnings

Hauke Mehrtens hauke at hauke-m.de
Sun Feb 19 07:20:24 PST 2023


This fixes warnings like this:
warning: comparison of integer expressions of different signedness: 'int' and 'long unsigned int' [-Wsign-compare]

Mostly this was an int compared to a size_t returned by ARRAY_SIZE().
The easiest fix is to count on the size_t type.

The ifindex is sometimes an unsigned int and sometimes a signed int in
the kernel interfaces. I think it normally fits into an unsigned 16 bit
value, so this should be fine. Do the one comparison where the
compiler complains as a long.

Casting the result of sizeof() to int should be safe. These values are
never out of range of int.

Signed-off-by: Hauke Mehrtens <hauke at hauke-m.de>
---
 bonding.c      |  2 +-
 handler.c      |  5 +++--
 interface-ip.c |  2 +-
 main.c         |  4 ++--
 system-linux.c | 21 ++++++++++++---------
 ubus.c         |  4 ++--
 vlan.c         |  4 ++--
 wireless.c     |  2 +-
 8 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/bonding.c b/bonding.c
index 457fe51..f4005de 100644
--- a/bonding.c
+++ b/bonding.c
@@ -396,7 +396,7 @@ bonding_apply_settings(struct bonding_device *bdev, struct blob_attr **tb)
 
 	if ((cur = tb[BOND_ATTR_POLICY]) != NULL) {
 		const char *policy = blobmsg_get_string(cur);
-		int i;
+		size_t i;
 
 		for (i = 0; i < ARRAY_SIZE(bonding_policy_str); i++) {
 			if (strcmp(policy, bonding_policy_str[i]) != 0)
diff --git a/handler.c b/handler.c
index 04bdbee..78fc9a0 100644
--- a/handler.c
+++ b/handler.c
@@ -229,7 +229,8 @@ netifd_parse_extdev_handler(const char *path_to_file, create_extdev_handler_cb c
 void netifd_init_script_handlers(int dir_fd, script_dump_cb cb)
 {
 	glob_t g;
-	int i, prev_fd;
+	int prev_fd;
+	size_t i;
 
 	prev_fd = netifd_dir_push(dir_fd);
 	if (glob("./*.sh", 0, NULL, &g)) {
@@ -252,7 +253,7 @@ netifd_init_extdev_handlers(int dir_fd, create_extdev_handler_cb cb)
 
 	prev_fd = netifd_dir_push(dir_fd);
 	glob("*.json", 0, NULL, &g);
-	for (int i = 0; i < g.gl_pathc; i++)
+	for (size_t i = 0; i < g.gl_pathc; i++)
 		netifd_parse_extdev_handler(g.gl_pathv[i], cb);
 	netifd_dir_pop(prev_fd);
 }
diff --git a/interface-ip.c b/interface-ip.c
index ab4a5cf..7359db2 100644
--- a/interface-ip.c
+++ b/interface-ip.c
@@ -99,7 +99,7 @@ static struct uloop_timeout valid_until_timeout;
 static void
 clear_if_addr(union if_addr *a, int mask)
 {
-	int m_bytes = (mask + 7) / 8;
+	size_t m_bytes = (mask + 7) / 8;
 	uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
 	uint8_t *p = (uint8_t *) a;
 
diff --git a/main.c b/main.c
index 4c1c855..874dc8b 100644
--- a/main.c
+++ b/main.c
@@ -303,8 +303,8 @@ int main(int argc, char **argv)
 			break;
 		case 'l':
 			log_level = atoi(optarg);
-			if (log_level >= ARRAY_SIZE(log_class))
-				log_level = ARRAY_SIZE(log_class) - 1;
+			if (log_level >= (int)ARRAY_SIZE(log_class))
+				log_level = (int)ARRAY_SIZE(log_class) - 1;
 			break;
 #ifndef DUMMY_MODE
 		case 'S':
diff --git a/system-linux.c b/system-linux.c
index 45a9efb..d13a561 100644
--- a/system-linux.c
+++ b/system-linux.c
@@ -1154,7 +1154,7 @@ static bool check_ifaddr(struct nlmsghdr *hdr, int ifindex)
 {
 	struct ifaddrmsg *ifa = NLMSG_DATA(hdr);
 
-	return ifa->ifa_index == ifindex;
+	return (long)ifa->ifa_index == ifindex;
 }
 
 static bool check_route(struct nlmsghdr *hdr, int ifindex)
@@ -1438,7 +1438,8 @@ int system_macvlan_add(struct device *macvlan, struct device *dev, struct macvla
 {
 	struct nl_msg *msg;
 	struct nlattr *linkinfo, *data;
-	int i, rv;
+	size_t i;
+	int rv;
 	static const struct {
 		const char *name;
 		enum macvlan_mode val;
@@ -1700,7 +1701,7 @@ system_set_ethtool_settings(struct device *dev, struct device_settings *s)
 		.ifr_data = (caddr_t)&ecmd,
 	};
 	static const struct {
-		int speed;
+		unsigned int speed;
 		uint8_t bit_half;
 		uint8_t bit_full;
 	} speed_mask[] = {
@@ -1709,7 +1710,7 @@ system_set_ethtool_settings(struct device *dev, struct device_settings *s)
 		{ 1000, ETHTOOL_LINK_MODE_1000baseT_Half_BIT, ETHTOOL_LINK_MODE_1000baseT_Full_BIT },
 	};
 	uint32_t adv;
-	int i;
+	size_t i;
 
 	strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
 
@@ -2355,7 +2356,7 @@ static const struct {
 
 static void system_add_link_modes(struct blob_buf *b, __u32 mask)
 {
-	int i;
+	size_t i;
 	for (i = 0; i < ARRAY_SIZE(ethtool_link_modes); i++) {
 		if (mask & ethtool_link_modes[i].mask)
 			blobmsg_add_string(b, NULL, ethtool_link_modes[i].name);
@@ -2373,7 +2374,7 @@ system_if_force_external(const char *ifname)
 static const char *
 system_netdevtype_name(unsigned short dev_type)
 {
-	unsigned int i;
+	size_t i;
 
 	for (i = 0; i < ARRAY_SIZE(netdev_types); i++) {
 		if (netdev_types[i].id == dev_type)
@@ -2457,7 +2458,8 @@ ethtool_feature_index(const char *ifname, const char *keyname)
 {
 	struct ethtool_gstrings *feature_names;
 	struct ifreq ifr = { 0 };
-	int32_t n_features, i;
+	int32_t n_features;
+	uint32_t i;
 
 	n_features = ethtool_feature_count(ifname);
 
@@ -2592,7 +2594,7 @@ system_if_dump_stats(struct device *dev, struct blob_buf *b)
 		"rx_fifo_errors", "tx_carrier_errors",
 	};
 	int stats_dir;
-	int i;
+	size_t i;
 	uint64_t val = 0;
 
 	stats_dir = open(dev_sysfs_path(dev->ifname, "statistics"), O_DIRECTORY);
@@ -2838,7 +2840,8 @@ int system_del_route(struct device *dev, struct device_route *route)
 int system_flush_routes(void)
 {
 	const char *names[] = { "ipv4", "ipv6" };
-	int fd, i;
+	size_t i;
+	int fd;
 
 	for (i = 0; i < ARRAY_SIZE(names); i++) {
 		snprintf(dev_buf, sizeof(dev_buf), "%s/sys/net/%s/route/flush", proc_path, names[i]);
diff --git a/ubus.c b/ubus.c
index 7f4821d..4d05786 100644
--- a/ubus.c
+++ b/ubus.c
@@ -1132,7 +1132,7 @@ netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
 {
 	struct interface *iface;
 	struct blob_attr *tb;
-	int i;
+	size_t i;
 
 	blobmsg_parse(&iface_policy, 1, &tb, blob_data(msg), blob_len(msg));
 	if (!tb)
@@ -1158,7 +1158,7 @@ netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
 static void netifd_add_iface_object(void)
 {
 	struct ubus_method *methods;
-	int i;
+	size_t i;
 
 	methods = calloc(1, sizeof(iface_object_methods));
 	if (!methods)
diff --git a/vlan.c b/vlan.c
index 3d444a9..4d32b60 100644
--- a/vlan.c
+++ b/vlan.c
@@ -143,7 +143,7 @@ static void vlan_dev_cb(struct device_user *dep, enum device_event ev)
 		vlan_hotplug_check(vldev, dep->dev);
 		vldev->dev.hidden = dep->dev->hidden;
 		if (snprintf(name, sizeof(name), "%s.%d", dep->dev->ifname,
-			     vldev->id) >= sizeof(name) - 1 ||
+			     vldev->id) >= (int)sizeof(name) - 1 ||
 		    device_set_ifname(&vldev->dev, name))
 			free_vlan_if(&vldev->dev);
 		break;
@@ -203,7 +203,7 @@ static struct device *get_vlan_device(struct device *dev, char *id_str, bool cre
 	if (!create)
 		return NULL;
 
-	if (snprintf(name, sizeof(name), "%s.%d", dev->ifname, id) >= sizeof(name) - 1)
+	if (snprintf(name, sizeof(name), "%s.%d", dev->ifname, id) >= (int)sizeof(name) - 1)
 		return NULL;
 
 	D(DEVICE, "Create vlan device '%s'\n", name);
diff --git a/wireless.c b/wireless.c
index 705d6dd..1f54bfb 100644
--- a/wireless.c
+++ b/wireless.c
@@ -1542,7 +1542,7 @@ void wireless_device_hotplug_event(const char *name, bool add)
 	struct wireless_interface *vif;
 	struct wireless_device *wdev;
 	const char *s;
-	int len;
+	size_t len;
 
 	s = strstr(name, ".sta");
 	if (s) {
-- 
2.39.1




More information about the openwrt-devel mailing list