public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Kees Cook <kees@kernel.org>
To: "Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2][next] overflow: Add STACK_FLEX_ARRAY_SIZE() helper
Date: Tue, 22 Apr 2025 13:39:32 -0700	[thread overview]
Message-ID: <202504221338.05240DB@keescook> (raw)
In-Reply-To: <8f9ab8fcd26ce59c0e0e25e095b446a77849c08e.1745342381.git.gustavoars@kernel.org>

On Tue, Apr 22, 2025 at 11:21:36AM -0600, Gustavo A. R. Silva wrote:
> Add new STACK_FLEX_ARRAY_SIZE() helper to get the size of a
> flexible-array member defined using DEFINE_FLEX()/DEFINE_RAW_FLEX()
> at compile time.
> 
> This is essentially the same as ARRAY_SIZE() but for on-stack
> flexible-array members.
> 
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
>  include/linux/overflow.h | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
> 
> diff --git a/include/linux/overflow.h b/include/linux/overflow.h
> index 6ee67c20b575..5b2e0cc9aba2 100644
> --- a/include/linux/overflow.h
> +++ b/include/linux/overflow.h
> @@ -420,6 +420,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
>   * flexible array member.
>   * Use __struct_size(@name) to get compile-time size of it afterwards.
>   * Use __member_size(@name->member) to get compile-time size of @name members.
> + * Use STACK_FLEX_ARRAY_SIZE(@name, @member) to get compile-time size of array @member.

Instead of "size of" how about "number of elements in"

>   */
>  #define DEFINE_RAW_FLEX(type, name, member, count)	\
>  	_DEFINE_FLEX(type, name, member, count, = {})
> @@ -438,8 +439,20 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
>   * flexible array member.
>   * Use __struct_size(@NAME) to get compile-time size of it afterwards.
>   * Use __member_size(@NAME->member) to get compile-time size of @NAME members.
> + * Use STACK_FLEX_ARRAY_SIZE(@name, @member) to get compile-time size of array @member.

Same here.

>   */
>  #define DEFINE_FLEX(TYPE, NAME, MEMBER, COUNTER, COUNT)	\
>  	_DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .obj.COUNTER = COUNT, })
>  
> +/**
> + * STACK_FLEX_ARRAY_SIZE() - helper macro for DEFINE_FLEX() family.
> + * Returns the number of elements in @array.

Which matches these docs.

> + *
> + * @name: Name for a variable defined in DEFINE_RAW_FLEX()/DEFINE_FLEX().
> + * @array: Name of the array member.
> + */
> +#define STACK_FLEX_ARRAY_SIZE(name, array)						\
> +	(__member_size((name)->array) / sizeof(*(name)->array) +			\
> +						__must_be_array((name)->array))
> +
>  #endif /* __LINUX_OVERFLOW_H */
> -- 
> 2.43.0
> 

-- 
Kees Cook

  reply	other threads:[~2025-04-22 20:39 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-22 17:20 [PATCH 0/2][next] Add STACK_FLEX_ARRAY_SIZE() helper Gustavo A. R. Silva
2025-04-22 17:21 ` [PATCH 1/2][next] overflow: " Gustavo A. R. Silva
2025-04-22 20:39   ` Kees Cook [this message]
2025-04-22 17:22 ` [PATCH 2/2][next] kunit/overflow: Add tests for " Gustavo A. R. Silva
2025-04-22 20:39   ` Kees Cook
2025-04-22 20:48     ` Gustavo A. R. Silva

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=202504221338.05240DB@keescook \
    --to=kees@kernel.org \
    --cc=gustavoars@kernel.org \
    --cc=linux-hardening@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