From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755279AbbIOJ1s (ORCPT ); Tue, 15 Sep 2015 05:27:48 -0400 Received: from mailout3.w1.samsung.com ([210.118.77.13]:43718 "EHLO mailout3.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753242AbbIOJ1q (ORCPT ); Tue, 15 Sep 2015 05:27:46 -0400 X-AuditID: cbfec7f5-f794b6d000001495-2d-55f7e4900b13 From: Andrzej Hajda To: Julia.Lawall@lip6.fr Cc: Andrzej Hajda , Gilles.Muller@lip6.fr, nicolas.palix@imag.fr, mmarek@suse.com, cocci@systeme.lip6.fr, joe@perches.com, linux-kernel@vger.kernel.org, b.zolnierkie@samsung.com Subject: [PATCH] coccinelle: tests: unsigned value cannot be lesser than zero Date: Tue, 15 Sep 2015 11:27:12 +0200 Message-id: <1442309232-5902-1-git-send-email-a.hajda@samsung.com> X-Mailer: git-send-email 1.9.1 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupnluLIzCtJLcpLzFFi42I5/e/4Fd0JT76HGny4oG5xa905VouNM9az WvzYtJrNYvbPS0wWs+8/ZrFY9uA0o8XlXXPYLOa0zmKzOPZyOZMDp8exY63MHnu3ZHl8WXWN 2aNvyypGj/VbrrJ4PFp2n8Xj8ya5APYoLpuU1JzMstQifbsEroy7p7tYCu4LVHxpEW1gfMzb xcjJISFgInFh+UxmCFtM4sK99WxdjFwcQgJLGSW6ZixnhnCamCQub/gGVsUmoCnxd/NNNhBb REBCYu2WnUwgRcwCpxglGhfOZgJJCAv4Ssx9dxOsgUVAVaLz+HkWEJtXwEliRt9ZqHVyEieP TWadwMi9gJFhFaNoamlyQXFSeq6RXnFibnFpXrpecn7uJkZIMH3dwbj0mNUhRgEORiUe3oj2 76FCrIllxZW5hxglOJiVRHhT7wGFeFMSK6tSi/Lji0pzUosPMUpzsCiJ887c9T5ESCA9sSQ1 OzW1ILUIJsvEwSnVwBhffYU9641R1K2nfK/Y/BSfN5nHbFQ/8Tgrcol8hlxX6WbH55Wv3Lx8 9zxaVMGW+TZn+ud1X2Y7h77L4F1Rszh28ff2hjPHJASmp/e9vbE+90nMOck5CwM6NRo+CS86 sbn7zKfD5zNmqnbZnLvuGj/x4/+0tZMXfvrEklDK+8DBw/4y89rKpElKLMUZiYZazEXFiQDQ VPgpIgIAAA== Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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