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

Signed-off-by: Mauro Carvalho Chehab <mchehab+sams...@kernel.org>
---
 drivers/media/i2c/ov9650.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/media/i2c/ov9650.c b/drivers/media/i2c/ov9650.c
index 8b56011446a9..cb49fda902fd 100644
--- a/drivers/media/i2c/ov9650.c
+++ b/drivers/media/i2c/ov9650.c
@@ -703,6 +703,11 @@ static int ov965x_set_gain(struct ov965x *ov965x, int 
auto_gain)
                for (m = 6; m >= 0; m--)
                        if (gain >= (1 << m) * 16)
                                break;
+
+               /* Sanity check: don't adjust the gain with a negative val */
+               if (m < 0)
+                       return -EINVAL;
+
                rgain = (gain - ((1 << m) * 16)) / (1 << m);
                rgain |= (((1 << m) - 1) << 4);
 
-- 
2.21.0

Reply via email to