Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

media: ov9650: add a sanity check

As pointed by cppcheck:

[drivers/media/i2c/ov9650.c:706]: (error) Shifting by a negative value is undefined behaviour
[drivers/media/i2c/ov9650.c:707]: (error) Shifting by a negative value is undefined behaviour
[drivers/media/i2c/ov9650.c:721]: (error) Shifting by a negative value is undefined behaviour

Prevent mangling with gains with invalid values.

As pointed by Sylvester, this should never happen in practice,
as min value of V4L2_CID_GAIN control is 16 (gain is always >= 16
and m is always >= 0), but it is too hard for a static analyzer
to get this, as the logic with validates control min/max is
elsewhere inside V4L2 core.

Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>

+5
+5
drivers/media/i2c/ov9650.c
··· 703 703 for (m = 6; m >= 0; m--) 704 704 if (gain >= (1 << m) * 16) 705 705 break; 706 + 707 + /* Sanity check: don't adjust the gain with a negative value */ 708 + if (m < 0) 709 + return -EINVAL; 710 + 706 711 rgain = (gain - ((1 << m) * 16)) / (1 << m); 707 712 rgain |= (((1 << m) - 1) << 4); 708 713