0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /// PTR_ERR should access the value just tested by IS_ERR
0003 //# There can be false positives in the patch case, where it is the call to
0004 //# IS_ERR that is wrong.
0005 ///
0006 // Confidence: High
0007 // Copyright: (C) 2012, 2015 Julia Lawall, INRIA.
0008 // Copyright: (C) 2012, 2015 Gilles Muller, INRIA.
0009 // URL: https://coccinelle.gitlabpages.inria.fr/website
0010 // Options: --no-includes --include-headers
0011
0012 virtual patch
0013 virtual context
0014 virtual org
0015 virtual report
0016
0017 @ok1 exists@
0018 expression x,e;
0019 position p;
0020 @@
0021
0022 if (IS_ERR(x=e) || ...) {
0023 <...
0024 PTR_ERR@p(x)
0025 ...>
0026 }
0027
0028 @ok2 exists@
0029 expression x,e1,e2;
0030 position p;
0031 @@
0032
0033 if (IS_ERR(x) || ...) {
0034 <...
0035 (
0036 PTR_ERR@p(\(e1 ? e2 : x\|e1 ? x : e2\))
0037 |
0038 PTR_ERR@p(x)
0039 )
0040 ...>
0041 }
0042
0043 @r1 depends on patch && !context && !org && !report exists@
0044 expression x,y;
0045 position p != {ok1.p,ok2.p};
0046 @@
0047
0048 if (IS_ERR(x) || ...) {
0049 ... when any
0050 when != IS_ERR(...)
0051 (
0052 PTR_ERR(x)
0053 |
0054 PTR_ERR@p(
0055 - y
0056 + x
0057 )
0058 )
0059 ... when any
0060 }
0061
0062 // ----------------------------------------------------------------------------
0063
0064 @r1_context depends on !patch && (context || org || report) exists@
0065 position p != {ok1.p,ok2.p};
0066 expression x, y;
0067 position j0, j1;
0068 @@
0069
0070 if (IS_ERR@j0(x) || ...) {
0071 ... when any
0072 when != IS_ERR(...)
0073 (
0074 PTR_ERR(x)
0075 |
0076 PTR_ERR@j1@p(
0077 y
0078 )
0079 )
0080 ... when any
0081 }
0082
0083 @r1_disj depends on !patch && (context || org || report) exists@
0084 position p != {ok1.p,ok2.p};
0085 expression x, y;
0086 position r1_context.j0, r1_context.j1;
0087 @@
0088
0089 * if (IS_ERR@j0(x) || ...) {
0090 ... when any
0091 when != IS_ERR(...)
0092 * PTR_ERR@j1@p(
0093 y
0094 )
0095 ... when any
0096 }
0097
0098 // ----------------------------------------------------------------------------
0099
0100 @script:python r1_org depends on org@
0101 j0 << r1_context.j0;
0102 j1 << r1_context.j1;
0103 @@
0104
0105 msg = "inconsistent IS_ERR and PTR_ERR"
0106 coccilib.org.print_todo(j0[0], msg)
0107 coccilib.org.print_link(j1[0], "")
0108
0109 // ----------------------------------------------------------------------------
0110
0111 @script:python r1_report depends on report@
0112 j0 << r1_context.j0;
0113 j1 << r1_context.j1;
0114 @@
0115
0116 msg = "inconsistent IS_ERR and PTR_ERR on line %s." % (j1[0].line)
0117 coccilib.report.print_report(j0[0], msg)
0118