Coccinelle Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: a.hajda@samsung.com (Andrzej Hajda)
To: cocci@systeme.lip6.fr
Subject: [Cocci] [PATCH] coccinelle: assign signed result to unsigned variable
Date: Thu, 24 Sep 2015 14:54:46 +0200	[thread overview]
Message-ID: <1443099286-16559-1-git-send-email-a.hajda@samsung.com> (raw)

Assigning signed function result to unsigned variable can indicate error.
To decrease number of false positives patch looks if after assignment
there is also check for negative values of the result.

Signed-off-by: Andrzej Hajda <a.hajda@samsung.com>
---
Hi,

This patch tries to catch bugs related to losing possible negative function
results and complements my previous patch[1]. I have found about 20 real bugs
thanks to it. I will post related kernel patches on LKML.

[1]: http://permalink.gmane.org/gmane.linux.kernel/2039591
---
 .../tests/assign_signed_to_unsigned.cocci          | 48 ++++++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100644 scripts/coccinelle/tests/assign_signed_to_unsigned.cocci

diff --git a/scripts/coccinelle/tests/assign_signed_to_unsigned.cocci b/scripts/coccinelle/tests/assign_signed_to_unsigned.cocci
new file mode 100644
index 0000000..ebd3d3a
--- /dev/null
+++ b/scripts/coccinelle/tests/assign_signed_to_unsigned.cocci
@@ -0,0 +1,48 @@
+/// Assigning signed function result to unsigned variable can indicate error.
+/// To decrease number of false positives patch looks if after assignment
+/// there is also check for negative values of the result.
+///
+// Confidence: High
+// Copyright: (C) 2015 Andrzej Hajda, Samsung Electronics Co., Ltd. GPLv2.
+// URL: http://coccinelle.lip6.fr/
+// Options: --include-headers --all-includes
+
+virtual context
+virtual org
+virtual report
+
+ at rs@
+position p;
+typedef bool, u8, u16, u32, u64, s8, s16, s32, s64;
+{char, short int, int, long, long long, s8, s16, s32, s64} vs;
+{unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, size_t, bool, u8, u16, u32, u64} vu;
+@@
+
+vu at p = vs
+
+ at r@
+position rs.p;
+identifier v, f;
+statement S1, S2;
+expression e;
+@@
+
+*v at p = f(...);
+... when != v = e;
+if ( \( v < 0 \| v <= 0 \) ) S1 else S2
+
+ at script:python depends on r && org@
+p << rs.p;
+@@
+
+msg = "WARNING: Assigning signed result to unsigned variable: %s = %s(...)" % (v, f)
+coccilib.org.print_todo(p[0], msg)
+
+@script:python depends on r && report@
+p << rs.p;
+f << r.f;
+v << r.v;
+@@
+
+msg = "WARNING: Assigning signed result to unsigned variable: %s = %s(...)" % (v, f)
+coccilib.report.print_report(p[0], msg)
-- 
1.9.1

             reply	other threads:[~2015-09-24 12:54 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-24 12:54 Andrzej Hajda [this message]
2015-09-24 15:51 ` [Cocci] [PATCH] coccinelle: assign signed result to unsigned variable SF Markus Elfring
2015-09-25 10:08   ` Andrzej Hajda
2015-09-25 15:51     ` SF Markus Elfring
2015-09-26  7:45     ` SF Markus Elfring
2015-09-26  9:07       ` Julia Lawall
2015-09-26  9:41         ` SF Markus Elfring
2015-09-26  9:45           ` Julia Lawall
2015-09-26  9:52             ` SF Markus Elfring
2015-09-26  9:55               ` Julia Lawall
2015-09-26 11:43                 ` SF Markus Elfring
2015-09-26 13:55                   ` Julia Lawall
2015-09-26 15:22                     ` SF Markus Elfring
2015-09-26 15:30                       ` Julia Lawall
2015-09-26 15:50                         ` SF Markus Elfring
2015-09-26 15:55                           ` Julia Lawall
2015-09-26 16:01                             ` SF Markus Elfring
2015-09-28 10:54         ` [Cocci] [PATCH v2] " Andrzej Hajda
2015-09-28 11:32           ` Julia Lawall
2015-09-28 11:59             ` Andrzej Hajda
2015-09-30 21:51               ` Julia Lawall
2015-09-28 12:07           ` SF Markus Elfring
2015-09-28 12:12             ` Andrzej Hajda
2015-09-28 12:20               ` SF Markus Elfring
2015-09-28 12:42                 ` Julia Lawall
2015-09-28 12:55                   ` SF Markus Elfring
2015-09-28 13:13                     ` Julia Lawall
2015-09-28 13:53                       ` SF Markus Elfring
2015-09-28 15:07                         ` Julia Lawall
2015-10-03  7:09           ` Julia Lawall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1443099286-16559-1-git-send-email-a.hajda@samsung.com \
    --to=a.hajda@samsung.com \
    --cc=cocci@systeme.lip6.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox