From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 50068C2D0C2 for ; Mon, 30 Dec 2019 18:50:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 223FE20409 for ; Mon, 30 Dec 2019 18:50:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="t9zWquYy" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727571AbfL3SuD (ORCPT ); Mon, 30 Dec 2019 13:50:03 -0500 Received: from mail-pg1-f196.google.com ([209.85.215.196]:45413 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727278AbfL3SuD (ORCPT ); Mon, 30 Dec 2019 13:50:03 -0500 Received: by mail-pg1-f196.google.com with SMTP id b9so18392104pgk.12 for ; Mon, 30 Dec 2019 10:50:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=cc:subject:to:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=4KSEyoVO99g/YB0i3Xm7t6pJQuoLKfABoKBRCidCwok=; b=t9zWquYyVs8+44O1squ9R9mN5jB9wqSGACg7xXi5N2gzwtf6xHoFLMS84tZ7R9ebMI 2f4mWz9kiROwrPiQFwJBFcKFAnwLXAc72NENqvZ6Zp//ObUGvObthk8BmPhkcucm/K28 nf/6FFR/o87WxqT2XKhDqNi8F0b9fTtEKAPpnK8v9+SWtAIiVshQlkMHnyrtyG1ZFbvv dyArjano9tmEFVXTFl4m8CAdLR3P+bL5xM6t7zXjRfc50m20yMcLVZe2KEnrkwvpLFAO OHjh8YYVR5GrgIqlxQXXtIvsNZp6NgHn3+7ZoQlvRw4SFwEQc6t6GUnUN9r4hzz9yTTq iuqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:cc:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=4KSEyoVO99g/YB0i3Xm7t6pJQuoLKfABoKBRCidCwok=; b=skS3E5hnYA2DL+YsgNopjSRe7LhZQyRTkbrGStsrCSYXfeAND9eWSfJofEnl1GhFty wbW75o9kmQffEqhkP9UkZxUtou1omytTRsO6zf0w1qsWYIiv54TcLMzdWpTjR4MWNtHM 7/8g3wJfd/dDMCYmaNM+bvDSQ504OhV2FqXCsUmk6J5eIbWONj5WuBFlvK4RAapEd1g8 3uyPxIgcqf0BZzAEt4EMBhhsXwg7pWkJMeBpuxn/roo90acczR6yHRdyvgwOPLKLcidq Zprolfc1Opq0exI5eVdmiQfqrGbtHXougn0IBgSRp+JEBygEaF92Iq1Y/MVjMvY6EMt7 tIew== X-Gm-Message-State: APjAAAVrsna71KMID5FQBXVBbTtXM8ucqiL+q5fn50o1WL7BLMfv195L uFfjweL4lrON/9NRllsG15uWJ9X2 X-Google-Smtp-Source: APXvYqx7ko4JO9c280Hv4FOD3CWzh8Wy70DWht1CiEpHZzEd1u/d44PdAypO4kZtA0JO03jIKX+9jQ== X-Received: by 2002:a63:e84d:: with SMTP id a13mr76698463pgk.274.1577731802704; Mon, 30 Dec 2019 10:50:02 -0800 (PST) Received: from [192.168.178.94] ([121.98.29.40]) by smtp.gmail.com with ESMTPSA id y197sm53528389pfc.79.2019.12.30.10.50.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 30 Dec 2019 10:50:02 -0800 (PST) Cc: mtk.manpages@gmail.com, linux-man@vger.kernel.org Subject: Re: [PATCH] netlink.7: fix alignment issue in example To: =?UTF-8?Q?Antonin_D=c3=a9cimo?= References: From: "Michael Kerrisk (man-pages)" Message-ID: Date: Mon, 30 Dec 2019 19:49:58 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.3.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: linux-man-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-man@vger.kernel.org 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/