xref: /linux/scripts/coccinelle/tests/odd_ptr_err.cocci (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
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: https://coccinelle.gitlabpages.inria.fr/website
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
119