0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /// Many iterators have the property that the first argument is always bound
0003 /// to a real list element, never NULL.
0004 //# False positives arise for some iterators that do not have this property,
0005 //# or in cases when the loop cursor is reassigned. The latter should only
0006 //# happen when the matched code is on the way to a loop exit (break, goto,
0007 //# or return).
0008 ///
0009 // Confidence: Moderate
0010 // Copyright: (C) 2010-2012 Nicolas Palix.
0011 // Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6.
0012 // Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6.
0013 // URL: https://coccinelle.gitlabpages.inria.fr/website
0014 // Comments:
0015 // Options: --no-includes --include-headers
0016
0017 virtual patch
0018 virtual context
0019 virtual org
0020 virtual report
0021
0022 @depends on patch@
0023 iterator I;
0024 expression x,E,E1,E2;
0025 statement S,S1,S2;
0026 @@
0027
0028 I(x,...) { <...
0029 (
0030 - if (x == NULL && ...) S
0031 |
0032 - if (x != NULL || ...)
0033 S
0034 |
0035 - (x == NULL) ||
0036 E
0037 |
0038 - (x != NULL) &&
0039 E
0040 |
0041 - (x == NULL && ...) ? E1 :
0042 E2
0043 |
0044 - (x != NULL || ...) ?
0045 E1
0046 - : E2
0047 |
0048 - if (x == NULL && ...) S1 else
0049 S2
0050 |
0051 - if (x != NULL || ...)
0052 S1
0053 - else S2
0054 |
0055 + BAD(
0056 x == NULL
0057 + )
0058 |
0059 + BAD(
0060 x != NULL
0061 + )
0062 )
0063 ...> }
0064
0065 @r depends on !patch exists@
0066 iterator I;
0067 expression x,E;
0068 position p1,p2;
0069 @@
0070
0071 *I@p1(x,...)
0072 { ... when != x = E
0073 (
0074 * x@p2 == NULL
0075 |
0076 * x@p2 != NULL
0077 )
0078 ... when any
0079 }
0080
0081 @script:python depends on org@
0082 p1 << r.p1;
0083 p2 << r.p2;
0084 @@
0085
0086 cocci.print_main("iterator-bound variable",p1)
0087 cocci.print_secs("useless NULL test",p2)
0088
0089 @script:python depends on report@
0090 p1 << r.p1;
0091 p2 << r.p2;
0092 @@
0093
0094 msg = "ERROR: iterator variable bound on line %s cannot be NULL" % (p1[0].line)
0095 coccilib.report.print_report(p2[0], msg)