All of lore.kernel.org
 help / color / mirror / Atom feed
From: a.hajda@samsung.com (Andrzej Hajda)
To: cocci@systeme.lip6.fr
Subject: [Cocci] [PATCH v6] coccinelle: tests: unsigned value cannot be lesser than zero
Date: Tue, 05 Jan 2016 14:49:40 +0100	[thread overview]
Message-ID: <568BC9F4.6020409@samsung.com> (raw)
In-Reply-To: <CAGG-pUSfBLeyktSP=u-rzjH+Q1LUcdKnh4H9ssTguL5WnKRkZw@mail.gmail.com>

On 01/05/2016 01:59 PM, Geyslan G. Bem wrote:
> Hello,
>
> 2016-01-04 4:45 GMT-03:00 Andrzej Hajda <a.hajda@samsung.com>:
>> 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 <a.hajda@samsung.com>
>> ---
>> 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

WARNING: multiple messages have this Message-ID (diff)
From: Andrzej Hajda <a.hajda@samsung.com>
To: "Geyslan G. Bem" <geyslan@gmail.com>
Cc: Julia Lawall <Julia.Lawall@lip6.fr>,
	Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>,
	Marek Szyprowski <m.szyprowski@samsung.com>,
	Gilles Muller <Gilles.Muller@lip6.fr>,
	Nicolas Palix <nicolas.palix@imag.fr>,
	Michal Marek <mmarek@suse.com>,
	open list <linux-kernel@vger.kernel.org>,
	"moderated list:COCCINELLE/Semantic Patches (SmPL)" 
	<cocci@systeme.lip6.fr>
Subject: Re: [PATCH v6] coccinelle: tests: unsigned value cannot be lesser than zero
Date: Tue, 05 Jan 2016 14:49:40 +0100	[thread overview]
Message-ID: <568BC9F4.6020409@samsung.com> (raw)
In-Reply-To: <CAGG-pUSfBLeyktSP=u-rzjH+Q1LUcdKnh4H9ssTguL5WnKRkZw@mail.gmail.com>

On 01/05/2016 01:59 PM, Geyslan G. Bem wrote:
> Hello,
>
> 2016-01-04 4:45 GMT-03:00 Andrzej Hajda <a.hajda@samsung.com>:
>> 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 <a.hajda@samsung.com>
>> ---
>> 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


  parent reply	other threads:[~2016-01-05 13:49 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-30 11:06 [Cocci] [PATCH v4] coccinelle: tests: unsigned value cannot be lesser than zero Andrzej Hajda
2015-12-30 11:06 ` Andrzej Hajda
2015-12-30 11:29 ` [Cocci] " Julia Lawall
2015-12-30 11:29   ` Julia Lawall
2015-12-30 12:33   ` [Cocci] " Andrzej Hajda
2015-12-30 12:33     ` Andrzej Hajda
2015-12-30 13:25     ` [Cocci] [PATCH v5] " Andrzej Hajda
2015-12-30 13:25       ` Andrzej Hajda
2015-12-30 14:06       ` [Cocci] " Julia Lawall
2015-12-30 14:06         ` Julia Lawall
2016-01-04  7:45         ` [Cocci] [PATCH v6] " Andrzej Hajda
2016-01-04  7:45           ` Andrzej Hajda
2016-01-05 12:59           ` [Cocci] " Geyslan G. Bem
2016-01-05 12:59             ` Geyslan G. Bem
2016-01-05 13:02             ` [Cocci] " Julia Lawall
2016-01-05 13:02               ` Julia Lawall
2016-01-05 14:10               ` [Cocci] " Geyslan G. Bem
2016-01-05 14:10                 ` Geyslan G. Bem
2016-01-05 16:48                 ` [Cocci] " Geyslan G. Bem
2016-01-05 16:48                   ` Geyslan G. Bem
2016-01-05 13:49             ` Andrzej Hajda [this message]
2016-01-05 13:49               ` Andrzej Hajda
2016-01-05 14:18               ` [Cocci] " Geyslan G. Bem
2016-01-05 14:18                 ` Geyslan G. Bem
2016-01-05 14:17           ` [Cocci] " Julia Lawall
2016-01-05 14:17             ` Julia Lawall
2016-01-05 14:29             ` [Cocci] " Andrzej Hajda
2016-01-05 14:29               ` Andrzej Hajda
2016-01-07  9:36             ` [Cocci] [PATCH v7] " Andrzej Hajda
2016-01-07  9:36               ` Andrzej Hajda
2016-01-07 11:35               ` [Cocci] " Julia Lawall
2016-01-07 11:35                 ` Julia Lawall
2016-01-07 14:37                 ` [Cocci] " Michal Marek
2016-01-07 14:37                   ` Michal Marek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=568BC9F4.6020409@samsung.com \
    --to=a.hajda@samsung.com \
    --cc=cocci@systeme.lip6.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.