linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] coccinelle: Add patch to use kcalloc instead of kzalloc for array allocations
@ 2011-11-25 14:46 Lars-Peter Clausen
  2011-11-29 18:42 ` Thomas Meyer
  0 siblings, 1 reply; 3+ messages in thread
From: Lars-Peter Clausen @ 2011-11-25 14:46 UTC (permalink / raw)
  To: Julia Lawall, Gilles Muller, Nicolas Palix
  Cc: cocci, linux-kernel, Lars-Peter Clausen

This patch adds a coccinelle patch to convert array allocations which use
kzalloc to kcalloc. The advantage of kcalloc is, that will take care of
integer overflows which could result from the multiplication and it is also
nicer to read.

The coccinelle patch does not convert array allocations using kmalloc, since
kcalloc will zero the allocated memory which is considered too much overhead
in some situations.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
---
 scripts/coccinelle/api/alloc/kcalloc.cocci |   59 ++++++++++++++++++++++++++++
 1 files changed, 59 insertions(+), 0 deletions(-)
 create mode 100644 scripts/coccinelle/api/alloc/kcalloc.cocci

diff --git a/scripts/coccinelle/api/alloc/kcalloc.cocci b/scripts/coccinelle/api/alloc/kcalloc.cocci
new file mode 100644
index 0000000..b2a5893
--- /dev/null
+++ b/scripts/coccinelle/api/alloc/kcalloc.cocci
@@ -0,0 +1,59 @@
+///
+/// Use kcalloc instead of kzalloc to allocate array.
+/// The advantage of kcalloc is, that will prevent integer overflows which could
+/// result from the multiplication of number of elements and size and it is also
+/// a bit nicer to read.
+///
+// Confidence: High
+// Options: -no_includes -include_headers
+//
+// Keywords: kzalloc, kcalloc
+// Version min: < 2.6.12 kcalloc
+// Version min:   2.6.14 kzalloc
+//
+
+virtual context
+virtual patch
+virtual org
+virtual report
+
+//----------------------------------------------------------
+//  For patch mode
+//----------------------------------------------------------
+
+@depends on patch@
+expression E;
+expression E2;
+expression gfp;
+expression x;
+@@
+-x = kzalloc(sizeof(E2) * (E), gfp);
++x = kcalloc(E, sizeof(E2), gfp);
+
+//----------------------------------------------------------
+//  For org and report mode
+//----------------------------------------------------------
+
+@r depends on org || report@
+expression E;
+expression E2;
+expression gfp;
+position p;
+expression x;
+@@
+x = kzalloc@p(sizeof(E2) * (E), gfp);
+
+@script:python depends on org@
+p << r.p;
+x << r.x;
+@@
+
+coccilib.org.print_safe_todo(p[0], "%s" % x)
+
+@script:python depends on report@
+p << r.p;
+x << r.x;
+@@
+
+msg="WARNING: kcalloc should be used to allocate an array instead of kzalloc"
+coccilib.report.print_report(p[0], msg)
-- 
1.7.7.1



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

* Re: [PATCH] coccinelle: Add patch to use kcalloc instead of kzalloc for array allocations
  2011-11-25 14:46 [PATCH] coccinelle: Add patch to use kcalloc instead of kzalloc for array allocations Lars-Peter Clausen
@ 2011-11-29 18:42 ` Thomas Meyer
  2011-11-29 19:42   ` Lars-Peter Clausen
  0 siblings, 1 reply; 3+ messages in thread
From: Thomas Meyer @ 2011-11-29 18:42 UTC (permalink / raw)
  To: Lars-Peter Clausen
  Cc: Julia Lawall, Gilles Muller, Nicolas Palix, cocci, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 450 bytes --]

Am Freitag, den 25.11.2011, 15:46 +0100 schrieb Lars-Peter Clausen:
> This patch adds a coccinelle patch to convert array allocations which use
> kzalloc to kcalloc. The advantage of kcalloc is, that will take care of
> integer overflows which could result from the multiplication and it is also
> nicer to read.

Hi Lars,

I did run your kcalloc spatch against 3.2-rc3. are you okay with if I
send the diffs to the LKML?

mfg
thohmas


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

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

* Re: [PATCH] coccinelle: Add patch to use kcalloc instead of kzalloc for array allocations
  2011-11-29 18:42 ` Thomas Meyer
@ 2011-11-29 19:42   ` Lars-Peter Clausen
  0 siblings, 0 replies; 3+ messages in thread
From: Lars-Peter Clausen @ 2011-11-29 19:42 UTC (permalink / raw)
  To: Thomas Meyer
  Cc: Julia Lawall, Gilles Muller, Nicolas Palix, cocci, linux-kernel

On 11/29/2011 07:42 PM, Thomas Meyer wrote:
> Am Freitag, den 25.11.2011, 15:46 +0100 schrieb Lars-Peter Clausen:
>> This patch adds a coccinelle patch to convert array allocations which use
>> kzalloc to kcalloc. The advantage of kcalloc is, that will take care of
>> integer overflows which could result from the multiplication and it is also
>> nicer to read.
> 
> Hi Lars,
> 
> I did run your kcalloc spatch against 3.2-rc3. are you okay with if I
> send the diffs to the LKML?
> 

Sure.

Thanks
- Lars

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

end of thread, other threads:[~2011-11-29 19:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-11-25 14:46 [PATCH] coccinelle: Add patch to use kcalloc instead of kzalloc for array allocations Lars-Peter Clausen
2011-11-29 18:42 ` Thomas Meyer
2011-11-29 19:42   ` Lars-Peter Clausen

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).