[PATCH v1 5/8] tools headers: Add non-NULL assert to container_of

Ian Rogers posted 8 patches 1 year ago
There is a newer version of this series
[PATCH v1 5/8] tools headers: Add non-NULL assert to container_of
Posted by Ian Rogers 1 year ago
container_of on a NULL may yield a non-NULL, confusion and errors.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/include/linux/kernel.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/include/linux/kernel.h b/tools/include/linux/kernel.h
index 10f74f021d55..8d617467c5c5 100644
--- a/tools/include/linux/kernel.h
+++ b/tools/include/linux/kernel.h
@@ -38,6 +38,7 @@
 	static_assert(__same_type(*(ptr), ((type *)0)->member) ||	\
 		      __same_type(*(ptr), void),			\
 		      "pointer type mismatch in container_of()");	\
+	assert(ptr != NULL);						\
 	((type *)(__mptr - offsetof(type, member))); })
 #endif
 
-- 
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v1 5/8] tools headers: Add non-NULL assert to container_of
Posted by Kuan-Wei Chiu 1 year ago
On Fri, Dec 13, 2024 at 01:04:22PM -0800, Ian Rogers wrote:
> container_of on a NULL may yield a non-NULL, confusion and errors.
> 
> Signed-off-by: Ian Rogers <irogers@google.com>

Reviewed-by: Kuan-Wei Chiu <visitorckw@gmail.com>

Regards,
Kuan-Wei