All of lore.kernel.org
 help / color / mirror / Atom feed
* [Cocci] [PATCH] coccinelle: tests: unsigned value cannot be lesser than zero
@ 2015-09-15  9:27 ` Andrzej Hajda
  0 siblings, 0 replies; 69+ messages in thread
From: Andrzej Hajda @ 2015-09-15  9:27 UTC (permalink / raw)
  To: cocci

Code comparing unsigned variables with zero using operators < or >= does not
make sense. It is always false or true, respectively. However, its presence
often indicates bugs in the code.
gcc can detect it also using -Wtype-limits switch, but it warns also in correct
cases, making too much noise.

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

This test finds 93 issues in kernel code (with --all-includes) which could
be corrected. Some of them are harmless, just unnecessary code, but there
are also serious bugs, like:
	u32 irq = platform_get_irq(...)
	if (irq < 0)
		...
	unsigned int target = cpumask_any_but(cpu_online_mask, cpu);
	if (target < 0)
		...

Regards
Andrzej
---
 .../tests/unsigned_lesser_than_zero.cocci          | 37 ++++++++++++++++++++++
 1 file changed, 37 insertions(+)
 create mode 100644 scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci

diff --git a/scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci b/scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci
new file mode 100644
index 0000000..6a90510
--- /dev/null
+++ b/scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci
@@ -0,0 +1,37 @@
+/// Unsigned variables cannot be lesser than zero. Presence of such checks
+/// can indicate incorrect variable type or just unnecessary code.
+///
+// Confidence: High
+// Copyright: (C) 2015 Andrzej Hajda, Samsung Electronics Co., Ltd. GPLv2.
+// URL: http://coccinelle.lip6.fr/
+// Options: --include-headers
+
+virtual context
+virtual org
+virtual report
+
+ at r depends on context || org || report@
+position p;
+typedef u8, u16, u32, u64;
+{unsigned char, unsigned short int, unsigned int, unsigned long, unsigned long long, size_t, u8, u16, u32, u64} v;
+@@
+
+(
+*v at p < 0
+|
+*v@p >= 0
+)
+
+ at script:python depends on org@
+p << r.p;
+@@
+
+msg = "WARNING: Unsigned value cannot be lesser than zero"
+coccilib.org.print_todo(p[0], msg)
+
+@script:python depends on report@
+p << r.p;
+@@
+
+msg = "WARNING: Unsigned value cannot be lesser than zero"
+coccilib.report.print_report(p[0], msg)
-- 
1.9.1

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

end of thread, other threads:[~2015-09-23 15:17 UTC | newest]

Thread overview: 69+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-09-15  9:27 [Cocci] [PATCH] coccinelle: tests: unsigned value cannot be lesser than zero Andrzej Hajda
2015-09-15  9:27 ` Andrzej Hajda
2015-09-15 13:01 ` [Cocci] " SF Markus Elfring
2015-09-15 13:01   ` SF Markus Elfring
2015-09-15 13:01   ` SF Markus Elfring
2015-09-15 13:07   ` [Cocci] " Julia Lawall
2015-09-15 13:07     ` Julia Lawall
2015-09-15 13:07     ` Julia Lawall
2015-09-15 13:16     ` [Cocci] " SF Markus Elfring
2015-09-15 13:16       ` SF Markus Elfring
2015-09-15 13:16       ` SF Markus Elfring
2015-09-15 13:31       ` [Cocci] " Julia Lawall
2015-09-15 13:31         ` Julia Lawall
2015-09-15 13:31         ` Julia Lawall
2015-09-15 13:51         ` [Cocci] " Andrzej Hajda
2015-09-15 13:51           ` Andrzej Hajda
2015-09-15 13:51           ` Andrzej Hajda
2015-09-15 13:57           ` [Cocci] " Julia Lawall
2015-09-15 13:57             ` Julia Lawall
2015-09-15 13:57             ` Julia Lawall
2015-09-16  9:11             ` [Cocci] " Andrzej Hajda
2015-09-16  9:11               ` Andrzej Hajda
2015-09-16  9:11               ` Andrzej Hajda
2015-09-16  9:25               ` [Cocci] " Julia Lawall
2015-09-16  9:25                 ` Julia Lawall
2015-09-16  9:25                 ` Julia Lawall
2015-09-16 13:22                 ` [Cocci] [PATCH v2] " Andrzej Hajda
2015-09-16 13:22                   ` Andrzej Hajda
2015-09-16 13:33                   ` [Cocci] " Julia Lawall
2015-09-16 13:33                     ` Julia Lawall
2015-09-16 18:56                   ` [Cocci] " SF Markus Elfring
2015-09-16 18:56                     ` SF Markus Elfring
2015-09-16 18:56                     ` SF Markus Elfring
2015-09-15 13:42   ` [Cocci] [PATCH] " Andrzej Hajda
2015-09-15 13:42     ` Andrzej Hajda
2015-09-15 13:42     ` Andrzej Hajda
2015-09-15 14:36     ` [Cocci] " SF Markus Elfring
2015-09-15 14:36       ` SF Markus Elfring
2015-09-15 14:36       ` SF Markus Elfring
2015-09-15 14:43       ` [Cocci] " Julia Lawall
2015-09-15 14:43         ` Julia Lawall
2015-09-15 14:43         ` Julia Lawall
2015-09-15 14:53         ` SF Markus Elfring
2015-09-15 14:53           ` SF Markus Elfring
2015-09-15 14:53           ` SF Markus Elfring
2015-09-18  5:35     ` Julia Lawall
2015-09-18  5:35       ` Julia Lawall
2015-09-18  5:35       ` Julia Lawall
2015-09-21 10:37       ` [Cocci] [PATCH v3] " Andrzej Hajda
2015-09-21 10:37         ` Andrzej Hajda
2015-09-21 10:37         ` Andrzej Hajda
2015-09-21 13:02         ` [Cocci] " SF Markus Elfring
2015-09-21 13:02           ` SF Markus Elfring
2015-09-21 13:02           ` SF Markus Elfring
2015-09-21 13:34           ` [Cocci] " Andrzej Hajda
2015-09-21 13:34             ` Andrzej Hajda
2015-09-21 13:34             ` Andrzej Hajda
2015-09-21 14:06             ` [Cocci] " SF Markus Elfring
2015-09-21 14:06               ` SF Markus Elfring
2015-09-21 14:06               ` SF Markus Elfring
2015-09-22 15:27             ` [Cocci] " SF Markus Elfring
2015-09-22 15:27               ` SF Markus Elfring
2015-09-22 15:27               ` SF Markus Elfring
2015-09-23  7:34               ` [Cocci] " Andrzej Hajda
2015-09-23  7:34                 ` Andrzej Hajda
2015-09-23  7:34                 ` Andrzej Hajda
2015-09-23 15:17                 ` [Cocci] " SF Markus Elfring
2015-09-23 15:17                   ` SF Markus Elfring
2015-09-23 15:17                   ` SF Markus Elfring

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.