Skip to contentSkip to conversation

media: v4l: Make media_entity_to_video_device() NULL-safe

PATCH Superseded Sakari Ailus 1 participant 1 message Jul 9, 2025 Media (V4L/DVB)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 media: v4l: Make media_entity_to_video_device() NULL-safe

Make media_entity_to_video_device(NULL) return NULL, instead of an invalid pointer value.

---
 include/media/v4l2-dev.h | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
include/media/v4l2-dev.h +11 -4
index 1b6222fab24e..e0461c697377 100644
@@ -313,10 +313,17 @@ struct video_device {
  * media_entity_to_video_device - Returns a &struct video_device from
  *	the &struct media_entity embedded on it.
  *
- * @__entity: pointer to &struct media_entity
- */
-#define media_entity_to_video_device(__entity) \
-	container_of(__entity, struct video_device, entity)
+ * @__entity: pointer to &struct media_entity, may be NULL
+ */
+#define media_entity_to_video_device(__entity)				\
+	({								\
+		typeof (__entity) __me_vdev_ent = __entity;		\
+									\
+		__me_vdev_ent ?						\
+			container_of(__me_vdev_ent,			\
+				     struct video_device, entity) :	\
+			NULL;						\
+	})
 
 /**
  * to_video_device - Returns a &struct video_device from the
Signature · 1 line
2.39.5