Skip to content

Commit 6f2c7ee

Browse files
himanshujha199640nashif
authored andcommitted
coccinelle: Add script to find cases of unsigned < 0
Unsigned expressions cannot be less than zero and presence of such practices very likely indicates a bug. Signed-off-by: Himanshu Jha <[email protected]>
1 parent 1df011c commit 6f2c7ee

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// Unsigned expressions cannot be lesser than zero. Presence of
2+
/// comparisons 'unsigned (<|<=) 0' often indicates a bug,
3+
/// usually wrong type of variable.
4+
///
5+
// Confidence: High
6+
// Copyright: (C) 2015 Andrzej Hajda, Samsung Electronics Co., Ltd. GPLv2.
7+
// URL: http://coccinelle.lip6.fr/
8+
9+
virtual org
10+
virtual report
11+
12+
@r_cmp depends on !(file in "ext")@
13+
position p;
14+
typedef u8_t, u16_t, u32_t, u64_t;
15+
{unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long,
16+
size_t, u8_t, u16_t, u32_t, u64_t} v;
17+
@@
18+
19+
(\( v@p < 0 \| v@p <= 0 \))
20+
21+
@script:python depends on org@
22+
p << r_cmp.p;
23+
@@
24+
25+
msg = "WARNING: Unsigned expression compared with zero."
26+
coccilib.org.print_todo(p[0], msg)
27+
28+
@script:python depends on report@
29+
p << r_cmp.p;
30+
@@
31+
32+
msg = "WARNING: Unsigned expression compared with zero."
33+
coccilib.report.print_report(p[0], msg)

0 commit comments

Comments
 (0)