From: "Gustavo A. R. Silva" <gustavoars@kernel.org>
To: Greg KH <gregkh@linuxfoundation.org>,
Dan Carpenter <dan.carpenter@linaro.org>
Cc: linux-kernel@vger.kernel.org,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
linux-hardening@vger.kernel.org
Subject: [PATCH v2][next] container_of: add container_first() macro
Date: Wed, 29 Jan 2025 15:56:01 +1030 [thread overview]
Message-ID: <Z5m76U5zh34WbeQY@kspp> (raw)
This is like container_of_const() but it contains an assert to
ensure that it's using the first member in the structure.
Co-developed-by: Dan Carpenter <dan.carpenter@linaro.org>
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
I will be using this in my -Wflex-array-member-not-at-end patches. :)
Changes in v2:
- Base this on container_of_const().
v1:
- Link: https://lore.kernel.org/linux-hardening/Zu1vekikKNR5oUoM@elsanto/
include/linux/container_of.h | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/include/linux/container_of.h b/include/linux/container_of.h
index 713890c867be..1a5e5f32db92 100644
--- a/include/linux/container_of.h
+++ b/include/linux/container_of.h
@@ -35,4 +35,15 @@
default: ((type *)container_of(ptr, type, member)) \
)
+/**
+ * container_first - cast first member of a structure out to the containing
+ * structure and preserve the const-ness of the pointer.
+ * @ptr: the pointer to the member.
+ * @type: the type of the container struct this is embedded in.
+ * @member: the name of the member within the struct.
+ */
+#define container_first(ptr, type, member) ({ \
+ static_assert(offsetof(type, member) == 0, "not first member"); \
+ container_of_const(ptr, type, member); })
+
#endif /* _LINUX_CONTAINER_OF_H */
--
2.43.0
next reply other threads:[~2025-01-29 5:26 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-29 5:26 Gustavo A. R. Silva [this message]
2025-01-29 5:54 ` Greg KH
2025-01-29 7:29 ` Dan Carpenter
2025-01-29 7:33 ` Dan Carpenter
2025-01-29 8:04 ` Greg KH
2025-01-29 8:05 ` Gustavo A. R. Silva
2025-01-29 8:34 ` Greg KH
2025-01-29 10:39 ` Dan Carpenter
2025-01-29 13:14 ` Greg KH
2025-01-29 14:06 ` Dan Carpenter
2025-01-29 16:08 ` Greg KH
2025-01-29 23:36 ` Gustavo A. R. Silva
2025-01-30 6:14 ` Greg KH
2025-01-30 6:23 ` Dan Carpenter
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=Z5m76U5zh34WbeQY@kspp \
--to=gustavoars@kernel.org \
--cc=dan.carpenter@linaro.org \
--cc=gregkh@linuxfoundation.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
all inboxes | Powered by JetHome®