162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/// Unsigned expressions cannot be lesser than zero. Presence of
362306a36Sopenharmony_ci/// comparisons 'unsigned (<|<=|>|>=) 0' often indicates a bug,
462306a36Sopenharmony_ci/// usually wrong type of variable.
562306a36Sopenharmony_ci///
662306a36Sopenharmony_ci/// To reduce number of false positives following tests have been added:
762306a36Sopenharmony_ci/// - parts of range checks are skipped, eg. "if (u < 0 || u > 15) ...",
862306a36Sopenharmony_ci///   developers prefer to keep such code,
962306a36Sopenharmony_ci/// - comparisons "<= 0" and "> 0" are performed only on results of
1062306a36Sopenharmony_ci///   signed functions/macros,
1162306a36Sopenharmony_ci/// - hardcoded list of signed functions/macros with always non-negative
1262306a36Sopenharmony_ci///   result is used to avoid false positives difficult to detect by other ways
1362306a36Sopenharmony_ci///
1462306a36Sopenharmony_ci// Confidence: Average
1562306a36Sopenharmony_ci// Copyright: (C) 2015 Andrzej Hajda, Samsung Electronics Co., Ltd.
1662306a36Sopenharmony_ci// URL: https://coccinelle.gitlabpages.inria.fr/website
1762306a36Sopenharmony_ci// Options: --all-includes
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_civirtual context
2062306a36Sopenharmony_civirtual org
2162306a36Sopenharmony_civirtual report
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci@r_cmp@
2462306a36Sopenharmony_ciposition p;
2562306a36Sopenharmony_citypedef bool, u8, u16, u32, u64;
2662306a36Sopenharmony_ci{unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long,
2762306a36Sopenharmony_ci	size_t, bool, u8, u16, u32, u64} v;
2862306a36Sopenharmony_ciexpression e;
2962306a36Sopenharmony_ci@@
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci	\( v = e \| &v \)
3262306a36Sopenharmony_ci	...
3362306a36Sopenharmony_ci	(\( v@p < 0 \| v@p <= 0 \| v@p >= 0 \| v@p > 0 \))
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci@r@
3662306a36Sopenharmony_ciposition r_cmp.p;
3762306a36Sopenharmony_citypedef s8, s16, s32, s64;
3862306a36Sopenharmony_ci{char, short, int, long, long long, ssize_t, s8, s16, s32, s64} vs;
3962306a36Sopenharmony_ciexpression c, e, v;
4062306a36Sopenharmony_ciidentifier 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_]+)$";
4162306a36Sopenharmony_ci@@
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci(
4462306a36Sopenharmony_ci	v = f(...)@vs;
4562306a36Sopenharmony_ci	... when != v = e;
4662306a36Sopenharmony_ci*	(\( v@p <=@e 0 \| v@p >@e 0 \))
4762306a36Sopenharmony_ci	... when any
4862306a36Sopenharmony_ci|
4962306a36Sopenharmony_ci(
5062306a36Sopenharmony_ci	(\( v@p < 0 \| v@p <= 0 \)) || ... || (\( v >= c \| v > c \))
5162306a36Sopenharmony_ci|
5262306a36Sopenharmony_ci	(\( v >= c \| v > c \)) || ... || (\( v@p < 0 \| v@p <= 0 \))
5362306a36Sopenharmony_ci|
5462306a36Sopenharmony_ci	(\( v@p >= 0 \| v@p > 0 \)) && ... && (\( v < c \| v <= c \))
5562306a36Sopenharmony_ci|
5662306a36Sopenharmony_ci	((\( v < c \| v <= c \) && ... && \( v@p >= 0 \| v@p > 0 \)))
5762306a36Sopenharmony_ci|
5862306a36Sopenharmony_ci*	(\( v@p <@e 0 \| v@p >=@e 0 \))
5962306a36Sopenharmony_ci)
6062306a36Sopenharmony_ci)
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci@script:python depends on org@
6362306a36Sopenharmony_cip << r_cmp.p;
6462306a36Sopenharmony_cie << r.e;
6562306a36Sopenharmony_ci@@
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_cimsg = "WARNING: Unsigned expression compared with zero: %s" % (e)
6862306a36Sopenharmony_cicoccilib.org.print_todo(p[0], msg)
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci@script:python depends on report@
7162306a36Sopenharmony_cip << r_cmp.p;
7262306a36Sopenharmony_cie << r.e;
7362306a36Sopenharmony_ci@@
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_cimsg = "WARNING: Unsigned expression compared with zero: %s" % (e)
7662306a36Sopenharmony_cicoccilib.report.print_report(p[0], msg)
77