Skip to contentSkip to conversation

media: v4l: Guarantee non-NULL return from v4l2_find_nearest_size()

PATCH In review Sakari Ailus 2 participants 2 messages Apr 15, 2024 – Apr 22, 2024 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: v4l: Guarantee non-NULL return from v4l2_find_nearest_size()

Guarantee that v4l2_find_nearest_size() returns a non-NULL value when the mode array passed to it has non-zero number of entries.

Fixes 95ce9c28601a ("media: v4l: common: Add a function to obtain best size from a list")
---
 drivers/media/v4l2-core/v4l2-common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-common.c +1 -1
index d34d210908d9..12e8cbe92d7c 100644
@@ -159,7 +159,7 @@ __v4l2_find_nearest_size(const void *array, size_t array_size,
 			 size_t height_offset, s32 width, s32 height)
 {
 	u32 error, min_error = U32_MAX;
-	const void *best = NULL;
+	const void *best = array;
 	unsigned int i;
 
 	if (!array)
Signature · 1 line
2.39.2
#1 lore raw
Quoted 14 lines · Sakari Ailus

Guarantee that v4l2_find_nearest_size() returns a non-NULL value when the mode array passed to it has non-zero number of entries.

Fixes 95ce9c28601a ("media: v4l: common: Add a function to obtain best size from a list")
---
 drivers/media/v4l2-core/v4l2-common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-common.c +0 -0
index d34d210908d9..12e8cbe92d7c 100644

drivers/media/v4l2-core/v4l2-common.c __v4l2_find_nearest_size(const void *array, size_t array_size, · line 165

Quoted 8 lines
drivers/media/v4l2-core/v4l2-common.c +1 -1
@@ -159,7 +159,7 @@ __v4l2_find_nearest_size(const void *array, size_t array_size,
 			 size_t height_offset, s32 width, s32 height)
 {
 	u32 error, min_error = U32_MAX;
-	const void *best = NULL;
+	const void *best = array;
 	unsigned int i;
 
 	if (!array)

Has this actually been observed? I don't think it is possible for this to happen. In fact, I think this actually introduces a new bug: if array != NULL and array_size == 0, then this would return NULL (correct), but with this patch it returns 'array' (wrong).

I will drop this patch from the PR until this is clarified.

If this issue was observed, then I can't help thinking that this patch just addresses the symptom and not the underlying cause.

Regards,

Hans