media: tegra-video: vi: fix invalid u32 return value in format lookup
Subsystems (4)
PATCH 1/1 media: tegra-video: vi: fix invalid u32 return value in format lookup
tegra_get_format_fourcc_by_idx() returns a u32 but uses -EINVAL to signal an out-of-bounds index. This results in a large unsigned value being returned, which may be interpreted as a valid fourcc.
Return 0 instead to indicate an invalid format.
Callers assign the return value directly to pixelformat, so returning an error code encoded in u32 is unsafe.
--- drivers/staging/media/tegra-video/vi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/media/tegra-video/vi.c +1 -1
@@ -81,7 +81,7 @@ static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi, unsigned int index) {if (index >= vi->soc->nformats) -return -EINVAL ; +return 0 ;return vi->soc->video_formats[index].fourcc; }
Signature · 1 line
2.34.1
Quoted 19 lines · Hungyu Lin
tegra_get_format_fourcc_by_idx()returns a u32 but uses-EINVALto signal an out-of-bounds index. This results in a large unsigned value being returned, which may be interpreted as a valid fourcc.Return 0 instead to indicate an invalid format.
Callers assign the return value directly to pixelformat, so returning an error code encoded in u32 is unsafe.
Signed-off-by Hungyu Lin <[email protected]>--- drivers/staging/media/tegra-video/vi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/staging/media/tegra-video/vi.c +0
-0
drivers/staging/media/tegra-video/vi.c static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi, · line 84
Quoted 5 lines
drivers/staging/media/tegra-video/vi.c +1
-1@@ -81,7 +81,7 @@ static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi, unsigned int index) {if (index >= vi->soc->nformats) -return -EINVAL ; +return 0 ;
Someone else just sent a patch for this which added error checking to the caller. I liked that approach better. (I haven't checked to see if your approach works).
But either way this needs further review to see if the bug is actually real and if so what is the Fixes tag?
regards,
dan carpenter
Quoted 19 lines · Hungyu Lin
tegra_get_format_fourcc_by_idx()returns a u32 but uses-EINVALto signal an out-of-bounds index. This results in a large unsigned value being returned, which may be interpreted as a valid fourcc.Return 0 instead to indicate an invalid format.
Callers assign the return value directly to pixelformat, so returning an error code encoded in u32 is unsafe.
Signed-off-by Hungyu Lin <[email protected]>--- drivers/staging/media/tegra-video/vi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/staging/media/tegra-video/vi.c +0
-0
drivers/staging/media/tegra-video/vi.c static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi, · line 84
Quoted 5 lines
drivers/staging/media/tegra-video/vi.c +1
-1@@ -81,7 +81,7 @@ static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi, unsigned int index) {if (index >= vi->soc->nformats) -return -EINVAL ; +return 0 ;
Returning 0 is not a valid fourcc either.
This should never happen, so use WARN_ON_ONCE in the 'if' and return vi->soc->video_formats[0].fourcc;
That's at least better than the current code.
Regards,
Hans
Quoted 3 lines
drivers/staging/media/tegra-video/vi.c +0
-0return vi->soc->video_formats[index].fourcc; }