linux-doc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Gustavo A. R. Silva" <gustavoars@kernel.org>
To: Kees Cook <keescook@chromium.org>
Cc: Jonathan Corbet <corbet@lwn.net>,
	linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Gustavo A. R. Silva" <gustavo@embeddedor.com>
Subject: Re: [PATCH] docs: deprecated.rst: Add note to the use of struct_size() helper
Date: Thu, 4 Jun 2020 13:21:23 -0500	[thread overview]
Message-ID: <20200604182123.GD10051@embeddedor> (raw)
In-Reply-To: <202006041047.9B3E8FB951@keescook>

On Thu, Jun 04, 2020 at 10:49:19AM -0700, Kees Cook wrote:
> On Thu, Jun 04, 2020 at 12:21:38PM -0500, Gustavo A. R. Silva wrote:
> > Add a note to educate people about the proper use of struct_size() when
> > the trailing array in the enclosing structure is a one-element array.
> > 
> > Zero-length and one-element arrays will soon be removed from the kernel,
> > but in the meantime, it's worth letting people know how to correctly
> > use struct_size() together with such constructs.
> > 
> > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> > ---
> > 
> > Kees,
> >  
> > This is not substitute for the patch I'll write about flexible-arrays
> > and the deprecation of zero-lenght and one-element arrays.
> 
> Hm, hm. I think I'd rather just get the 0/1-array docs written, since
> that will mean this paragraph isn't needed at all. (Or rather, it can be

Yeah. My reasoning for is that it will take a while --at least one 
development cycle more-- to completely get rid of all the 0/1-arrays.
Also, this was motivated by the following comments from Christian
König:

"May I suggest that we add a section how to correctly do this to
Documentation/process/coding-style.rst or similar document?

I've seen a bunch of different approaches and some even doesn't work
with some gcc versions and result in a broken binary."[1]

> modified to say "if you're using struct_size() on a 1-array, stop using
> a 1-array, see [link]". If someone is going to switch around their code,
> they need to switch to flex at the same time, IMO.
> 

I agree with this. I can add the comments in quotes you suggest to this
patch.

But I think we can add this note while I continue working on the flexible-array
conversions. Once that work is complete, I can go back and update the
documentation. :)

What do you think?

Thanks
--
Gustavo

[1] https://lore.kernel.org/lkml/1065d63e-7959-e4b4-af4e-70607ba92296@amd.com/

> > 
> >  Documentation/process/deprecated.rst | 11 +++++++++++
> >  1 file changed, 11 insertions(+)
> > 
> > diff --git a/Documentation/process/deprecated.rst b/Documentation/process/deprecated.rst
> > index 652e2aa02a66c..0b7b37718bf96 100644
> > --- a/Documentation/process/deprecated.rst
> > +++ b/Documentation/process/deprecated.rst
> > @@ -85,6 +85,17 @@ Instead, use the helper::
> >  
> >  	header = kzalloc(struct_size(header, item, count), GFP_KERNEL);
> >  
> > +NOTE: You might want to use the following form in case the trailing array
> > +is a one-element array, as unlike zero-length arrays and flexible-array
> > +members, `one-element arrays occupy at least as much space as a single
> > +object of the type <https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html>`_,
> > +hence they contribute to the size of the enclosing structure::
> > +
> > +	header = kzalloc(struct_size(header, item, count - 1), GFP_KERNEL);
> > +
> > +It's also worth noting that one-element arrays --together with zero-length
> > +arrays-- will soon be completely removed from the codebase and deprecated.
> > +
> >  See array_size(), array3_size(), and struct_size(),
> >  for more details as well as the related check_add_overflow() and
> >  check_mul_overflow() family of functions.
> > -- 
> > 2.27.0
> > 
> 
> -- 
> Kees Cook

  reply	other threads:[~2020-06-04 18:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-04 17:21 [PATCH] docs: deprecated.rst: Add note to the use of struct_size() helper Gustavo A. R. Silva
2020-06-04 17:49 ` Kees Cook
2020-06-04 18:21   ` Gustavo A. R. Silva [this message]
2020-06-04 20:25     ` Kees Cook
2020-06-04 21:19       ` Gustavo A. R. Silva
2020-06-04 21:33         ` Kees Cook

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=20200604182123.GD10051@embeddedor \
    --to=gustavoars@kernel.org \
    --cc=corbet@lwn.net \
    --cc=gustavo@embeddedor.com \
    --cc=keescook@chromium.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@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 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).