From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755179AbbL3N0N (ORCPT ); Wed, 30 Dec 2015 08:26:13 -0500 Received: from mailout1.w1.samsung.com ([210.118.77.11]:64555 "EHLO mailout1.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755136AbbL3N0G (ORCPT ); Wed, 30 Dec 2015 08:26:06 -0500 X-AuditID: cbfec7f4-f79026d00000418a-e1-5683db6c5910 From: Andrzej Hajda To: Julia Lawall Cc: Andrzej Hajda , Bartlomiej Zolnierkiewicz , Marek Szyprowski , Gilles Muller , Nicolas Palix , Michal Marek , linux-kernel@vger.kernel.org (open list), cocci@systeme.lip6.fr (moderated list:COCCINELLE/Semantic Patches (SmPL)) Subject: [PATCH v5] coccinelle: tests: unsigned value cannot be lesser than zero Date: Wed, 30 Dec 2015 14:25:43 +0100 Message-id: <1451481943-20282-1-git-send-email-a.hajda@samsung.com> X-Mailer: git-send-email 1.9.1 In-reply-to: <5683CF1A.1030109@samsung.com> References: <5683CF1A.1030109@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrILMWRmVeSWpSXmKPExsVy+t/xq7o5t5vDDBbvEba4te4cq8XGGetZ LX5sWs1mMfvnJSaLZQ9OM1pc3jWHzWLtkbvsFnNaZ7FZHHu5nMmB0+PYsVZmj71bsjz6tqxi 9Fi/5SqLx6Nl91k8Pm+SC2CL4rJJSc3JLEst0rdL4MpYd3kbS8Fk6Yp9n0+xNTC+Ee1i5OSQ EDCROPC9mRnCFpO4cG89G4gtJLCUUaJ1WV4XIxeQ3cQk0XTiCBNIgk1AU+Lv5ptgRSIC6hKt G3YxghQxC3xnkpi14AY7SEJYIEBiQfMfMJtFQFVi8bUtrCA2r4CzxIN5V9ghtslJnDw2GSjO wcEpoC2xYlImxGItiXUPzrJPYORdwMiwilE0tTS5oDgpPddQrzgxt7g0L10vOT93EyMk4L7s YFx8zOoQowAHoxIPr4Joc5gQa2JZcWXuIUYJDmYlEd4Fm4BCvCmJlVWpRfnxRaU5qcWHGKU5 WJTEeefueh8iJJCeWJKanZpakFoEk2Xi4JRqYJQ6eEhUxPtb9r4Lz8w8M/WPnuix/ZrzxqVb I46N8ePaGVo+t5RvZakrsXRZGC18MH229BKf321JRt7rnY/c7l69/J53Tf3CKGGz1zqFocZT Z/7YJfeKT3HWrIWmARFX7NX2feZ6b8DdfMmHxeZwmiLHaQ6LJ98cUsorDSveLNj28HtI4OIs WyWW4oxEQy3mouJEAO0Vld40AgAA Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Unsigned expressions cannot be lesser than zero. Presence of comparisons 'unsigned (<|<=|>|>=) 0' often indicates a bug, usually wrong type of variable. The patch beside finding such comparisons tries to eliminate false positives, mainly by bypassing range checks. gcc can detect such comparisons also using -Wtype-limits switch, but it warns also in correct cases, making too much noise. Signed-off-by: Andrzej Hajda --- v5: improved range check detection v4: added range check detection, added full check in case value holds a result of signed function v3: added bool type v2: added --all-includes option --- .../tests/unsigned_lesser_than_zero.cocci | 77 ++++++++++++++++++++++ 1 file changed, 77 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..b843392 --- /dev/null +++ b/scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci @@ -0,0 +1,77 @@ +/// Unsigned expressions cannot be lesser than zero. Presence of +/// comparisons 'unsigned (<|<=|>|>=) 0' often indicates a bug, +/// usually wrong type of variable. +/// +/// To reduce number of false positives following tests have been added: +/// - parts of range checks are skipped, eg. "if (u < 0 || u > 15) ...", +/// developers prefer to keep such code, +/// - comparisons "<= 0" and "> 0" are performed only on results of +/// signed functions/macros, +/// - hardcoded list of signed functions/macros with always non-negative +/// result is used to avoid false positives difficult to detect by other ways +/// +// Confidence: Average +// Copyright: (C) 2015 Andrzej Hajda, Samsung Electronics Co., Ltd. GPLv2. +// URL: http://coccinelle.lip6.fr/ +// Options: --all-includes + +virtual context +virtual org +virtual report + +@r_cmp@ +position p; +typedef bool, u8, u16, u32, u64; +{unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, + size_t, bool, u8, u16, u32, u64} v; +expression e; +@@ + \( v = e \| &v \) + ... + (\( v@p < 0 \| v@p <= 0 \| v@p >= 0 \| v@p > 0 \)) + +@r@ +position r_cmp.p; +typedef s8, s16, s32, s64; +{char, short, int, long, long long, ssize_t, s8, s16, s32, s64} vs; +expression c, e, v; +identifier f !~ "^(ata_id_queue_depth|btrfs_copy_from_user|dma_map_sg|dma_map_sg_attrs|fls|fls64|gameport_time|get_write_extents|nla_len|ntoh24|of_flat_dt_match|of_get_child_count|uart_circ_chars_pending|[A-Z0-9_]+)$"; +@@ + +( + ... +( + (\( v@p < 0 \| v@p <= 0 \)) || ... || (\( v >= c \| v > c \)) +| + (\( v >= c \| v > c \)) || ... || (\( v@p < 0 \| v@p <= 0 \)) +| + (\( v@p >= 0 \| v@p > 0 \)) && ... && (\( v < c \| v <= c \)) +| + ((\( v < c \| v <= c \) && ... && \( v@p >= 0 \| v@p > 0 \))) +| +* (\( v@p <@e 0 \| v@p >=@e 0 \)) +) + ... +| + v = f(...)@vs; + ... when != v = e; +* (\( v@p <=@e 0 \| v@p >@e 0 \)) + ... +) + +@script:python depends on org@ +p << r_cmp.p; +e << r.e = ""; +@@ + +msg = "WARNING: Unsigned expression compared with zero: %s" % (e) +coccilib.org.print_todo(p[0], msg) + +@script:python depends on report@ +p << r_cmp.p; +e << r.e = ""; +@@ + +msg = "WARNING: Unsigned expression compared with zero: %s" % (e) +if e: + coccilib.report.print_report(p[0], msg) -- 1.9.1 From mboxrd@z Thu Jan 1 00:00:00 1970 From: a.hajda@samsung.com (Andrzej Hajda) Date: Wed, 30 Dec 2015 14:25:43 +0100 Subject: [Cocci] [PATCH v5] coccinelle: tests: unsigned value cannot be lesser than zero In-Reply-To: <5683CF1A.1030109@samsung.com> References: <5683CF1A.1030109@samsung.com> Message-ID: <1451481943-20282-1-git-send-email-a.hajda@samsung.com> To: cocci@systeme.lip6.fr List-Id: cocci@systeme.lip6.fr Unsigned expressions cannot be lesser than zero. Presence of comparisons 'unsigned (<|<=|>|>=) 0' often indicates a bug, usually wrong type of variable. The patch beside finding such comparisons tries to eliminate false positives, mainly by bypassing range checks. gcc can detect such comparisons also using -Wtype-limits switch, but it warns also in correct cases, making too much noise. Signed-off-by: Andrzej Hajda --- v5: improved range check detection v4: added range check detection, added full check in case value holds a result of signed function v3: added bool type v2: added --all-includes option --- .../tests/unsigned_lesser_than_zero.cocci | 77 ++++++++++++++++++++++ 1 file changed, 77 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..b843392 --- /dev/null +++ b/scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci @@ -0,0 +1,77 @@ +/// Unsigned expressions cannot be lesser than zero. Presence of +/// comparisons 'unsigned (<|<=|>|>=) 0' often indicates a bug, +/// usually wrong type of variable. +/// +/// To reduce number of false positives following tests have been added: +/// - parts of range checks are skipped, eg. "if (u < 0 || u > 15) ...", +/// developers prefer to keep such code, +/// - comparisons "<= 0" and "> 0" are performed only on results of +/// signed functions/macros, +/// - hardcoded list of signed functions/macros with always non-negative +/// result is used to avoid false positives difficult to detect by other ways +/// +// Confidence: Average +// Copyright: (C) 2015 Andrzej Hajda, Samsung Electronics Co., Ltd. GPLv2. +// URL: http://coccinelle.lip6.fr/ +// Options: --all-includes + +virtual context +virtual org +virtual report + + at r_cmp@ +position p; +typedef bool, u8, u16, u32, u64; +{unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, + size_t, bool, u8, u16, u32, u64} v; +expression e; +@@ + \( v = e \| &v \) + ... + (\( v at p < 0 \| v at p <= 0 \| v@p >= 0 \| v at p > 0 \)) + + at r@ +position r_cmp.p; +typedef s8, s16, s32, s64; +{char, short, int, long, long long, ssize_t, s8, s16, s32, s64} vs; +expression c, e, v; +identifier f !~ "^(ata_id_queue_depth|btrfs_copy_from_user|dma_map_sg|dma_map_sg_attrs|fls|fls64|gameport_time|get_write_extents|nla_len|ntoh24|of_flat_dt_match|of_get_child_count|uart_circ_chars_pending|[A-Z0-9_]+)$"; +@@ + +( + ... +( + (\( v at p < 0 \| v@p <= 0 \)) || ... || (\( v >= c \| v > c \)) +| + (\( v >= c \| v > c \)) || ... || (\( v at p < 0 \| v at p <= 0 \)) +| + (\( v@p >= 0 \| v at p > 0 \)) && ... && (\( v < c \| v <= c \)) +| + ((\( v < c \| v <= c \) && ... && \( v@p >= 0 \| v at p > 0 \))) +| +* (\( v at p <@e 0 \| v@p >=@e 0 \)) +) + ... +| + v = f(...)@vs; + ... when != v = e; +* (\( v at p <=@e 0 \| v@p >@e 0 \)) + ... +) + + at script:python depends on org@ +p << r_cmp.p; +e << r.e = ""; +@@ + +msg = "WARNING: Unsigned expression compared with zero: %s" % (e) +coccilib.org.print_todo(p[0], msg) + +@script:python depends on report@ +p << r_cmp.p; +e << r.e = ""; +@@ + +msg = "WARNING: Unsigned expression compared with zero: %s" % (e) +if e: + coccilib.report.print_report(p[0], msg) -- 1.9.1