linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: [iio:togreg 3/3] WARNING: Reusing the krealloc arg is almost always a bug
       [not found] ` <20140302022404.GA16141@localhost>
@ 2014-03-02  9:11   ` Lars-Peter Clausen
  2014-03-02 12:06     ` Joe Perches
  0 siblings, 1 reply; 3+ messages in thread
From: Lars-Peter Clausen @ 2014-03-02  9:11 UTC (permalink / raw)
  To: Fengguang Wu; +Cc: Jonathan Cameron, Joe Perches, linux-kernel

On 03/02/2014 03:24 AM, Fengguang Wu wrote:
>
> Hi Lars-Peter,
>
> FYI, there are new warnings show up in
>
> tree:   git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
> head:   bdc8cda1d010887c06bd8c29564b74cd61ec0a7b
> commit: bdc8cda1d010887c06bd8c29564b74cd61ec0a7b [3/3] iio:adc: Add Xilinx XADC driver
>
> scripts/checkpatch.pl 0001-iio-adc-Add-Xilinx-XADC-driver.patch
> # many are suggestions rather than must-fix
> WARNING: Reusing the krealloc arg is almost always a bug
> #1220: drivers/iio/adc/xilinx-xadc-core.c:1138:
> +	indio_dev->channels = krealloc(channels, sizeof(*channels) *
>

This is actually a false positive, checkpatch thinks that 
'indio_dev->channels' and 'channels' is the same. Added Joe to Cc.

- Lars

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

* Re: [iio:togreg 3/3] WARNING: Reusing the krealloc arg is almost always a bug
  2014-03-02  9:11   ` [iio:togreg 3/3] WARNING: Reusing the krealloc arg is almost always a bug Lars-Peter Clausen
@ 2014-03-02 12:06     ` Joe Perches
  2014-03-02 13:35       ` Joe Perches
  0 siblings, 1 reply; 3+ messages in thread
From: Joe Perches @ 2014-03-02 12:06 UTC (permalink / raw)
  To: Lars-Peter Clausen, Andy Whitcroft
  Cc: Fengguang Wu, Jonathan Cameron, linux-kernel

(Adding Andy Whitcroft to cc's)

On Sun, 2014-03-02 at 10:11 +0100, Lars-Peter Clausen wrote:
> On 03/02/2014 03:24 AM, Fengguang Wu wrote:
> >
> > Hi Lars-Peter,
> >
> > FYI, there are new warnings show up in
> >
> > tree:   git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
> > head:   bdc8cda1d010887c06bd8c29564b74cd61ec0a7b
> > commit: bdc8cda1d010887c06bd8c29564b74cd61ec0a7b [3/3] iio:adc: Add Xilinx XADC driver
> >
> > scripts/checkpatch.pl 0001-iio-adc-Add-Xilinx-XADC-driver.patch
> > # many are suggestions rather than must-fix
> > WARNING: Reusing the krealloc arg is almost always a bug
> > #1220: drivers/iio/adc/xilinx-xadc-core.c:1138:
> > +	indio_dev->channels = krealloc(channels, sizeof(*channels) *
> >
> 
> This is actually a false positive, checkpatch thinks that 
> 'indio_dev->channels' and 'channels' is the same. Added Joe to Cc.

Yeah.  perl finds an $Lval match for either
foo->bar and bar for

	foo->bar = krealloc(bar, 1, GFP_KERNEL);
	bar = krealloc(bar, 1, GFP_KERNEL);

I can't think of a "pretty" perl way to fix this
because I'm not much of a perl guy, but maybe this
patch below works well enough.

Find all the lvals when there's a possible krealloc
reuse.  Check the lvals before and after the krealloc.
Bleat if they not equal.

Andy?  Got any better idea?

---
 scripts/checkpatch.pl | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 91308be..7391e01 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4275,9 +4275,15 @@ sub process {
 
 # check for krealloc arg reuse
 		if ($^V && $^V ge 5.10.0 &&
-		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
-			WARN("KREALLOC_ARG_REUSE",
-			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
+		    $line =~ /($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
+			my @line_lvals = $line =~ m/$Lval/g;
+			for my $index (1 .. $#line_lvals - 1) {
+				if ($line_lvals[$index] eq "krealloc" &&
+				    $line_lvals[$index - 1] eq $line_lvals[$index + 1]) {
+					WARN("KREALLOC_ARG_REUSE",
+					     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
+				}
+			}
 		}
 
 # check for alloc argument mismatch



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

* Re: [iio:togreg 3/3] WARNING: Reusing the krealloc arg is almost always a bug
  2014-03-02 12:06     ` Joe Perches
@ 2014-03-02 13:35       ` Joe Perches
  0 siblings, 0 replies; 3+ messages in thread
From: Joe Perches @ 2014-03-02 13:35 UTC (permalink / raw)
  To: Lars-Peter Clausen
  Cc: Andy Whitcroft, Fengguang Wu, Jonathan Cameron, linux-kernel

That last one doesn't work if the krealloc has a cast like
	foo = (struct bar *)krealloc(foo, 1, GFP_KERNEL)
Maybe this one is better...
---
 scripts/checkpatch.pl | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 91308be..43914c3 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4275,9 +4275,20 @@ sub process {
 
 # check for krealloc arg reuse
 		if ($^V && $^V ge 5.10.0 &&
-		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
-			WARN("KREALLOC_ARG_REUSE",
-			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
+		    $line =~ /($Lval)\s*\=\s*($balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
+			my $cast = $2;
+			my $cast_count = 0;
+			my @line_lvals = $line =~ m/$Lval/g;
+			if (defined $cast) {
+				$cast_count++ while ($cast =~ /$Lval/g);
+			}
+			for my $index (1 + $cast_count .. $#line_lvals - 1) {
+				if ($line_lvals[$index] eq "krealloc" &&
+				    $line_lvals[$index - 1 - $cast_count] eq $line_lvals[$index + 1]) {
+					WARN("KREALLOC_ARG_REUSE",
+					     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
+				}
+			}
 		}
 
 # check for alloc argument mismatch



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

end of thread, other threads:[~2014-03-02 13:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <53124ca5.kw+3caZ7enyrhSmq%fengguang.wu@intel.com>
     [not found] ` <20140302022404.GA16141@localhost>
2014-03-02  9:11   ` [iio:togreg 3/3] WARNING: Reusing the krealloc arg is almost always a bug Lars-Peter Clausen
2014-03-02 12:06     ` Joe Perches
2014-03-02 13:35       ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).