Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1224870

[PATCH] coccinelle: tests: unsigned value cannot be lesser than zero

From Andrzej Hajda <a.hajda@samsung.com>
Newsgroups linux.kernel
Subject [PATCH] coccinelle: tests: unsigned value cannot be lesser than zero
Date 2015-09-15 11:30 +0200
Message-ID <q8UIO-FE-5@gated-at.bofh.it> (permalink)
Organization linux.* mail to news gateway

Show all headers | View raw


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
+
+@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@p < 0
+|
+*v@p >= 0
+)
+
+@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

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | NextNext in thread | Find similar | Unroll thread


Thread

[PATCH] coccinelle: tests: unsigned value cannot be lesser than zero Andrzej Hajda <a.hajda@samsung.com> - 2015-09-15 11:30 +0200
  Re: [PATCH] coccinelle: tests: unsigned value cannot be lesser than  zero Julia Lawall <julia.lawall@lip6.fr> - 2015-09-15 15:10 +0200
    Re: [PATCH] coccinelle: tests: unsigned value cannot be lesser than  zero SF Markus Elfring <elfring@users.sourceforge.net> - 2015-09-15 15:20 +0200
      Re: [PATCH] coccinelle: tests: unsigned value cannot be lesser than  zero Julia Lawall <julia.lawall@lip6.fr> - 2015-09-15 15:40 +0200
        Re: [PATCH] coccinelle: tests: unsigned value cannot be lesser than  zero Andrzej Hajda <a.hajda@samsung.com> - 2015-09-15 16:00 +0200
          Re: [PATCH] coccinelle: tests: unsigned value cannot be lesser than  zero Julia Lawall <julia.lawall@lip6.fr> - 2015-09-15 16:00 +0200
            Re: [PATCH] coccinelle: tests: unsigned value cannot be lesser than  zero Andrzej Hajda <a.hajda@samsung.com> - 2015-09-16 11:20 +0200
              Re: [PATCH] coccinelle: tests: unsigned value cannot be lesser than  zero Julia Lawall <julia.lawall@lip6.fr> - 2015-09-16 11:30 +0200
                [PATCH v2] coccinelle: tests: unsigned value cannot be lesser than zero Andrzej Hajda <a.hajda@samsung.com> - 2015-09-16 15:30 +0200
                Re: [PATCH v2] coccinelle: tests: unsigned value cannot be lesser  than zero Julia Lawall <julia.lawall@lip6.fr> - 2015-09-16 15:40 +0200
                Re: [PATCH v2] coccinelle: tests: unsigned value cannot be lesser  than zero SF Markus Elfring <elfring@users.sourceforge.net> - 2015-09-16 21:00 +0200
  Re: [PATCH] coccinelle: tests: unsigned value cannot be lesser than  zero SF Markus Elfring <elfring@users.sourceforge.net> - 2015-09-15 15:10 +0200
    Re: [PATCH] coccinelle: tests: unsigned value cannot be lesser than  zero Andrzej Hajda <a.hajda@samsung.com> - 2015-09-15 15:50 +0200
      Re: [PATCH] coccinelle: tests: unsigned value cannot be lesser than  zero SF Markus Elfring <elfring@users.sourceforge.net> - 2015-09-15 16:40 +0200
        Re: [Cocci] [PATCH] coccinelle: tests: unsigned value cannot be  lesser than zero Julia Lawall <julia.lawall@lip6.fr> - 2015-09-15 16:50 +0200
          Re: [Cocci] [PATCH] coccinelle: tests: unsigned value cannot be  lesser than zero SF Markus Elfring <elfring@users.sourceforge.net> - 2015-09-15 17:00 +0200
      Re: [PATCH] coccinelle: tests: unsigned value cannot be lesser than  zero Julia Lawall <julia.lawall@lip6.fr> - 2015-09-18 07:40 +0200
        [PATCH v3] coccinelle: tests: unsigned value cannot be lesser than zero Andrzej Hajda <a.hajda@samsung.com> - 2015-09-21 12:40 +0200
          Re: [PATCH v3] coccinelle: tests: unsigned value cannot be lesser  than zero SF Markus Elfring <elfring@users.sourceforge.net> - 2015-09-21 15:10 +0200
            Re: [PATCH v3] coccinelle: tests: unsigned value cannot be lesser than  zero Andrzej Hajda <a.hajda@samsung.com> - 2015-09-21 15:40 +0200
              Re: [PATCH v3] coccinelle: tests: unsigned value cannot be lesser  than zero SF Markus Elfring <elfring@users.sourceforge.net> - 2015-09-21 16:10 +0200

csiph-web