Started adding some basic Coccinelle SmPL scripts
[netsniff-ng.git] / src / s / notnull.cocci
blob9c2a0edb3c72d6a997469db98c7cc4dcf919e7a5
1 // this detects NULL tests that can only be reached when the value is known 
2 // not to be NULL
3 //
4 // Confidence: High
5 // Copyright: (C) Gilles Muller, Julia Lawall, EMN, INRIA, DIKU.  GPLv2.
6 // URL: http://coccinelle.lip6.fr/rules/notnull.html
7 // Options:
9 @r exists@
10 local idexpression x;
11 expression E;
12 position p1,p2;
15 if (x@p1 == NULL || ...) { ... when forall
16    return ...; }
17 ... when != \(x=E\|x--\|x++\|--x\|++x\|x-=E\|x+=E\|x|=E\|x&=E\)
18     when != &x
20 x@p2 == NULL
22 x@p2 != NULL
25 // another path to the test that is not through p1?
27 @s exists@
28 local idexpression r.x;
29 position r.p1,r.p2;
32 ... when != x@p1
34 x@p2 == NULL
36 x@p2 != NULL
39 // another path to the test from p1?
41 @t exists@
42 local idexpression x;
43 position r.p1,r.p2;
46 if (x@p1 == NULL || ...) { ... x@p2 ... when any
47    return ...; }
49 // another path to the test containing an assignment?
51 @u exists@
52 local idexpression x;
53 expression E;
54 position r.p1,r.p2;
57 if (x@p1 == NULL || ...) { ... when forall
58    return ...; }
59  ...
60  \(x=E\|x--\|x++\|--x\|++x\|x-=E\|x+=E\|x|=E\|x&=E\|&x\)
61  ...  when != x@p1
62       when any
64 x@p2 == NULL
66 x@p2 != NULL
69 @fix depends on !s && !t && !u@
70 position r.p2;
71 expression x,E;
72 statement S1,S2;
76 - if ((x@p2 != NULL) || ...)
77   S1
79 - if ((x@p2 != NULL) || ...)
80   S1
81 - else S2
83 - (x@p2 != NULL) && E
84 + E
86 - (x@p2 == NULL) || E
87 + E
89 - if ((x@p2 == NULL) && ...) S1
91 - if ((x@p2 == NULL) && ...) S1 else
92   S2
94 - BUG_ON(x@p2 == NULL);
97 @script:python depends on !s && !t && !u && !fix@
98 p1 << r.p1;
99 p2 << r.p2;
102 cocci.print_main(p1)
103 cocci.print_secs("retest",p2)