diff mbox series

[4/7] media: ov9650: add a sanity check

Message ID 2868a99c83f1cfef93058f7e49aefa4b104b644f.1566502743.git.mchehab+samsung@kernel.org (mailing list archive)
State New, archived
Headers show
Series [1/7] media: remove include stdarg.h from some drivers | expand

Commit Message

Mauro Carvalho Chehab Aug. 22, 2019, 7:39 p.m. UTC
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+samsung@kernel.org>
---
 drivers/media/i2c/ov9650.c | 5 +++++
 1 file changed, 5 insertions(+)

Comments

On 8/22/19 21:39, Mauro Carvalho Chehab wrote:
> 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+samsung@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 */

s/val/value ?

> +		if (m < 0)
> +			return -EINVAL;

This will never happen as min value of V4L2_CID_GAIN control is 16
(gain is always >= 16 and m is always >= 0).
But if it suppresses the warning I'm fine with it.

Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
diff mbox series

Patch

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);