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

From: Ian Rogers
Date: Fri Dec 13 2024 - 16:06:17 EST


container_of on a NULL may yield a non-NULL, confusion and errors.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
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