Linux-man Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] netlink.7: fix alignment issue in example
@ 2019-12-27 21:15 Antonin Décimo
  2019-12-30 18:49 ` Michael Kerrisk (man-pages)
  0 siblings, 1 reply; 2+ messages in thread
From: Antonin Décimo @ 2019-12-27 21:15 UTC (permalink / raw)
  To: mtk.manpages; +Cc: linux-man

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

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

* Re: [PATCH] netlink.7: fix alignment issue in example
  2019-12-27 21:15 [PATCH] netlink.7: fix alignment issue in example Antonin Décimo
@ 2019-12-30 18:49 ` Michael Kerrisk (man-pages)
  0 siblings, 0 replies; 2+ messages in thread
From: Michael Kerrisk (man-pages) @ 2019-12-30 18:49 UTC (permalink / raw)
  To: Antonin Décimo; +Cc: mtk.manpages, linux-man

Hello Antonin,

On 12/27/19 10:15 PM, Antonin Décimo wrote:
> 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

Thanks. Patch applied.

Cheers,

Michael

> ---
>  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;
> 


-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/

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

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-27 21:15 [PATCH] netlink.7: fix alignment issue in example Antonin Décimo
2019-12-30 18:49 ` Michael Kerrisk (man-pages)

Linux-man Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-man/0 linux-man/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-man linux-man/ https://lore.kernel.org/linux-man \
		linux-man@vger.kernel.org
	public-inbox-index linux-man

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-man


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git