aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/coccinelle/tests
diff options
context:
space:
mode:
authorWe-unite <3205135446@qq.com>2025-03-08 22:04:20 +0800
committerWe-unite <3205135446@qq.com>2025-03-08 22:04:20 +0800
commita07bb8fd1299070229f0e8f3dcb57ffd5ef9870a (patch)
tree84f21bd0bf7071bc5fc7dd989e77d7ceb5476682 /scripts/coccinelle/tests
downloadohosKernel-a07bb8fd1299070229f0e8f3dcb57ffd5ef9870a.tar.gz
ohosKernel-a07bb8fd1299070229f0e8f3dcb57ffd5ef9870a.zip
Initial commit: OpenHarmony-v4.0-ReleaseOpenHarmony-v4.0-Release
Diffstat (limited to 'scripts/coccinelle/tests')
-rw-r--r--scripts/coccinelle/tests/doublebitand.cocci55
-rw-r--r--scripts/coccinelle/tests/doubletest.cocci59
-rw-r--r--scripts/coccinelle/tests/odd_ptr_err.cocci118
-rw-r--r--scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci76
4 files changed, 308 insertions, 0 deletions
diff --git a/scripts/coccinelle/tests/doublebitand.cocci b/scripts/coccinelle/tests/doublebitand.cocci
new file mode 100644
index 000000000..0f0b94e7d
--- /dev/null
+++ b/scripts/coccinelle/tests/doublebitand.cocci
@@ -0,0 +1,55 @@
1// SPDX-License-Identifier: GPL-2.0-only
2/// Find bit operations that include the same argument more than once
3//# One source of false positives is when the argument performs a side
4//# effect. Another source of false positives is when a neutral value
5//# such as 0 for | is used to indicate no information, to maintain the
6//# same structure as other similar expressions
7///
8// Confidence: Moderate
9// Copyright: (C) 2010 Nicolas Palix, DIKU.
10// Copyright: (C) 2010 Julia Lawall, DIKU.
11// Copyright: (C) 2010 Gilles Muller, INRIA/LiP6.
12// URL: http://coccinelle.lip6.fr/
13// Comments:
14// Options: --no-includes --include-headers
15
16virtual context
17virtual org
18virtual report
19
20@r expression@
21expression E;
22position p;
23@@
24
25(
26* E@p
27 & ... & E
28|
29* E@p
30 | ... | E
31|
32* E@p
33 & ... & !E
34|
35* E@p
36 | ... | !E
37|
38* !E@p
39 & ... & E
40|
41* !E@p
42 | ... | E
43)
44
45@script:python depends on org@
46p << r.p;
47@@
48
49cocci.print_main("duplicated argument to & or |",p)
50
51@script:python depends on report@
52p << r.p;
53@@
54
55coccilib.report.print_report(p[0],"duplicated argument to & or |")
diff --git a/scripts/coccinelle/tests/doubletest.cocci b/scripts/coccinelle/tests/doubletest.cocci
new file mode 100644
index 000000000..b35519cdd
--- /dev/null
+++ b/scripts/coccinelle/tests/doubletest.cocci
@@ -0,0 +1,59 @@
1// SPDX-License-Identifier: GPL-2.0-only
2/// Find &&/|| operations that include the same argument more than once
3//# A common source of false positives is when the expression, or
4//# another expresssion in the same && or || operation, performs a
5//# side effect.
6///
7// Confidence: Moderate
8// Copyright: (C) 2010 Nicolas Palix, DIKU.
9// Copyright: (C) 2010 Julia Lawall, DIKU.
10// Copyright: (C) 2010 Gilles Muller, INRIA/LiP6.
11// URL: http://coccinelle.lip6.fr/
12// Comments:
13// Options: --no-includes --include-headers
14
15virtual context
16virtual org
17virtual report
18
19@r expression@
20expression E;
21position p;
22@@
23
24(
25 E@p || ... || E
26|
27 E@p && ... && E
28)
29
30@bad@
31expression r.E,e1,e2,fn;
32position r.p;
33assignment operator op;
34@@
35
36(
37E@p
38&
39 <+... \(fn(...)\|e1 op e2\|e1++\|e1--\|++e1\|--e1\) ...+>
40)
41
42@depends on context && !bad@
43expression r.E;
44position r.p;
45@@
46
47*E@p
48
49@script:python depends on org && !bad@
50p << r.p;
51@@
52
53cocci.print_main("duplicated argument to && or ||",p)
54
55@script:python depends on report && !bad@
56p << r.p;
57@@
58
59coccilib.report.print_report(p[0],"duplicated argument to && or ||")
diff --git a/scripts/coccinelle/tests/odd_ptr_err.cocci b/scripts/coccinelle/tests/odd_ptr_err.cocci
new file mode 100644
index 000000000..11d4e2b6d
--- /dev/null
+++ b/scripts/coccinelle/tests/odd_ptr_err.cocci
@@ -0,0 +1,118 @@
1// SPDX-License-Identifier: GPL-2.0-only
2/// PTR_ERR should access the value just tested by IS_ERR
3//# There can be false positives in the patch case, where it is the call to
4//# IS_ERR that is wrong.
5///
6// Confidence: High
7// Copyright: (C) 2012, 2015 Julia Lawall, INRIA.
8// Copyright: (C) 2012, 2015 Gilles Muller, INRIA.
9// URL: http://coccinelle.lip6.fr/
10// Options: --no-includes --include-headers
11
12virtual patch
13virtual context
14virtual org
15virtual report
16
17@ok1 exists@
18expression x,e;
19position p;
20@@
21
22if (IS_ERR(x=e) || ...) {
23 <...
24 PTR_ERR@p(x)
25 ...>
26}
27
28@ok2 exists@
29expression x,e1,e2;
30position p;
31@@
32
33if (IS_ERR(x) || ...) {
34 <...
35(
36 PTR_ERR@p(\(e1 ? e2 : x\|e1 ? x : e2\))
37|
38 PTR_ERR@p(x)
39)
40 ...>
41}
42
43@r1 depends on patch && !context && !org && !report exists@
44expression x,y;
45position p != {ok1.p,ok2.p};
46@@
47
48if (IS_ERR(x) || ...) {
49 ... when any
50 when != IS_ERR(...)
51(
52 PTR_ERR(x)
53|
54 PTR_ERR@p(
55- y
56+ x
57 )
58)
59 ... when any
60}
61
62// ----------------------------------------------------------------------------
63
64@r1_context depends on !patch && (context || org || report) exists@
65position p != {ok1.p,ok2.p};
66expression x, y;
67position j0, j1;
68@@
69
70if (IS_ERR@j0(x) || ...) {
71 ... when any
72 when != IS_ERR(...)
73(
74 PTR_ERR(x)
75|
76 PTR_ERR@j1@p(
77 y
78 )
79)
80 ... when any
81}
82
83@r1_disj depends on !patch && (context || org || report) exists@
84position p != {ok1.p,ok2.p};
85expression x, y;
86position r1_context.j0, r1_context.j1;
87@@
88
89* if (IS_ERR@j0(x) || ...) {
90 ... when any
91 when != IS_ERR(...)
92* PTR_ERR@j1@p(
93 y
94 )
95 ... when any
96}
97
98// ----------------------------------------------------------------------------
99
100@script:python r1_org depends on org@
101j0 << r1_context.j0;
102j1 << r1_context.j1;
103@@
104
105msg = "inconsistent IS_ERR and PTR_ERR"
106coccilib.org.print_todo(j0[0], msg)
107coccilib.org.print_link(j1[0], "")
108
109// ----------------------------------------------------------------------------
110
111@script:python r1_report depends on report@
112j0 << r1_context.j0;
113j1 << r1_context.j1;
114@@
115
116msg = "inconsistent IS_ERR and PTR_ERR on line %s." % (j1[0].line)
117coccilib.report.print_report(j0[0], msg)
118
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 000000000..91e286ace
--- /dev/null
+++ b/scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci
@@ -0,0 +1,76 @@
1// SPDX-License-Identifier: GPL-2.0-only
2/// Unsigned expressions cannot be lesser than zero. Presence of
3/// comparisons 'unsigned (<|<=|>|>=) 0' often indicates a bug,
4/// usually wrong type of variable.
5///
6/// To reduce number of false positives following tests have been added:
7/// - parts of range checks are skipped, eg. "if (u < 0 || u > 15) ...",
8/// developers prefer to keep such code,
9/// - comparisons "<= 0" and "> 0" are performed only on results of
10/// signed functions/macros,
11/// - hardcoded list of signed functions/macros with always non-negative
12/// result is used to avoid false positives difficult to detect by other ways
13///
14// Confidence: Average
15// Copyright: (C) 2015 Andrzej Hajda, Samsung Electronics Co., Ltd.
16// URL: http://coccinelle.lip6.fr/
17// Options: --all-includes
18
19virtual context
20virtual org
21virtual report
22
23@r_cmp@
24position p;
25typedef bool, u8, u16, u32, u64;
26{unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long,
27 size_t, bool, u8, u16, u32, u64} v;
28expression e;
29@@
30
31 \( v = e \| &v \)
32 ...
33 (\( v@p < 0 \| v@p <= 0 \| v@p >= 0 \| v@p > 0 \))
34
35@r@
36position r_cmp.p;
37typedef s8, s16, s32, s64;
38{char, short, int, long, long long, ssize_t, s8, s16, s32, s64} vs;
39expression c, e, v;
40identifier 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_]+)$";
41@@
42
43(
44 v = f(...)@vs;
45 ... when != v = e;
46* (\( v@p <=@e 0 \| v@p >@e 0 \))
47 ... when any
48|
49(
50 (\( v@p < 0 \| v@p <= 0 \)) || ... || (\( v >= c \| v > c \))
51|
52 (\( v >= c \| v > c \)) || ... || (\( v@p < 0 \| v@p <= 0 \))
53|
54 (\( v@p >= 0 \| v@p > 0 \)) && ... && (\( v < c \| v <= c \))
55|
56 ((\( v < c \| v <= c \) && ... && \( v@p >= 0 \| v@p > 0 \)))
57|
58* (\( v@p <@e 0 \| v@p >=@e 0 \))
59)
60)
61
62@script:python depends on org@
63p << r_cmp.p;
64e << r.e;
65@@
66
67msg = "WARNING: Unsigned expression compared with zero: %s" % (e)
68coccilib.org.print_todo(p[0], msg)
69
70@script:python depends on report@
71p << r_cmp.p;
72e << r.e;
73@@
74
75msg = "WARNING: Unsigned expression compared with zero: %s" % (e)
76coccilib.report.print_report(p[0], msg)