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


Groups > linux.kernel > #1303497

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

From Julia Lawall <julia.lawall@lip6.fr>
Newsgroups linux.kernel
Subject Re: [PATCH v7] coccinelle: tests: unsigned value cannot be lesser than zero
Date 2016-01-07 12:40 +0100
Message-ID <qOh57-7sH-1@gated-at.bofh.it> (permalink)
References <qNACT-3VY-31@gated-at.bofh.it> <qOfd0-6ef-19@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Thu, 7 Jan 2016, Andrzej Hajda wrote:

> 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>

Acked-by: Julia Lawall <julia.lawall@lip6.fr>

Looks good, thanks!

julia

> ---
> 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          | 75 ++++++++++++++++++++++
>  1 file changed, 75 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..8fa5a3c
> --- /dev/null
> +++ b/scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci
> @@ -0,0 +1,75 @@
> +/// 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 = f(...)@vs;
> +	... when != v = e;
> +*	(\( v@p <=@e 0 \| v@p >@e 0 \))
> +	... when any
> +|
> +(
> +	(\( 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 \))
> +)
> +)
> +
> +@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)
> +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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH v6] coccinelle: tests: unsigned value cannot be lesser than zero Andrzej Hajda <a.hajda@samsung.com> - 2016-01-04 08:50 +0100
  Re: [PATCH v6] coccinelle: tests: unsigned value cannot be lesser  than zero "Geyslan G. Bem" <geyslan@gmail.com> - 2016-01-05 14:00 +0100
    Re: [PATCH v6] coccinelle: tests: unsigned value cannot be lesser  than zero Julia Lawall <julia.lawall@lip6.fr> - 2016-01-05 14:10 +0100
      Re: [PATCH v6] coccinelle: tests: unsigned value cannot be lesser  than zero "Geyslan G. Bem" <geyslan@gmail.com> - 2016-01-05 15:20 +0100
        Re: [PATCH v6] coccinelle: tests: unsigned value cannot be lesser  than zero "Geyslan G. Bem" <geyslan@gmail.com> - 2016-01-05 17:50 +0100
    Re: [PATCH v6] coccinelle: tests: unsigned value cannot be lesser than  zero Andrzej Hajda <a.hajda@samsung.com> - 2016-01-05 15:00 +0100
      Re: [PATCH v6] coccinelle: tests: unsigned value cannot be lesser  than zero "Geyslan G. Bem" <geyslan@gmail.com> - 2016-01-05 15:20 +0100
  Re: [PATCH v6] coccinelle: tests: unsigned value cannot be lesser  than zero Julia Lawall <julia.lawall@lip6.fr> - 2016-01-05 15:20 +0100
    Re: [PATCH v6] coccinelle: tests: unsigned value cannot be lesser than  zero Andrzej Hajda <a.hajda@samsung.com> - 2016-01-05 15:30 +0100
    [PATCH v7] coccinelle: tests: unsigned value cannot be lesser than zero Andrzej Hajda <a.hajda@samsung.com> - 2016-01-07 10:40 +0100
      Re: [PATCH v7] coccinelle: tests: unsigned value cannot be lesser  than zero Julia Lawall <julia.lawall@lip6.fr> - 2016-01-07 12:40 +0100
        Re: [PATCH v7] coccinelle: tests: unsigned value cannot be lesser  than zero Michal Marek <mmarek@suse.com> - 2016-01-07 15:40 +0100

csiph-web