Skip to contentSkip to conversation

staging: gpib: Add missing check for NULL

PATCH In review Kees Bakker 2 participants 4 messages Oct 20, 2024 – Oct 21, 2024 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: gpib: Add missing check for NULL

This was detected by Coverity, CID 1600787

---
 drivers/staging/gpib/common/gpib_os.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/staging/gpib/common/gpib_os.c +2 -0
index d5860a0a131f..57ff1ed30dac 100644
@@ -1667,6 +1667,8 @@ static int autospoll_ioctl(gpib_board_t *board, gpib_file_private_t *file_priv,
 		return -EFAULT;
 
 	desc = handle_to_descriptor(file_priv, 0); /* board handle is 0 */
+	if (!desc)
+		return -EINVAL;
 
 	if (enable) {
 		if (!desc->autopoll_enabled) {
Signature · 1 line
2.47.0
#1 lore raw
Quoted 3 lines · Kees Bakker

This was detected by Coverity, CID 1600787

This is how your patch looks like in lore.
https://lore.kernel.org/all/[email protected]/
The subject and the body of the email are far apart. It's like a book, right? The title of the book isn't the first sentence of the book. Feel free to restate the subject if necessary. The commit message should really say the impact of the bug, (how it looks like to the user) and you need a Fixes tag.

Quoted 9 lines
---
 drivers/staging/gpib/common/gpib_os.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/staging/gpib/common/gpib_os.c +0 -0
index d5860a0a131f..57ff1ed30dac 100644

drivers/staging/gpib/common/gpib_os.c static int autospoll_ioctl(gpib_board_t *board, gpib_file_private_t *file_priv, · line 1671

Quoted 5 lines
drivers/staging/gpib/common/gpib_os.c +2 -0
@@ -1667,6 +1667,8 @@ static int autospoll_ioctl(gpib_board_t *board, gpib_file_private_t *file_priv,
 		return -EFAULT;
 
 	desc = handle_to_descriptor(file_priv, 0); /* board handle is 0 */
+	if (!desc)
+		return -EINVAL;

There is always a board 0 so the check is unnecessary. This kind of heuristic tends to have a lot of false positives. It's better to just ignore static checkers when they are wrong.

regards,
dan carpenter

#2 lore raw

Op 21-10-2024 om 09:54 schreef Dan Carpenter:

Quoted 22 lines · Kees Bakker
Quoted 3 lines

This was detected by Coverity, CID 1600787

This is how your patch looks like in lore.
https://lore.kernel.org/all/[email protected]/
The subject and the body of the email are far apart. It's like a book, right? The title of the book isn't the first sentence of the book. Feel free to restate the subject if necessary. The commit message should really say the impact of the bug, (how it looks like to the user) and you need a Fixes tag.

Quoted 11 lines
---
  drivers/staging/gpib/common/gpib_os.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/staging/gpib/common/gpib_os.c +0 -0
index d5860a0a131f..57ff1ed30dac 100644
@@ -1667,6 +1667,8 @@ static int autospoll_ioctl(gpib_board_t *board, gpib_file_private_t *file_priv,
  		return -EFAULT;
 
Quoted 6 lines

drivers/staging/gpib/common/gpib_os.c static int autospoll_ioctl(gpib_board_t *board, gpib_file_private_t *file_priv, · line 1671

Quoted 3 lines
drivers/staging/gpib/common/gpib_os.c +2 -0
  	desc = handle_to_descriptor(file_priv, 0); /* board handle is 0 */
+	if (!desc)
+		return -EINVAL;

There is always a board 0 so the check is unnecessary. This kind of heuristic tends to have a lot of false positives. It's better to just ignore static checkers when they are wrong.

Let me go ahead and disagree with you there.

Notice that we already have the following code in ibclose() desc = handle_to_descriptor(priv, 0);
        if (desc) {
So my change isn't that alien in this module.

If we really hate to add that check, then there can be two alternatives
1. add a comment

2. change the code as follows:
    /* There is always a board 0 */
    desc = file_priv->descriptors[0];

Quoted 4 lines

regards,
dan carpenter

#3 lore raw

Actually I've reviewed it again, and I think that board zero is supposed to always be present, but that might not necessarily be the case. I'm not sure if the checking in close_dev_ioctl() is sufficient?

Personally, I don't have a problem with adding a NULL check, but you need to sell it better in the commit message. Just saying Coverity says we should add a NULL check is not sufficient. Anyone reviewing this code has to investigate, is Coverity right? How can we trigger this bug? If you did that investigation for us, then we would review your patches more generously.

When you review lots and lots of patches per day, then you develop rules to avoid wasting time. And one of those rules is "We don't do things just to make the checker happy." It's not a rule that I invented, it's the normal rule for almost everywhere in the kernel.

But if instead of a static checker fix, you sold the patch as a clean up then maybe that would be fine:

The NULL checking on handle_to_descriptor() is not consistent. The ibclose() function has a NULL check for this exact function call but it's not checked here.

It turns out that the zero board is always supposed to be present. It's allocated in ibopen() and freed in ibclose(). So, really, the NULL check should not be required. However, the error message in ibclose() suggests that desc could be NULL in the case of a bug. Let's just be cautious and add a NULL check here as well.

To be honest, I don't know if that commit message is good enough to get the patch past Greg but I might have been sold by something like that. At a minimum it shows that you are invested in the code and we tend of give people who care about the driver more freedom.

Quoted 3 lines

If we really hate to add that check, then there can be two alternatives
1. add a comment

Adding a comment is fine.

Quoted 3 lines

2. change the code as follows:
    /* There is always a board 0 */
    desc = file_priv->descriptors[0];

This is not fine. It's an example of making the code uglier to silence a static checker warning.

Btw, don't get too hung up on silencing every static checker warning. Kernel developers are good at fixing any real bugs so just look at how old the warning is and ignore the old ones. It's not like there is a shortage of real bugs to fix. For example, in open_dev_ioctl() if increment_open_device_count() fails then the file_priv->descriptors[i] is not cleaned up. It can't be used or freed. If ->is_board is false none of the file_priv->descriptors[] elements that come afterward can be freed either.

regards,
dan carpenter