Skip to contentSkip to conversation

media: ov7670: check status of ov7670_read

PATCH In review [email protected] 3 participants 5 messages Aug 28, 2020 – Oct 6, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: ov7670: check status of ov7670_read

From: Tom Rix <[email protected]>

clang static analysis flags this representative problem

drivers/media/i2c/ov7670.c:1463:9: warning: Assigned
value is garbage or undefined

        *value = gain;
               ^ ~~~~

gain is set by a successful call to ov7670_read()

So check that ov7670_read() is successful.

The remaining static analysis problems are false positives. There appears to be a limitation with checking the aggregated returns.

---
 drivers/media/i2c/ov7670.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/media/i2c/ov7670.c +13 -4
index b42b289faaef..001d4b09db72 100644
@@ -929,6 +929,8 @@ static int ov7670_set_hw(struct v4l2_subdev *sd, int hstart, int hstop,
 	ret =  ov7670_write(sd, REG_HSTART, (hstart >> 3) & 0xff);
 	ret += ov7670_write(sd, REG_HSTOP, (hstop >> 3) & 0xff);
 	ret += ov7670_read(sd, REG_HREF, &v);
+	if (ret)
+		return ret;
 	v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x7);
 	msleep(10);
 	ret += ov7670_write(sd, REG_HREF, v);
@@ -938,6 +940,8 @@ static int ov7670_set_hw(struct v4l2_subdev *sd, int hstart, int hstop,
 	ret += ov7670_write(sd, REG_VSTART, (vstart >> 2) & 0xff);
 	ret += ov7670_write(sd, REG_VSTOP, (vstop >> 2) & 0xff);
 	ret += ov7670_read(sd, REG_VREF, &v);
+	if (ret)
+		return ret;
 	v = (v & 0xf0) | ((vstop & 0x3) << 2) | (vstart & 0x3);
 	msleep(10);
 	ret += ov7670_write(sd, REG_VREF, v);
@@ -1460,6 +1464,8 @@ static int ov7670_g_gain(struct v4l2_subdev *sd, __s32 *value)
 	unsigned char gain;
 
 	ret = ov7670_read(sd, REG_GAIN, &gain);
+	if (ret)
+		return ret;
 	*value = gain;
 	return ret;
 }
@@ -1470,11 +1476,14 @@ static int ov7670_s_gain(struct v4l2_subdev *sd, int value)
 	unsigned char com8;
 
 	ret = ov7670_write(sd, REG_GAIN, value & 0xff);
+	if (ret)
+		return ret;
 	/* Have to turn off AGC as well */
-	if (ret == 0) {
-		ret = ov7670_read(sd, REG_COM8, &com8);
-		ret = ov7670_write(sd, REG_COM8, com8 & ~COM8_AGC);
-	}
+	ret = ov7670_read(sd, REG_COM8, &com8);
+	if (ret)
+		return ret;
+	ret = ov7670_write(sd, REG_COM8, com8 & ~COM8_AGC);
+
 	return ret;
 }
 
Signature · 1 line
2.18.1
#1 lore raw
Quoted 11 lines · On Fri, Aug 28, 2020 at 7:55 AM <[email protected]> wrote:

From: Tom Rix <[email protected]>

clang static analysis flags this representative problem

drivers/media/i2c/ov7670.c:1463:9: warning: Assigned
value is garbage or undefined

        *value = gain;
               ^ ~~~~

gain is set by a successful call to ov7670_read()

Indeed, it looks like gain is only valid if the return value from ov7670_read() is >= 0. Would it be simpler to just initialize gain to 0 in ov7670_g_gain?

Side question; I'm super happy to see someone sending patches for things identified by clang's static analyzer. I'm curious, Tom, how did you run it? Did you use the recently landed in kbuild-next support for the make target make clang-analyzer?

Quoted 65 lines

So check that ov7670_read() is successful.

The remaining static analysis problems are false positives. There appears to be a limitation with checking the aggregated returns.

---
 drivers/media/i2c/ov7670.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/media/i2c/ov7670.c +13 -5
index b42b289faaef..001d4b09db72 100644
@@ -929,6 +929,8 @@ static int ov7670_set_hw(struct v4l2_subdev *sd, int hstart, int hstop,
        ret =  ov7670_write(sd, REG_HSTART, (hstart >> 3) & 0xff);
        ret += ov7670_write(sd, REG_HSTOP, (hstop >> 3) & 0xff);
        ret += ov7670_read(sd, REG_HREF, &v);
+       if (ret)
+               return ret;
        v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x7);
        msleep(10);
        ret += ov7670_write(sd, REG_HREF, v);
@@ -938,6 +940,8 @@ static int ov7670_set_hw(struct v4l2_subdev *sd, int hstart, int hstop,
        ret += ov7670_write(sd, REG_VSTART, (vstart >> 2) & 0xff);
        ret += ov7670_write(sd, REG_VSTOP, (vstop >> 2) & 0xff);
        ret += ov7670_read(sd, REG_VREF, &v);
+       if (ret)
+               return ret;
        v = (v & 0xf0) | ((vstop & 0x3) << 2) | (vstart & 0x3);
        msleep(10);
        ret += ov7670_write(sd, REG_VREF, v);
@@ -1460,6 +1464,8 @@ static int ov7670_g_gain(struct v4l2_subdev *sd, __s32 *value)
        unsigned char gain;
 
        ret = ov7670_read(sd, REG_GAIN, &gain);
+       if (ret)
+               return ret;
        *value = gain;
        return ret;
 }
@@ -1470,11 +1476,14 @@ static int ov7670_s_gain(struct v4l2_subdev *sd, int value)
        unsigned char com8;
 
        ret = ov7670_write(sd, REG_GAIN, value & 0xff);
+       if (ret)
+               return ret;
        /* Have to turn off AGC as well */
-       if (ret == 0) {
-               ret = ov7670_read(sd, REG_COM8, &com8);
-               ret = ov7670_write(sd, REG_COM8, com8 & ~COM8_AGC);
-       }
+       ret = ov7670_read(sd, REG_COM8, &com8);
+       if (ret)
+               return ret;
+       ret = ov7670_write(sd, REG_COM8, com8 & ~COM8_AGC);
+
        return ret;
 }
 
--

2.18.1

Signature · 2 lines
Thanks,
~Nick Desaulniers
author
#2 lore raw
Quoted 10 lines · Nick Desaulniers
Quoted 9 lines · On Fri, Aug 28, 2020 at 7:55 AM <[email protected]> wrote:

From: Tom Rix <[email protected]>

clang static analysis flags this representative problem

drivers/media/i2c/ov7670.c:1463:9: warning: Assigned
value is garbage or undefined

        *value = gain;
               ^ ~~~~
Quoted 4 lines
Quoted 1 line

gain is set by a successful call to ov7670_read()

Indeed, it looks like gain is only valid if the return value from ov7670_read() is >= 0. Would it be simpler to just initialize gain to 0 in ov7670_g_gain?

It looks like ov7670_read() subroutines both convert > 0 returns to 0.

Setting gain, and similar cases, to 0 means it is ok the setting side effect.  maybe it would be ok if this was a void return or there was some known poison value.  I did not look into that.  There are other routines that are more than a simple get() and i wanted to use the same fix for all of them.

yeah on clang static analysis making it into kbuild!  I have been doing these fixes for a couple of months.  with a just-built clang, i run 'scan-build make randomconfig', 'scan-build make', then run scan-view on the html.  the randomconfig accounts for the very scattered fixes i have posted.  generally i would say kernel is doing pretty well, maybe 20 to 1 false to real problems.

Tom

Quoted 71 lines

Side question; I'm super happy to see someone sending patches for things identified by clang's static analyzer. I'm curious, Tom, how did you run it? Did you use the recently landed in kbuild-next support for the make target make clang-analyzer?

Quoted 64 lines

So check that ov7670_read() is successful.

The remaining static analysis problems are false positives. There appears to be a limitation with checking the aggregated returns.

---
 drivers/media/i2c/ov7670.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/media/i2c/ov7670.c +13 -5
index b42b289faaef..001d4b09db72 100644
@@ -929,6 +929,8 @@ static int ov7670_set_hw(struct v4l2_subdev *sd, int hstart, int hstop,
        ret =  ov7670_write(sd, REG_HSTART, (hstart >> 3) & 0xff);
        ret += ov7670_write(sd, REG_HSTOP, (hstop >> 3) & 0xff);
        ret += ov7670_read(sd, REG_HREF, &v);
+       if (ret)
+               return ret;
        v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x7);
        msleep(10);
        ret += ov7670_write(sd, REG_HREF, v);
@@ -938,6 +940,8 @@ static int ov7670_set_hw(struct v4l2_subdev *sd, int hstart, int hstop,
        ret += ov7670_write(sd, REG_VSTART, (vstart >> 2) & 0xff);
        ret += ov7670_write(sd, REG_VSTOP, (vstop >> 2) & 0xff);
        ret += ov7670_read(sd, REG_VREF, &v);
+       if (ret)
+               return ret;
        v = (v & 0xf0) | ((vstop & 0x3) << 2) | (vstart & 0x3);
        msleep(10);
        ret += ov7670_write(sd, REG_VREF, v);
@@ -1460,6 +1464,8 @@ static int ov7670_g_gain(struct v4l2_subdev *sd, __s32 *value)
        unsigned char gain;
 
        ret = ov7670_read(sd, REG_GAIN, &gain);
+       if (ret)
+               return ret;
        *value = gain;
        return ret;
 }
@@ -1470,11 +1476,14 @@ static int ov7670_s_gain(struct v4l2_subdev *sd, int value)
        unsigned char com8;
 
        ret = ov7670_write(sd, REG_GAIN, value & 0xff);
+       if (ret)
+               return ret;
        /* Have to turn off AGC as well */
-       if (ret == 0) {
-               ret = ov7670_read(sd, REG_COM8, &com8);
-               ret = ov7670_write(sd, REG_COM8, com8 & ~COM8_AGC);
-       }
+       ret = ov7670_read(sd, REG_COM8, &com8);
+       if (ret)
+               return ret;
+       ret = ov7670_write(sd, REG_COM8, com8 & ~COM8_AGC);
+
        return ret;
 }
 
--

2.18.1

#3 lore raw
In reply to [email protected]

Hi Tom,

Quoted 27 lines · On Fri, Aug 28, 2020 at 07:55:18AM -0700, [email protected] wrote:

From: Tom Rix <[email protected]>

clang static analysis flags this representative problem

drivers/media/i2c/ov7670.c:1463:9: warning: Assigned
value is garbage or undefined

        *value = gain;
               ^ ~~~~

gain is set by a successful call to ov7670_read()

So check that ov7670_read() is successful.

The remaining static analysis problems are false positives. There appears to be a limitation with checking the aggregated returns.

---
 drivers/media/i2c/ov7670.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/media/i2c/ov7670.c +0 -0
index b42b289faaef..001d4b09db72 100644

drivers/media/i2c/ov7670.c static int ov7670_set_hw(struct v4l2_subdev *sd, int hstart, int hstop, · line 933

Quoted 5 lines
drivers/media/i2c/ov7670.c +2 -0
@@ -929,6 +929,8 @@ static int ov7670_set_hw(struct v4l2_subdev *sd, int hstart, int hstop,
 	ret =  ov7670_write(sd, REG_HSTART, (hstart >> 3) & 0xff);
 	ret += ov7670_write(sd, REG_HSTOP, (hstop >> 3) & 0xff);
 	ret += ov7670_read(sd, REG_HREF, &v);
+	if (ret)
+		return ret;

Thanks for the patch.

While the patch fixes a bug, could you also fix adding the return values? These are valid error codes to begin with, but it makes no sense to add them together.

Quoted 40 lines
drivers/media/i2c/ov7670.c +11 -4
 	v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x7);
 	msleep(10);
 	ret += ov7670_write(sd, REG_HREF, v);
@@ -938,6 +940,8 @@ static int ov7670_set_hw(struct v4l2_subdev *sd, int hstart, int hstop,
 	ret += ov7670_write(sd, REG_VSTART, (vstart >> 2) & 0xff);
 	ret += ov7670_write(sd, REG_VSTOP, (vstop >> 2) & 0xff);
 	ret += ov7670_read(sd, REG_VREF, &v);
+	if (ret)
+		return ret;
 	v = (v & 0xf0) | ((vstop & 0x3) << 2) | (vstart & 0x3);
 	msleep(10);
 	ret += ov7670_write(sd, REG_VREF, v);
@@ -1460,6 +1464,8 @@ static int ov7670_g_gain(struct v4l2_subdev *sd, __s32 *value)
 	unsigned char gain;
 
 	ret = ov7670_read(sd, REG_GAIN, &gain);
+	if (ret)
+		return ret;
 	*value = gain;
 	return ret;
 }
@@ -1470,11 +1476,14 @@ static int ov7670_s_gain(struct v4l2_subdev *sd, int value)
 	unsigned char com8;
 
 	ret = ov7670_write(sd, REG_GAIN, value & 0xff);
+	if (ret)
+		return ret;
 	/* Have to turn off AGC as well */
-	if (ret == 0) {
-		ret = ov7670_read(sd, REG_COM8, &com8);
-		ret = ov7670_write(sd, REG_COM8, com8 & ~COM8_AGC);
-	}
+	ret = ov7670_read(sd, REG_COM8, &com8);
+	if (ret)
+		return ret;
+	ret = ov7670_write(sd, REG_COM8, com8 & ~COM8_AGC);
+
 	return ret;
 }
 
Signature · 3 lines
Regards,

Sakari Ailus
author
#4 lore raw
Quoted 37 lines · Sakari Ailus

Hi Tom,

Quoted 27 lines · On Fri, Aug 28, 2020 at 07:55:18AM -0700, [email protected] wrote:

From: Tom Rix <[email protected]>

clang static analysis flags this representative problem

drivers/media/i2c/ov7670.c:1463:9: warning: Assigned
value is garbage or undefined

        *value = gain;
               ^ ~~~~

gain is set by a successful call to ov7670_read()

So check that ov7670_read() is successful.

The remaining static analysis problems are false positives. There appears to be a limitation with checking the aggregated returns.

---
 drivers/media/i2c/ov7670.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/media/i2c/ov7670.c +0 -0
index b42b289faaef..001d4b09db72 100644

drivers/media/i2c/ov7670.c static int ov7670_set_hw(struct v4l2_subdev *sd, int hstart, int hstop, · line 933

Quoted 5 lines
drivers/media/i2c/ov7670.c +2 -0
@@ -929,6 +929,8 @@ static int ov7670_set_hw(struct v4l2_subdev *sd, int hstart, int hstop,
 	ret =  ov7670_write(sd, REG_HSTART, (hstart >> 3) & 0xff);
 	ret += ov7670_write(sd, REG_HSTOP, (hstop >> 3) & 0xff);
 	ret += ov7670_read(sd, REG_HREF, &v);
+	if (ret)
+		return ret;

Thanks for the patch.

Quoted 3 lines

While the patch fixes a bug, could you also fix adding the return values? These are valid error codes to begin with, but it makes no sense to add them together.

Yes. That is a problem, a general problem with this file.

I count 10+

I'll see if fixing the general problem also fixes this problem.

Tom

Quoted 41 lines
Quoted 41 lines
drivers/media/i2c/ov7670.c +11 -4
 	v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x7);
 	msleep(10);
 	ret += ov7670_write(sd, REG_HREF, v);
@@ -938,6 +940,8 @@ static int ov7670_set_hw(struct v4l2_subdev *sd, int hstart, int hstop,
 	ret += ov7670_write(sd, REG_VSTART, (vstart >> 2) & 0xff);
 	ret += ov7670_write(sd, REG_VSTOP, (vstop >> 2) & 0xff);
 	ret += ov7670_read(sd, REG_VREF, &v);
+	if (ret)
+		return ret;
 	v = (v & 0xf0) | ((vstop & 0x3) << 2) | (vstart & 0x3);
 	msleep(10);
 	ret += ov7670_write(sd, REG_VREF, v);
@@ -1460,6 +1464,8 @@ static int ov7670_g_gain(struct v4l2_subdev *sd, __s32 *value)
 	unsigned char gain;
 
 	ret = ov7670_read(sd, REG_GAIN, &gain);
+	if (ret)
+		return ret;
 	*value = gain;
 	return ret;
 }
@@ -1470,11 +1476,14 @@ static int ov7670_s_gain(struct v4l2_subdev *sd, int value)
 	unsigned char com8;
 
 	ret = ov7670_write(sd, REG_GAIN, value & 0xff);
+	if (ret)
+		return ret;
 	/* Have to turn off AGC as well */
-	if (ret == 0) {
-		ret = ov7670_read(sd, REG_COM8, &com8);
-		ret = ov7670_write(sd, REG_COM8, com8 & ~COM8_AGC);
-	}
+	ret = ov7670_read(sd, REG_COM8, &com8);
+	if (ret)
+		return ret;
+	ret = ov7670_write(sd, REG_COM8, com8 & ~COM8_AGC);
+
 	return ret;
 }