mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Kees Cook <keescook@chromium.org>, linux-kernel@vger.kernel.org
Cc: Borislav Petkov <bp@alien8.de>
Subject: [RFC][PATCH] overflow: Twiddle with struct_size()
Date: Wed, 5 Mar 2025 14:43:15 +0100	[thread overview]
Message-ID: <20250305134315.GB16878@noisy.programming.kicks-ass.net> (raw)

Hi Kees,

I keep getting hit by the struct_size() brigade, and I keep having
trouble reading that macro.

I had a wee poke and ended up with the below, WDYT?

(I also tried to create a __must_be_flex_array(), but utterly failed :/)

---
diff --git a/include/linux/overflow.h b/include/linux/overflow.h
index 0c7e3dcfe867..2123d0e238bb 100644
--- a/include/linux/overflow.h
+++ b/include/linux/overflow.h
@@ -352,9 +352,10 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
  * Return: number of bytes needed or SIZE_MAX on overflow.
  */
 #define flex_array_size(p, member, count)				\
-	__builtin_choose_expr(__is_constexpr(count),			\
-		(count) * sizeof(*(p)->member) + __must_be_array((p)->member),	\
-		size_mul(count, sizeof(*(p)->member) + __must_be_array((p)->member)))
+	(__must_be_array((p)->member) +					\
+	 __builtin_choose_expr(__is_constexpr(count),			\
+			       sizeof(*(p)->member) * (count),		\
+			       size_mul(sizeof(*(p)->member), (count))))
 
 /**
  * struct_size() - Calculate size of structure with trailing flexible array.
@@ -367,10 +368,12 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
  *
  * Return: number of bytes needed or SIZE_MAX on overflow.
  */
-#define struct_size(p, member, count)					\
-	__builtin_choose_expr(__is_constexpr(count),			\
-		sizeof(*(p)) + flex_array_size(p, member, count),	\
-		size_add(sizeof(*(p)), flex_array_size(p, member, count)))
+#define struct_size(p, member, count)					       \
+	(__must_be_array((p)->member) +					       \
+	 __builtin_choose_expr(__is_constexpr(count),			       \
+			       sizeof(*(p)) + (sizeof((p)->member) * (count)), \
+			       size_add(sizeof(*(p)),			       \
+					size_mul(sizeof(*(p)->member), count))))
 
 /**
  * struct_size_t() - Calculate size of structure with trailing flexible array

             reply	other threads:[~2025-03-05 13:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-05 13:43 Peter Zijlstra [this message]
2025-03-06  7:13 ` Kees Cook
2025-03-06  8:40   ` Peter Zijlstra

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=20250305134315.GB16878@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=bp@alien8.de \
    --cc=keescook@chromium.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

all inboxes | Powered by JetHome®