[PATCH] netlink.7: fix alignment issue in example

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



PVS-Studio reports that in

    char buf[8192];
    /* ... */
    nh = (struct nlmsghdr *) buf,

the pointer 'buf' is cast to a more strictly aligned pointer
type. This is undefined behaviour. One possible solution to make sure
that buf is correctly aligned is to declare buf as an array of struct
nlmsghdr. Other solutions include allocating the array on the heap,
use an union, or stdalign features.
With this patch, the buffer still contains 8192 bytes.

This was raised on Stack Overflow:
https://stackoverflow.com/questions/57745580/netlink-receive-buffer-alignment
---
 man7/netlink.7 | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/man7/netlink.7 b/man7/netlink.7
index 81d4249..853dee6 100644
--- a/man7/netlink.7
+++ b/man7/netlink.7
@@ -533,8 +533,9 @@ And the last example is about reading netlink message.
 .in +4n
 .EX
 int len;
-char buf[8192];     /* 8192 to avoid message truncation on
-                       platforms with page size > 4096 */
+/* 8192 to avoid message truncation on platforms with
+   page size > 4096 */
+struct nlmsghdr buf[8192/sizeof(struct nlmsghdr)];
 struct iovec iov = { buf, sizeof(buf) };
 struct sockaddr_nl sa;
 struct msghdr msg;
-- 
2.24.1



[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux