iwd.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] monitor: use int64_t type for NLMSG_NEXT length
@ 2022-10-13 20:02 James Prestwood
  2022-10-14 14:49 ` Denis Kenzior
  0 siblings, 1 reply; 2+ messages in thread
From: James Prestwood @ 2022-10-13 20:02 UTC (permalink / raw)
  To: iwd; +Cc: James Prestwood

Commit c7640f8346 was meant to fix a sign compare warning
in clang because NLMSG_NEXT internally compares the length
with nlmsghdr->nlmsg_len which is a u32. The problem is the
NLMSG_NEXT can underflow an unsigned value, hence why it
expects an int type to be passed in.

To work around this we can instead pass a larger sized
int64_t which the compiler allows since it can upgrade the
unsigned nlmsghdr->nlmsg_len. There is no underflow risk
with an int64_t either because the buffer used is much
smaller than what can fit in an int64_t.

Fixes: c7640f8346 ("monitor: fix integer comparison error (clang)")
---
 monitor/main.c  | 2 +-
 monitor/nlmon.c | 6 +++---
 monitor/nlmon.h | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)

v2:
 * Use int64 instead of int

diff --git a/monitor/main.c b/monitor/main.c
index 9117fba3..f0b16bbf 100644
--- a/monitor/main.c
+++ b/monitor/main.c
@@ -451,7 +451,7 @@ static int analyze_pcap(const char *pathname)
 
 	while (pcap_read(pcap, &tv, buf, snaplen, &len, &real_len)) {
 		struct nlmsghdr *nlmsg;
-		uint32_t aligned_len;
+		int64_t aligned_len;
 		uint16_t arphrd_type;
 		uint16_t proto_type;
 
diff --git a/monitor/nlmon.c b/monitor/nlmon.c
index 2222db09..69e86ce3 100644
--- a/monitor/nlmon.c
+++ b/monitor/nlmon.c
@@ -8080,7 +8080,7 @@ static void print_rtnl_msg(const struct timeval *tv,
 void nlmon_print_rtnl(struct nlmon *nlmon, const struct timeval *tv,
 					const void *data, uint32_t size)
 {
-	uint32_t aligned_size = NLMSG_ALIGN(size);
+	int64_t aligned_size = NLMSG_ALIGN(size);
 	const struct nlmsghdr *nlmsg;
 
 	update_time_offset(tv);
@@ -8112,7 +8112,7 @@ void nlmon_print_rtnl(struct nlmon *nlmon, const struct timeval *tv,
 }
 
 void nlmon_print_genl(struct nlmon *nlmon, const struct timeval *tv,
-					const void *data, uint32_t size)
+					const void *data, int64_t size)
 {
 	const struct nlmsghdr *nlmsg;
 
@@ -8144,7 +8144,7 @@ static bool nlmon_receive(struct l_io *io, void *user_data)
 	unsigned char buf[8192];
 	unsigned char control[32];
 	ssize_t bytes_read;
-	uint32_t nlmsg_len;
+	int64_t nlmsg_len;
 	int fd;
 
 	fd = l_io_get_fd(io);
diff --git a/monitor/nlmon.h b/monitor/nlmon.h
index f2f2892a..ab038fdf 100644
--- a/monitor/nlmon.h
+++ b/monitor/nlmon.h
@@ -41,7 +41,7 @@ void nlmon_destroy(struct nlmon *nlmon);
 void nlmon_print_rtnl(struct nlmon *nlmon, const struct timeval *tv,
 					const void *data, uint32_t size);
 void nlmon_print_genl(struct nlmon *nlmon, const struct timeval *tv,
-					const void *data, uint32_t size);
+					const void *data, int64_t size);
 void nlmon_print_pae(struct nlmon *nlmon, const struct timeval *tv,
 					uint8_t type, int index,
 					const void *data, uint32_t size);
-- 
2.34.3


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] monitor: use int64_t type for NLMSG_NEXT length
  2022-10-13 20:02 [PATCH v2] monitor: use int64_t type for NLMSG_NEXT length James Prestwood
@ 2022-10-14 14:49 ` Denis Kenzior
  0 siblings, 0 replies; 2+ messages in thread
From: Denis Kenzior @ 2022-10-14 14:49 UTC (permalink / raw)
  To: James Prestwood, iwd

Hi James,

On 10/13/22 15:02, James Prestwood wrote:
> Commit c7640f8346 was meant to fix a sign compare warning
> in clang because NLMSG_NEXT internally compares the length
> with nlmsghdr->nlmsg_len which is a u32. The problem is the
> NLMSG_NEXT can underflow an unsigned value, hence why it
> expects an int type to be passed in.
> 
> To work around this we can instead pass a larger sized
> int64_t which the compiler allows since it can upgrade the
> unsigned nlmsghdr->nlmsg_len. There is no underflow risk
> with an int64_t either because the buffer used is much
> smaller than what can fit in an int64_t.
> 
> Fixes: c7640f8346 ("monitor: fix integer comparison error (clang)")
> ---
>   monitor/main.c  | 2 +-
>   monitor/nlmon.c | 6 +++---
>   monitor/nlmon.h | 2 +-
>   3 files changed, 5 insertions(+), 5 deletions(-)
> 
> v2:
>   * Use int64 instead of int
> 

Applied, thanks.

Regards,
-Denis


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-10-14 14:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-13 20:02 [PATCH v2] monitor: use int64_t type for NLMSG_NEXT length James Prestwood
2022-10-14 14:49 ` Denis Kenzior

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).