checkpatch: add check for reuse of krealloc arg
authorJoe Perches <joe@perches.com>
Mon, 29 Apr 2013 23:18:12 +0000 (16:18 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Tue, 30 Apr 2013 01:28:19 +0000 (18:28 -0700)
On Thu, 2013-03-14 at 13:30 +0000, David Woodhouse wrote:
> If krealloc() returns NULL, it *doesn't* free the original. So any code
> of the form 'foo = krealloc(foo, …);' is almost certainly a bug.

So add a check for it to checkpatch.

Signed-off-by: Joe Perches <joe@perches.com>
Tested-by: Guenter Roeck <linux@roeck-us.net>
Acked-by: Guenter Roeck <linux@roeck-us.net>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Andy Whitcroft <apw@canonical.com>
Cc: Jean Delvare <khali@linux-fr.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
scripts/checkpatch.pl

index 4de4bc48493bca4947182bc10483bc272f459331..a820249a3fce0e2c7d488a27286926aa14779a7c 100755 (executable)
@@ -3477,6 +3477,13 @@ sub process {
                             "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
                }
 
+# 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);
+               }
+
 # check for alloc argument mismatch
                if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
                        WARN("ALLOC_ARRAY_ARGS",