All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] checkpatch: add check for wrong "if (IS_ERR_OR_NULL(<foo>)) return PTR_ERR(<foo>)" uses
@ 2013-05-14  0:41 Ruslan Bilovol
  2013-05-14  4:36 ` Joe Perches
  0 siblings, 1 reply; 3+ messages in thread
From: Ruslan Bilovol @ 2013-05-14  0:41 UTC (permalink / raw)
  To: apw, joe; +Cc: linux-kernel

This was many times discussed by Russell King
and I also was about wrong usage of IS_ERR_OR_NULL()
in my patch. So I added this check and other people will
be at least warned about potentially wrong usage
of mentioned macro.

Quoting Russell:
| Well, the whole reasoning here is that IS_ERR_OR_NULL() is far too easy
| to get wrong - there are too many of this kind of crap in the kernel:
|
| 	foo = some_func();
| 	if (IS_ERR_OR_NULL(foo))
| 		return PTR_ERR(foo);
|
| which is wrong, because if foo _is_ NULL, the function doesn't return an
| error, it returns success instead.  Of course, if some_func() never ever
| returns NULL in the first place, that can't happen, but then the additional
| test there for a NULL pointer is, to put it bluntly, total bollocks.

The full discussion may be found here:
http://permalink.gmane.org/gmane.linux.ports.arm.omap/97874

Tested on:
+	if (IS_ERR_OR_NULL(soc_dev))
+		return PTR_ERR(soc_dev);
+
+	if (IS_ERR_OR_NULL(soc_dev))
+		return soc_dev ? PTR_ERR(soc_dev) : -ENODEV;

The result is:

WARNING: Use of IS_ERR_OR_NULL is usually wrong: see http://permalink.gmane.org/gmane.linux.ports.arm.omap/97874
+	if (IS_ERR_OR_NULL(soc_dev))
+		return PTR_ERR(soc_dev);

Signed-off-by: Ruslan Bilovol <ruslan.bilovol@ti.com>
---

I'm not very experienced in the perl, so feel free to
comment this patch or fix it :)

 scripts/checkpatch.pl |    9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index b954de5..6ec71bd 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3262,6 +3262,15 @@ sub process {
 			}
 		}
 
+# check for wrong "if (IS_ERR_OR_NULL(<foo>)) return PTR_ERR(<foo>)" uses
+		if ($prevline =~ /\bif\s*\(\s*IS_ERR_OR_NULL\s*\(\s*($Lval)\s*\)\s*\)/) {
+			my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
+			if ($line =~ /\b(\s*PTR_ERR)$expr/) {
+				WARN('IS_ERR_OR_NULL',
+				     "Use of IS_ERR_OR_NULL is usually wrong: see http://permalink.gmane.org/gmane.linux.ports.arm.omap/97874\n" . $hereprev);
+			}
+		}
+
 # prefer usleep_range over udelay
 		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
 			# ignore udelay's < 10, however
-- 
1.7.9.5


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] checkpatch: add check for wrong "if (IS_ERR_OR_NULL(<foo>)) return PTR_ERR(<foo>)" uses
  2013-05-14  0:41 [PATCH] checkpatch: add check for wrong "if (IS_ERR_OR_NULL(<foo>)) return PTR_ERR(<foo>)" uses Ruslan Bilovol
@ 2013-05-14  4:36 ` Joe Perches
  2013-05-23 13:10   ` Ruslan Bilovol
  0 siblings, 1 reply; 3+ messages in thread
From: Joe Perches @ 2013-05-14  4:36 UTC (permalink / raw)
  To: Ruslan Bilovol; +Cc: apw, linux-kernel

On Tue, 2013-05-14 at 03:41 +0300, Ruslan Bilovol wrote:
> This was many times discussed by Russell King
> and I also was about wrong usage of IS_ERR_OR_NULL()
> in my patch. So I added this check and other people will
> be at least warned about potentially wrong usage
> of mentioned macro.
[]
> Tested on:
> +	if (IS_ERR_OR_NULL(soc_dev))
> +		return PTR_ERR(soc_dev);
> +
> +	if (IS_ERR_OR_NULL(soc_dev))en
> +		return soc_dev ? PTR_ERR(soc_dev) : -ENODEV;

I did a grep for IS_ERR_OR_NULL and this misses variants like:

		if (unlikely(IS_ERR_OR_NULL(etc...

Perhaps something like this?
---
 scripts/checkpatch.pl | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index b954de5..18c7d8c 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -2184,6 +2184,15 @@ sub process {
 				WARN("SUSPECT_CODE_INDENT",
 				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
 			}
+
+# check for misuses of IS_ERR_OR_NULL
+			if ($stat =~ /^\+.*\bIS_ERR_OR_NULL\s*\(\s*($Lval)\s*\)/) {
+				my $test = $1;
+				if ($stat =~ /\bPTR_ERR\s*\(\s*${test}\s*\)/) {
+					WARN("IS_ERR_OR_NULL",
+					     "Use of IS_ERR_OR_NULL($test) with PTR_ERR($test) is usually wrong: see http://permalink.gmane.org/gmane.linux.ports.arm.omap/97874\n" . $herecurr . "$stat_real\n");
+				}
+			}
 		}
 
 		# Track the 'values' across context and added lines.



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] checkpatch: add check for wrong "if (IS_ERR_OR_NULL(<foo>)) return PTR_ERR(<foo>)" uses
  2013-05-14  4:36 ` Joe Perches
@ 2013-05-23 13:10   ` Ruslan Bilovol
  0 siblings, 0 replies; 3+ messages in thread
From: Ruslan Bilovol @ 2013-05-23 13:10 UTC (permalink / raw)
  To: Joe Perches; +Cc: apw, linux-kernel

Hi Joe,

Thank you for looking into this.

On Tue, May 14, 2013 at 7:36 AM, Joe Perches <joe@perches.com> wrote:
> On Tue, 2013-05-14 at 03:41 +0300, Ruslan Bilovol wrote:
>> This was many times discussed by Russell King
>> and I also was about wrong usage of IS_ERR_OR_NULL()
>> in my patch. So I added this check and other people will
>> be at least warned about potentially wrong usage
>> of mentioned macro.
> []
>> Tested on:
>> +     if (IS_ERR_OR_NULL(soc_dev))
>> +             return PTR_ERR(soc_dev);
>> +
>> +     if (IS_ERR_OR_NULL(soc_dev))en
>> +             return soc_dev ? PTR_ERR(soc_dev) : -ENODEV;
>
> I did a grep for IS_ERR_OR_NULL and this misses variants like:
>
>                 if (unlikely(IS_ERR_OR_NULL(etc...

Agree, I checked and you are right

>
> Perhaps something like this?

hmm.. your variant have false positives.
I used next 2 examples for verification:

This one - is incorrect usage of IS_ERR_OR_NULL
+     if (IS_ERR_OR_NULL(soc_dev))
+             return PTR_ERR(soc_dev);

But this one is correct and checkpach shouldn't react here but in your
version does react
+     if (IS_ERR_OR_NULL(soc_dev))
+             return soc_dev ? PTR_ERR(soc_dev) : -ENODEV;

Care to fix it?

Best regards,
Ruslan

> ---
>  scripts/checkpatch.pl | 9 +++++++++
>  1 file changed, 9 insertions(+)
>
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index b954de5..18c7d8c 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -2184,6 +2184,15 @@ sub process {
>                                 WARN("SUSPECT_CODE_INDENT",
>                                      "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
>                         }
> +
> +# check for misuses of IS_ERR_OR_NULL
> +                       if ($stat =~ /^\+.*\bIS_ERR_OR_NULL\s*\(\s*($Lval)\s*\)/) {
> +                               my $test = $1;
> +                               if ($stat =~ /\bPTR_ERR\s*\(\s*${test}\s*\)/) {
> +                                       WARN("IS_ERR_OR_NULL",
> +                                            "Use of IS_ERR_OR_NULL($test) with PTR_ERR($test) is usually wrong: see http://permalink.gmane.org/gmane.linux.ports.arm.omap/97874\n" . $herecurr . "$stat_real\n");
> +                               }
> +                       }
>                 }
>
>                 # Track the 'values' across context and added lines.
>
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-05-23 13:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-05-14  0:41 [PATCH] checkpatch: add check for wrong "if (IS_ERR_OR_NULL(<foo>)) return PTR_ERR(<foo>)" uses Ruslan Bilovol
2013-05-14  4:36 ` Joe Perches
2013-05-23 13:10   ` Ruslan Bilovol

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.