diff options
author | 2025-03-08 22:04:20 +0800 | |
---|---|---|
committer | 2025-03-08 22:04:20 +0800 | |
commit | a07bb8fd1299070229f0e8f3dcb57ffd5ef9870a (patch) | |
tree | 84f21bd0bf7071bc5fc7dd989e77d7ceb5476682 /scripts/coccinelle/tests | |
download | ohosKernel-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.cocci | 55 | ||||
-rw-r--r-- | scripts/coccinelle/tests/doubletest.cocci | 59 | ||||
-rw-r--r-- | scripts/coccinelle/tests/odd_ptr_err.cocci | 118 | ||||
-rw-r--r-- | scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci | 76 |
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 | |||
16 | virtual context | ||
17 | virtual org | ||
18 | virtual report | ||
19 | |||
20 | @r expression@ | ||
21 | expression E; | ||
22 | position 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@ | ||
46 | p << r.p; | ||
47 | @@ | ||
48 | |||
49 | cocci.print_main("duplicated argument to & or |",p) | ||
50 | |||
51 | @script:python depends on report@ | ||
52 | p << r.p; | ||
53 | @@ | ||
54 | |||
55 | coccilib.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 | |||
15 | virtual context | ||
16 | virtual org | ||
17 | virtual report | ||
18 | |||
19 | @r expression@ | ||
20 | expression E; | ||
21 | position p; | ||
22 | @@ | ||
23 | |||
24 | ( | ||
25 | E@p || ... || E | ||
26 | | | ||
27 | E@p && ... && E | ||
28 | ) | ||
29 | |||
30 | @bad@ | ||
31 | expression r.E,e1,e2,fn; | ||
32 | position r.p; | ||
33 | assignment operator op; | ||
34 | @@ | ||
35 | |||
36 | ( | ||
37 | E@p | ||
38 | & | ||
39 | <+... \(fn(...)\|e1 op e2\|e1++\|e1--\|++e1\|--e1\) ...+> | ||
40 | ) | ||
41 | |||
42 | @depends on context && !bad@ | ||
43 | expression r.E; | ||
44 | position r.p; | ||
45 | @@ | ||
46 | |||
47 | *E@p | ||
48 | |||
49 | @script:python depends on org && !bad@ | ||
50 | p << r.p; | ||
51 | @@ | ||
52 | |||
53 | cocci.print_main("duplicated argument to && or ||",p) | ||
54 | |||
55 | @script:python depends on report && !bad@ | ||
56 | p << r.p; | ||
57 | @@ | ||
58 | |||
59 | coccilib.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 | |||
12 | virtual patch | ||
13 | virtual context | ||
14 | virtual org | ||
15 | virtual report | ||
16 | |||
17 | @ok1 exists@ | ||
18 | expression x,e; | ||
19 | position p; | ||
20 | @@ | ||
21 | |||
22 | if (IS_ERR(x=e) || ...) { | ||
23 | <... | ||
24 | PTR_ERR@p(x) | ||
25 | ...> | ||
26 | } | ||
27 | |||
28 | @ok2 exists@ | ||
29 | expression x,e1,e2; | ||
30 | position p; | ||
31 | @@ | ||
32 | |||
33 | if (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@ | ||
44 | expression x,y; | ||
45 | position p != {ok1.p,ok2.p}; | ||
46 | @@ | ||
47 | |||
48 | if (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@ | ||
65 | position p != {ok1.p,ok2.p}; | ||
66 | expression x, y; | ||
67 | position j0, j1; | ||
68 | @@ | ||
69 | |||
70 | if (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@ | ||
84 | position p != {ok1.p,ok2.p}; | ||
85 | expression x, y; | ||
86 | position 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@ | ||
101 | j0 << r1_context.j0; | ||
102 | j1 << r1_context.j1; | ||
103 | @@ | ||
104 | |||
105 | msg = "inconsistent IS_ERR and PTR_ERR" | ||
106 | coccilib.org.print_todo(j0[0], msg) | ||
107 | coccilib.org.print_link(j1[0], "") | ||
108 | |||
109 | // ---------------------------------------------------------------------------- | ||
110 | |||
111 | @script:python r1_report depends on report@ | ||
112 | j0 << r1_context.j0; | ||
113 | j1 << r1_context.j1; | ||
114 | @@ | ||
115 | |||
116 | msg = "inconsistent IS_ERR and PTR_ERR on line %s." % (j1[0].line) | ||
117 | coccilib.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 | |||
19 | virtual context | ||
20 | virtual org | ||
21 | virtual report | ||
22 | |||
23 | @r_cmp@ | ||
24 | position p; | ||
25 | typedef 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; | ||
28 | expression e; | ||
29 | @@ | ||
30 | |||
31 | \( v = e \| &v \) | ||
32 | ... | ||
33 | (\( v@p < 0 \| v@p <= 0 \| v@p >= 0 \| v@p > 0 \)) | ||
34 | |||
35 | @r@ | ||
36 | position r_cmp.p; | ||
37 | typedef s8, s16, s32, s64; | ||
38 | {char, short, int, long, long long, ssize_t, s8, s16, s32, s64} vs; | ||
39 | expression c, e, v; | ||
40 | 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_]+)$"; | ||
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@ | ||
63 | p << r_cmp.p; | ||
64 | e << r.e; | ||
65 | @@ | ||
66 | |||
67 | msg = "WARNING: Unsigned expression compared with zero: %s" % (e) | ||
68 | coccilib.org.print_todo(p[0], msg) | ||
69 | |||
70 | @script:python depends on report@ | ||
71 | p << r_cmp.p; | ||
72 | e << r.e; | ||
73 | @@ | ||
74 | |||
75 | msg = "WARNING: Unsigned expression compared with zero: %s" % (e) | ||
76 | coccilib.report.print_report(p[0], msg) | ||