Skip to contentSkip to conversation

Fixes to cppcheck errors in v4l2-ioctl.c

PATCH In review Shuah Khan 3 participants 10 messages Jun 5, 2019 – Jun 13, 2019 Media (V4L/DVB)

lore
Share & tools
lore

Shuah Khan

author
#0 lore raw

cover Fixes to cppcheck errors in v4l2-ioctl.c

cppcheck runs on the kernel found a couple of cppcheck errors in v4l2-ioctl.c. These two patches fix them.

Shuah Khan (2):
media: v4l2-core: Shifting signed 32-bit value by 31 bits error
media: v4l2-core: fix uninitialized variable error

 drivers/media/v4l2-core/v4l2-ioctl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH 1/2 media: v4l2-core: Shifting signed 32-bit value by 31 bits error

Fix the following cppcheck error:

Checking drivers/media/v4l2-core/v4l2-ioctl.c ...
[drivers/media/v4l2-core/v4l2-ioctl.c:1370]: (error) Shifting signed 32-bit value by 31 bits is undefined behaviour

---
 drivers/media/v4l2-core/v4l2-ioctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-ioctl.c +1 -1
index 6859bdac86fe..333e387bafeb 100644
@@ -1364,7 +1364,7 @@ static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
 					(char)((fmt->pixelformat >> 8) & 0x7f),
 					(char)((fmt->pixelformat >> 16) & 0x7f),
 					(char)((fmt->pixelformat >> 24) & 0x7f),
-					(fmt->pixelformat & (1 << 31)) ? "-BE" : "");
+					(fmt->pixelformat & BIT(31)) ? "-BE" : "");
 			break;
 		}
 	}
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH 1/2] media: v4l2-core: Shifting signed 32-bit value by 31 bits error

Quoted 16 lines · Shuah Khan

Fix the following cppcheck error:

Checking drivers/media/v4l2-core/v4l2-ioctl.c ...
[drivers/media/v4l2-core/v4l2-ioctl.c:1370]: (error) Shifting signed 32-bit value by 31 bits is undefined behaviour

---
 drivers/media/v4l2-core/v4l2-ioctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-ioctl.c +0 -0
index 6859bdac86fe..333e387bafeb 100644

drivers/media/v4l2-core/v4l2-ioctl.c static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt) · line 1370

Quoted 8 lines
drivers/media/v4l2-core/v4l2-ioctl.c +1 -1
@@ -1364,7 +1364,7 @@ static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
 					(char)((fmt->pixelformat >> 8) & 0x7f),
 					(char)((fmt->pixelformat >> 16) & 0x7f),
 					(char)((fmt->pixelformat >> 24) & 0x7f),
-					(fmt->pixelformat & (1 << 31)) ? "-BE" : "");
+					(fmt->pixelformat & BIT(31)) ? "-BE" : "");
 			break;
 		}
 	}

If this builds, I guess #define BIT(x) got pulled in indirectly since bits.h nor bitops.h is currently #included in that source file.

Documentation/process/submit-checklist.rst rule #1 says:
1) If you use a facility then #include the file that defines/declares that facility. Don't depend on other header files pulling in ones that you use.

Please add #include <linux/bits or bitops.h>

Signature · 1 line
~Randy
#3 lore raw

Re: [PATCH 1/2] media: v4l2-core: Shifting signed 32-bit value by 31 bits error

Quoted 29 lines · Randy Dunlap
Quoted 16 lines · Shuah Khan

Fix the following cppcheck error:

Checking drivers/media/v4l2-core/v4l2-ioctl.c ...
[drivers/media/v4l2-core/v4l2-ioctl.c:1370]: (error) Shifting signed 32-bit value by 31 bits is undefined behaviour

---
 drivers/media/v4l2-core/v4l2-ioctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-ioctl.c +0 -0
index 6859bdac86fe..333e387bafeb 100644

drivers/media/v4l2-core/v4l2-ioctl.c static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt) · line 1370

Quoted 8 lines
drivers/media/v4l2-core/v4l2-ioctl.c +1 -1
@@ -1364,7 +1364,7 @@ static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
 					(char)((fmt->pixelformat >> 8) & 0x7f),
 					(char)((fmt->pixelformat >> 16) & 0x7f),
 					(char)((fmt->pixelformat >> 24) & 0x7f),
-					(fmt->pixelformat & (1 << 31)) ? "-BE" : "");
+					(fmt->pixelformat & BIT(31)) ? "-BE" : "");
 			break;
 		}
 	}

If this builds, I guess #define BIT(x) got pulled in indirectly since bits.h nor bitops.h is currently #included in that source file.

Quoted 7 lines

Documentation/process/submit-checklist.rst rule #1 says:
1) If you use a facility then #include the file that defines/declares that facility. Don't depend on other header files pulling in ones that you use.

Please add #include <linux/bits or bitops.h>

I'm not sure about this patch. '1 << 31' is used all over in the kernel, including in public headers (e.g. media.h, videodev2.h).

It seems arbitrary to change it only here, but not anywhere else.

In this particular example for the fourcc handling I would prefer to just use '1U << 31', both in v4l2-ioctl.c and videodev2.h.

A separate patch doing the same for MEDIA_ENT_ID_FLAG_NEXT in media.h would probably be a good idea either: that way the public API at least will do the right thing.

Regards,

Hans

author
#4 lore raw

Re: [PATCH 1/2] media: v4l2-core: Shifting signed 32-bit value by 31 bits error

Quoted 22 lines · Hans Verkuil
Quoted 21 lines · Randy Dunlap
Quoted 20 lines · Shuah Khan

Fix the following cppcheck error:

Checking drivers/media/v4l2-core/v4l2-ioctl.c ...
[drivers/media/v4l2-core/v4l2-ioctl.c:1370]: (error) Shifting signed 32-bit value by 31 bits is undefined behaviour

---
  drivers/media/v4l2-core/v4l2-ioctl.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-ioctl.c +1 -1
index 6859bdac86fe..333e387bafeb 100644
@@ -1364,7 +1364,7 @@ static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
  					(char)((fmt->pixelformat >> 8) & 0x7f),
  					(char)((fmt->pixelformat >> 16) & 0x7f),
  					(char)((fmt->pixelformat >> 24) & 0x7f),
-					(fmt->pixelformat & (1 << 31)) ? "-BE" : "");
+					(fmt->pixelformat & BIT(31)) ? "-BE" : "");
Quoted 8 lines
Quoted 8 lines

drivers/media/v4l2-core/v4l2-ioctl.c static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt) · line 1370

Quoted 4 lines
drivers/media/v4l2-core/v4l2-ioctl.c +0 -0
  			break;
  		}
  	}

If this builds, I guess #define BIT(x) got pulled in indirectly since bits.h nor bitops.h is currently #included in that source file.

It does build. You are right that I should have included bitops.h

Quoted 5 lines
Quoted 5 lines

Documentation/process/submit-checklist.rst rule #1 says:
1) If you use a facility then #include the file that defines/declares that facility. Don't depend on other header files pulling in ones that you use.

Quoted 8 lines
Quoted 2 lines

Please add #include <linux/bits or bitops.h>

I'm not sure about this patch. '1 << 31' is used all over in the kernel, including in public headers (e.g. media.h, videodev2.h).

It seems arbitrary to change it only here, but not anywhere else.

Right. We have several places in the kernel that do that.

Quoted 3 lines

In this particular example for the fourcc handling I would prefer to just use '1U << 31', both in v4l2-ioctl.c and videodev2.h.

If you would like to take the patch, I can send v2 fixing it using 1U << 31 - This is simpler since it doesn't nee additional includes.

Quoted 4 lines

A separate patch doing the same for MEDIA_ENT_ID_FLAG_NEXT in media.h would probably be a good idea either: that way the public API at least will do the right thing.

I should have explained it better. I wanted to start with one or two places first to see if it is worth our time to fix these:

The full kernel cppcheck log for "Shifting signed 32-bit value by 31 bits is undefined behaviour" can be found at:

https://drive.google.com/file/d/19Xu7UqBGJ7BpzxEp92ZQYb6F8UPrk3z3/view

thanks,
-- Shuah

#5 lore raw

Re: [PATCH 1/2] media: v4l2-core: Shifting signed 32-bit value by 31 bits error

Quoted 48 lines · Shuah Khan
Quoted 22 lines · Hans Verkuil
Quoted 21 lines · Randy Dunlap
Quoted 20 lines · Shuah Khan

Fix the following cppcheck error:

Checking drivers/media/v4l2-core/v4l2-ioctl.c ...
[drivers/media/v4l2-core/v4l2-ioctl.c:1370]: (error) Shifting signed 32-bit value by 31 bits is undefined behaviour

---
  drivers/media/v4l2-core/v4l2-ioctl.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-ioctl.c +1 -1
index 6859bdac86fe..333e387bafeb 100644
@@ -1364,7 +1364,7 @@ static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
  					(char)((fmt->pixelformat >> 8) & 0x7f),
  					(char)((fmt->pixelformat >> 16) & 0x7f),
  					(char)((fmt->pixelformat >> 24) & 0x7f),
-					(fmt->pixelformat & (1 << 31)) ? "-BE" : "");
+					(fmt->pixelformat & BIT(31)) ? "-BE" : "");
Quoted 8 lines
Quoted 8 lines

drivers/media/v4l2-core/v4l2-ioctl.c static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt) · line 1370

Quoted 4 lines
drivers/media/v4l2-core/v4l2-ioctl.c +0 -0
  			break;
  		}
  	}

If this builds, I guess #define BIT(x) got pulled in indirectly since bits.h nor bitops.h is currently #included in that source file.

It does build. You are right that I should have included bitops.h

Quoted 5 lines
Quoted 5 lines

Documentation/process/submit-checklist.rst rule #1 says:
1) If you use a facility then #include the file that defines/declares that facility. Don't depend on other header files pulling in ones that you use.

Quoted 8 lines
Quoted 2 lines

Please add #include <linux/bits or bitops.h>

I'm not sure about this patch. '1 << 31' is used all over in the kernel, including in public headers (e.g. media.h, videodev2.h).

It seems arbitrary to change it only here, but not anywhere else.

Quoted 8 lines

Right. We have several places in the kernel that do that.

Quoted 3 lines

In this particular example for the fourcc handling I would prefer to just use '1U << 31', both in v4l2-ioctl.c and videodev2.h.

If you would like to take the patch, I can send v2 fixing it using 1U << 31 - This is simpler since it doesn't nee additional includes.

I would like to have this cleaned up in the public media APIs. Those can be used by other compilers as well and it makes sense to me not to have undefined behavior in those headers.

Quoted 6 lines
Quoted 5 lines

A separate patch doing the same for MEDIA_ENT_ID_FLAG_NEXT in media.h would probably be a good idea either: that way the public API at least will do the right thing.

Quoted 7 lines

I should have explained it better. I wanted to start with one or two places first to see if it is worth our time to fix these:

The full kernel cppcheck log for "Shifting signed 32-bit value by 31 bits is undefined behaviour" can be found at:

https://drive.google.com/file/d/19Xu7UqBGJ7BpzxEp92ZQYb6F8UPrk3z3/view

I don't think it makes sense to fix this for drivers. If gcc would do this wrong, we'd have noticed it ages ago.

But I think it makes sense to fix this in public headers.

Regards,

Hans

author
#6 lore raw

Re: [PATCH 1/2] media: v4l2-core: Shifting signed 32-bit value by 31 bits error

Quoted 55 lines · Hans Verkuil
Quoted 54 lines · Shuah Khan
Quoted 22 lines · Hans Verkuil
Quoted 21 lines · Randy Dunlap
Quoted 20 lines · Shuah Khan

Fix the following cppcheck error:

Checking drivers/media/v4l2-core/v4l2-ioctl.c ...
[drivers/media/v4l2-core/v4l2-ioctl.c:1370]: (error) Shifting signed 32-bit value by 31 bits is undefined behaviour

---
   drivers/media/v4l2-core/v4l2-ioctl.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-ioctl.c +1 -1
index 6859bdac86fe..333e387bafeb 100644
@@ -1364,7 +1364,7 @@ static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
   					(char)((fmt->pixelformat >> 8) & 0x7f),
   					(char)((fmt->pixelformat >> 16) & 0x7f),
   					(char)((fmt->pixelformat >> 24) & 0x7f),
-					(fmt->pixelformat & (1 << 31)) ? "-BE" : "");
+					(fmt->pixelformat & BIT(31)) ? "-BE" : "");
Quoted 8 lines
Quoted 8 lines

drivers/media/v4l2-core/v4l2-ioctl.c static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt) · line 1370

Quoted 4 lines
drivers/media/v4l2-core/v4l2-ioctl.c +0 -0
   			break;
   		}
   	}

If this builds, I guess #define BIT(x) got pulled in indirectly since bits.h nor bitops.h is currently #included in that source file.

It does build. You are right that I should have included bitops.h

Quoted 5 lines
Quoted 5 lines

Documentation/process/submit-checklist.rst rule #1 says:
1) If you use a facility then #include the file that defines/declares that facility. Don't depend on other header files pulling in ones that you use.

Quoted 8 lines
Quoted 2 lines

Please add #include <linux/bits or bitops.h>

I'm not sure about this patch. '1 << 31' is used all over in the kernel, including in public headers (e.g. media.h, videodev2.h).

It seems arbitrary to change it only here, but not anywhere else.

Right. We have several places in the kernel that do that.

Quoted 3 lines

In this particular example for the fourcc handling I would prefer to just use '1U << 31', both in v4l2-ioctl.c and videodev2.h.

Quoted 7 lines
Quoted 2 lines

If you would like to take the patch, I can send v2 fixing it using 1U << 31 - This is simpler since it doesn't nee additional includes.

I would like to have this cleaned up in the public media APIs. Those can be used by other compilers as well and it makes sense to me not to have undefined behavior in those headers.

Great. That is a good point. I will start looking at the public media APIs.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

A separate patch doing the same for MEDIA_ENT_ID_FLAG_NEXT in media.h would probably be a good idea either: that way the public API at least will do the right thing.

Sounds good.

Quoted 11 lines
Quoted 8 lines

I should have explained it better. I wanted to start with one or two places first to see if it is worth our time to fix these:

The full kernel cppcheck log for "Shifting signed 32-bit value by 31 bits is undefined behaviour" can be found at:

https://drive.google.com/file/d/19Xu7UqBGJ7BpzxEp92ZQYb6F8UPrk3z3/view

I don't think it makes sense to fix this for drivers. If gcc would do this wrong, we'd have noticed it ages ago.

Agreed. I am not concerned about it being incorrect. More for silencing cppcheck. I do agree that it isn't of a great value to us.

Quoted 3 lines

But I think it makes sense to fix this in public headers.

Yes. This would definitely help.

thanks,
-- Shuah

author
#7 lore raw

Re: [PATCH 1/2] media: v4l2-core: Shifting signed 32-bit value by 31 bits error

Quoted 69 lines · Shuah Khan
Quoted 56 lines · Hans Verkuil
Quoted 55 lines · Shuah Khan
Quoted 23 lines · Hans Verkuil
Quoted 22 lines · Randy Dunlap
Quoted 21 lines · Shuah Khan

Fix the following cppcheck error:

Checking drivers/media/v4l2-core/v4l2-ioctl.c ...
[drivers/media/v4l2-core/v4l2-ioctl.c:1370]: (error) Shifting signed 32-bit value by 31 bits is undefined behaviour

---
   drivers/media/v4l2-core/v4l2-ioctl.c | 2 +-

   1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-ioctl.c +1 -1
index 6859bdac86fe..333e387bafeb 100644
@@ -1364,7 +1364,7 @@ static void v4l_fill_fmtdesc(struct 
 v4l2_fmtdesc *fmt)
                        (char)((fmt->pixelformat >> 8) & 0x7f),
                        (char)((fmt->pixelformat >> 16) & 0x7f),
                        (char)((fmt->pixelformat >> 24) & 0x7f),
-                    (fmt->pixelformat & (1 << 31)) ? "-BE" : "");
+                    (fmt->pixelformat & BIT(31)) ? "-BE" : "");
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
drivers/media/v4l2-core/v4l2-ioctl.c +0 -0
                break;
            }

       }

If this builds, I guess #define BIT(x) got pulled in indirectly since bits.h nor bitops.h is currently #included in that source file.

It does build. You are right that I should have included bitops.h

Quoted 5 lines
Quoted 5 lines

Documentation/process/submit-checklist.rst rule #1 says:
1) If you use a facility then #include the file that defines/declares that facility.  Don't depend on other header files pulling in ones that you use.

Quoted 8 lines
Quoted 2 lines

Please add #include <linux/bits or bitops.h>

I'm not sure about this patch. '1 << 31' is used all over in the kernel, including in public headers (e.g. media.h, videodev2.h).

It seems arbitrary to change it only here, but not anywhere else.

Right. We have several places in the kernel that do that.

Quoted 3 lines

In this particular example for the fourcc handling I would prefer to just use '1U << 31', both in v4l2-ioctl.c and videodev2.h.

Quoted 7 lines
Quoted 2 lines

If you would like to take the patch, I can send v2 fixing it using 1U << 31 - This is simpler since it doesn't nee additional includes.

I would like to have this cleaned up in the public media APIs. Those can be used by other compilers as well and it makes sense to me not to have undefined behavior in those headers.

Great. That is a good point. I will start looking at the public media APIs.

Quoted 1 line
Quoted 1 line
Quoted 19 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

A separate patch doing the same for MEDIA_ENT_ID_FLAG_NEXT in media.h would probably be a good idea either: that way the public API at least will do the right thing.

Sounds good.

Quoted 11 lines
Quoted 8 lines

I should have explained it better. I wanted to start with one or two places first to see if it is worth our time to fix these:

The full kernel cppcheck log for "Shifting signed 32-bit value by 31 bits is undefined behaviour" can be found at:

https://drive.google.com/file/d/19Xu7UqBGJ7BpzxEp92ZQYb6F8UPrk3z3/view

I don't think it makes sense to fix this for drivers. If gcc would do this wrong, we'd have noticed it ages ago.

Did some research into this. We are fine with gcc, however looks like clang had the problem which has been fixed very recently in late 2018 in 6.0 release. This will be a problem even for kernel/drivers if older clang is used to build it.

I am sending the two header fixes (media.h, and videodev2.h) to start with.

thanks,
-- Shuah

author
#8 lore raw

PATCH 2/2 media: v4l2-core: fix uninitialized variable error

In reply to Shuah Khan

Fix the following cppcheck error:

Checking drivers/media/v4l2-core/v4l2-ioctl.c ...
Checking drivers/media/v4l2-core/v4l2-ioctl.c: CONFIG_VIDEO_ADV_DEBUG... [drivers/media/v4l2-core/v4l2-ioctl.c:2470]: (error) Uninitialized variable: sd

---
 drivers/media/v4l2-core/v4l2-ioctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-ioctl.c +1 -1
index 333e387bafeb..205addb949ce 100644
@@ -2445,7 +2445,7 @@ static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
 #ifdef CONFIG_VIDEO_ADV_DEBUG
 	struct video_device *vfd = video_devdata(file);
 	struct v4l2_dbg_chip_info *p = arg;
-	struct v4l2_subdev *sd;
+	struct v4l2_subdev *sd = NULL;
 	int idx = 0;
 
 	switch (p->match.type) {
Signature · 1 line
2.17.1
#9 lore raw

Re: [PATCH 2/2] media: v4l2-core: fix uninitialized variable error

Quoted 5 lines · Shuah Khan

Fix the following cppcheck error:

Checking drivers/media/v4l2-core/v4l2-ioctl.c ...
Checking drivers/media/v4l2-core/v4l2-ioctl.c: CONFIG_VIDEO_ADV_DEBUG... [drivers/media/v4l2-core/v4l2-ioctl.c:2470]: (error) Uninitialized variable: sd

This is a false report: sd is set by v4l2_device_for_each_subdev before it is used.

I'm dropping this patch.

Regards,

Hans

Quoted 20 lines
---
 drivers/media/v4l2-core/v4l2-ioctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-ioctl.c +1 -1
index 333e387bafeb..205addb949ce 100644
@@ -2445,7 +2445,7 @@ static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
 #ifdef CONFIG_VIDEO_ADV_DEBUG
 	struct video_device *vfd = video_devdata(file);
 	struct v4l2_dbg_chip_info *p = arg;
-	struct v4l2_subdev *sd;
+	struct v4l2_subdev *sd = NULL;
 	int idx = 0;
 
 	switch (p->match.type) {