media: i2c: fix an uninitialized error code
PATCH 1/1 media: i2c: fix an uninitialized error code
From: Arnd Bergmann <[email protected]>
Clang points out that the error handling in ov02a10_s_stream() is broken, and just returns a random error code:
drivers/media/i2c/ov02a10.c:537:6: warning: variable 'ret' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
if (ov02a10->streaming == on)
^~~~~~~~~~~~~~~~~~~~~~~~
drivers/media/i2c/ov02a10.c:568:9: note: uninitialized use occurs here
return ret;
^~~
drivers/media/i2c/ov02a10.c:537:2: note: remove the 'if' if its condition is always false
if (ov02a10->streaming == on)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/media/i2c/ov02a10.c:533:9: note: initialize the variable 'ret' to silence this warning
int ret;I assume that -EBUSY is the intended error code, so use that.
--- drivers/media/i2c/ov02a10.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/media/i2c/ov02a10.c +3 -1
@@ -534,8 +534,10 @@ static int ov02a10_s_stream(struct v4l2_subdev *sd, int on)mutex_lock (&ov02a10->mutex); -if (ov02a10->streaming == on) +if (ov02a10->streaming == on) { + ret = -EBUSY ; goto unlock_and_return; + }if (on) { ret =pm_runtime_get_sync (&client->dev);
Signature · 1 line
2.27.0
Hi Arnd,
Thanks for the patch.
Quoted 31 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
Clang points out that the error handling in
ov02a10_s_stream()is broken, and just returns a random error code:
drivers/media/i2c/ov02a10.c:537:6: warning: variable 'ret' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]if (ov02a10->streaming == on) ^~~~~~~~~~~~~~~~~~~~~~~~ drivers/media/i2c/ov02a10.c:568:9: note: uninitialized use occurs here return ret; ^~~ drivers/media/i2c/ov02a10.c:537:2: note: remove the 'if' if its condition is always false if (ov02a10->streaming == on) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ drivers/media/i2c/ov02a10.c:533:9: note: initialize the variable 'ret' to silence this warning int ret;I assume that
-EBUSYis the intended error code, so use that.Fixes 91807efbe8ec ("media: i2c: add OV02A10 image sensor driver")Signed-off-by Arnd Bergmann <[email protected]>--- drivers/media/i2c/ov02a10.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/media/i2c/ov02a10.c +0
-0@@ -534,8 +534,10 @@ static int ov02a10_s_stream(struct v4l2_subdev *sd, int on)
drivers/media/i2c/ov02a10.c static int ov02a10_s_stream(struct v4l2_subdev *sd, int on) · line 543
Quoted 10 lines
drivers/media/i2c/ov02a10.c +3
-1mutex_lock (&ov02a10->mutex); -if (ov02a10->streaming == on) +if (ov02a10->streaming == on) { + ret = -EBUSY ; goto unlock_and_return; + }if (on) { ret =pm_runtime_get_sync (&client->dev);
Only if sensor fails to stream on, ret can return a negative error code. Thus ret above needs to be initialized to '0'.
Also you could fix the clang error like this.
static int ov02a10_s_stream(struct v4l2_subdev *sd, int on)
{
struct ov02a10 *ov02a10 = to_ov02a10(sd);
...
int ret = 0;
...
if (ov02a10->streaming == on)
goto unlock_and_return;
...
}Quoted 45 lines · Dongchun Zhu
Hi Arnd,
Thanks for the patch.
Quoted 39 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
Clang points out that the error handling in
ov02a10_s_stream()is broken, and just returns a random error code:
drivers/media/i2c/ov02a10.c:537:6: warning: variable 'ret' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]if (ov02a10->streaming == on) ^~~~~~~~~~~~~~~~~~~~~~~~ drivers/media/i2c/ov02a10.c:568:9: note: uninitialized use occurs here return ret; ^~~ drivers/media/i2c/ov02a10.c:537:2: note: remove the 'if' if its condition is always false if (ov02a10->streaming == on) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ drivers/media/i2c/ov02a10.c:533:9: note: initialize the variable 'ret' to silence this warning int ret;I assume that
-EBUSYis the intended error code, so use that.Fixes 91807efbe8ec ("media: i2c: add OV02A10 image sensor driver")Signed-off-by Arnd Bergmann <[email protected]>--- drivers/media/i2c/ov02a10.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/media/i2c/ov02a10.c +3
-1@@ -534,8 +534,10 @@ static int ov02a10_s_stream(struct v4l2_subdev *sd, int on)mutex_lock (&ov02a10->mutex); -if (ov02a10->streaming == on) +if (ov02a10->streaming == on) { + ret = -EBUSY ; goto unlock_and_return; + }
Quoted 5 lines
drivers/media/i2c/ov02a10.c static int ov02a10_s_stream(struct v4l2_subdev *sd, int on) · line 543
Quoted 2 lines
drivers/media/i2c/ov02a10.c +0
-0if (on) { ret =pm_runtime_get_sync (&client->dev);Only if sensor fails to stream on, ret can return a negative error code. Thus ret above needs to be initialized to '0'.
Ok, I sent a version 2.
Quoted 14 lines
Also you could fix the clang error like this.
static int
ov02a10_s_stream(struct v4l2_subdev *sd, int on){ struct ov02a10 *ov02a10 = to_ov02a10(sd); ... int ret = 0; ... if (ov02a10->streaming == on) goto unlock_and_return; ... }
Sorry, I should have removed the last line of the output from clang that suggested doing that. Initializing a local variable in the declaration is generally a bad idea precisely because it prevents the compiler from warning about a case like this where the author forgot to set the correct error value.
We should really fix clang to not propagate that nonsense.
Arnd