From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751996AbcAENt7 (ORCPT ); Tue, 5 Jan 2016 08:49:59 -0500 Received: from mailout2.w1.samsung.com ([210.118.77.12]:58753 "EHLO mailout2.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751543AbcAENt4 (ORCPT ); Tue, 5 Jan 2016 08:49:56 -0500 X-AuditID: cbfec7f4-f79026d00000418a-b7-568bca01102b Subject: Re: [PATCH v6] coccinelle: tests: unsigned value cannot be lesser than zero To: "Geyslan G. Bem" References: <1451893531-15817-1-git-send-email-a.hajda@samsung.com> Cc: Julia Lawall , Bartlomiej Zolnierkiewicz , Marek Szyprowski , Gilles Muller , Nicolas Palix , Michal Marek , open list , "moderated list:COCCINELLE/Semantic Patches (SmPL)" From: Andrzej Hajda Message-id: <568BC9F4.6020409@samsung.com> Date: Tue, 05 Jan 2016 14:49:40 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 MIME-version: 1.0 In-reply-to: Content-type: text/plain; charset=utf-8 Content-transfer-encoding: 7bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrNLMWRmVeSWpSXmKPExsVy+t/xq7qMp7rDDBqv6FtsnLGe1eLHptVs Fr1HjzFazP55icli2YPTjBaXd81hs1h75C67xZzWWWwWx14uZ3Lg9Ng56y67x7Fjrcwee7dk efRtWcXosX7LVRaPR8vus3h83iQXwB7FZZOSmpNZllqkb5fAlbH4wGXWgtnCFSsWzmBrYHzG 38XIwSEhYCLx7HRRFyMnkCkmceHeerYuRi4OIYGljBKtq7+wQDjPGSU2NS5jBqkSFgiTODe5 BcwWEdCQ2PqmmxHEFhI4ySjx9qsIiM0s8JNJ4tV8YRCbTUBT4u/mm2wgNq+AlkTv04WsIDaL gKrEu5YFTCBHiApESCzakQlRIijxY/I9FhCbUyBY4sD7G8wgJcwC6hJTpuRCTJeX2LzmLfME RoFZSDpmIVTNQlK1gJF5FaNoamlyQXFSeq6hXnFibnFpXrpecn7uJkZIFHzZwbj4mNUhRgEO RiUeXo6XXWFCrIllxZW5hxglOJiVRHhfH+kOE+JNSaysSi3Kjy8qzUktPsQozcGiJM47d9f7 ECGB9MSS1OzU1ILUIpgsEwenVAOjyOxViRIhhmVH9q5LkW/MXKA+o+3GlTPTHK9UCUx9wJL6 Z31xz6XtEtZ7d+Zf8+V/kfR7se1irWDGTqlTLFwdujYKde2ZSnwH39o+f6y0c+b5A+K3Nf0O Jks0P+cQ2eCj1Bcjl+JeM5XfoftJQ+7byx16+ywseHs+tHBdS/GPV/bJ+Xzx5QUlluKMREMt 5qLiRABmP7d+fgIAAA== Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 01/05/2016 01:59 PM, Geyslan G. Bem wrote: > Hello, > > 2016-01-04 4:45 GMT-03:00 Andrzej Hajda : >> 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 >> --- >> v6: improved range check detection (according to Julia suggestion) >> 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 | 76 ++++++++++++++++++++++ >> 1 file changed, 76 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..b9c7ed8 >> --- /dev/null >> +++ b/scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci >> @@ -0,0 +1,76 @@ >> +/// 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, > Why common unsigned comparisons with <= 0 are not being detected? I > think that it misleads the code reading and induces further bugs. > Just reading "var <= 0" infers that var can receive signed value. The > be clear the comparison should be against zero only "var == 0" or > depending of the context "!var". > Many developers prefer to use "unsigned <= 0" comparison, as more descriptive and less fragile. See for example for the last phrase of Linus email[1]. [1]: http://permalink.gmane.org/gmane.linux.kernel/2054063 Regards Andrzej