All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Michael Kerrisk (man-pages)" <mtk.manpages@gmail.com>
To: "Antonin Décimo" <antonin.decimo@gmail.com>
Cc: mtk.manpages@gmail.com, linux-man@vger.kernel.org
Subject: Re: [PATCH] netlink.7: fix alignment issue in example
Date: Mon, 30 Dec 2019 19:49:58 +0100	[thread overview]
Message-ID: <eb5aefe0-ab56-d37b-96c5-cec03e373007@gmail.com> (raw)
In-Reply-To: <CAC=54BKOz_ReXURKh8HCzA67SN=vO9439G7K8J0P19dn-PX_mg@mail.gmail.com>

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/

      reply	other threads:[~2019-12-30 18:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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) [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=eb5aefe0-ab56-d37b-96c5-cec03e373007@gmail.com \
    --to=mtk.manpages@gmail.com \
    --cc=antonin.decimo@gmail.com \
    --cc=linux-man@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.