xref: /openbmc/linux/scripts/coccinelle/null/badzero.cocci (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
17f904d7eSThomas Gleixner// SPDX-License-Identifier: GPL-2.0-only
247f67ea9SJulia Lawall/// Compare pointer-typed values to NULL rather than 0
347f67ea9SJulia Lawall///
447f67ea9SJulia Lawall//# This makes an effort to choose between !x and x == NULL.  !x is used
547f67ea9SJulia Lawall//# if it has previously been used with the function used to initialize x.
647f67ea9SJulia Lawall//# This relies on type information.  More type information can be obtained
747f67ea9SJulia Lawall//# using the option -all_includes and the option -I to specify an
847f67ea9SJulia Lawall//# include path.
947f67ea9SJulia Lawall//
1047f67ea9SJulia Lawall// Confidence: High
117f904d7eSThomas Gleixner// Copyright: (C) 2012 Julia Lawall, INRIA/LIP6.
127f904d7eSThomas Gleixner// Copyright: (C) 2012 Gilles Muller, INRIA/LiP6.
13*f01701ceSJulia Lawall// URL: https://coccinelle.gitlabpages.inria.fr/website
14e0be348eSMasahiro Yamada// Requires: 1.0.0
1547f67ea9SJulia Lawall// Options:
1647f67ea9SJulia Lawall
1747f67ea9SJulia Lawallvirtual patch
1847f67ea9SJulia Lawallvirtual context
1947f67ea9SJulia Lawallvirtual org
2047f67ea9SJulia Lawallvirtual report
2147f67ea9SJulia Lawall
2247f67ea9SJulia Lawall@initialize:ocaml@
23b5889ab7SHimangi Saraogi@@
2447f67ea9SJulia Lawalllet negtable = Hashtbl.create 101
2547f67ea9SJulia Lawall
2647f67ea9SJulia Lawall@depends on patch@
2747f67ea9SJulia Lawallexpression *E;
2847f67ea9SJulia Lawallidentifier f;
2947f67ea9SJulia Lawall@@
3047f67ea9SJulia Lawall
3147f67ea9SJulia Lawall(
3247f67ea9SJulia Lawall  (E = f(...)) ==
3347f67ea9SJulia Lawall- 0
3447f67ea9SJulia Lawall+ NULL
3547f67ea9SJulia Lawall|
3647f67ea9SJulia Lawall  (E = f(...)) !=
3747f67ea9SJulia Lawall- 0
3847f67ea9SJulia Lawall+ NULL
3947f67ea9SJulia Lawall|
4047f67ea9SJulia Lawall- 0
4147f67ea9SJulia Lawall+ NULL
4247f67ea9SJulia Lawall  == (E = f(...))
4347f67ea9SJulia Lawall|
4447f67ea9SJulia Lawall- 0
4547f67ea9SJulia Lawall+ NULL
4647f67ea9SJulia Lawall  != (E = f(...))
4747f67ea9SJulia Lawall)
4847f67ea9SJulia Lawall
4947f67ea9SJulia Lawall
5047f67ea9SJulia Lawall@t1 depends on !patch@
5147f67ea9SJulia Lawallexpression *E;
5247f67ea9SJulia Lawallidentifier f;
5347f67ea9SJulia Lawallposition p;
5447f67ea9SJulia Lawall@@
5547f67ea9SJulia Lawall
5647f67ea9SJulia Lawall(
5747f67ea9SJulia Lawall  (E = f(...)) ==
5847f67ea9SJulia Lawall* 0@p
5947f67ea9SJulia Lawall|
6047f67ea9SJulia Lawall  (E = f(...)) !=
6147f67ea9SJulia Lawall* 0@p
6247f67ea9SJulia Lawall|
6347f67ea9SJulia Lawall* 0@p
6447f67ea9SJulia Lawall  == (E = f(...))
6547f67ea9SJulia Lawall|
6647f67ea9SJulia Lawall* 0@p
6747f67ea9SJulia Lawall  != (E = f(...))
6847f67ea9SJulia Lawall)
6947f67ea9SJulia Lawall
7047f67ea9SJulia Lawall@script:python depends on org@
7147f67ea9SJulia Lawallp << t1.p;
7247f67ea9SJulia Lawall@@
7347f67ea9SJulia Lawall
7447f67ea9SJulia Lawallcoccilib.org.print_todo(p[0], "WARNING comparing pointer to 0")
7547f67ea9SJulia Lawall
7647f67ea9SJulia Lawall@script:python depends on report@
7747f67ea9SJulia Lawallp << t1.p;
7847f67ea9SJulia Lawall@@
7947f67ea9SJulia Lawall
8047f67ea9SJulia Lawallcoccilib.report.print_report(p[0], "WARNING comparing pointer to 0")
8147f67ea9SJulia Lawall
8247f67ea9SJulia Lawall// Tests of returned values
8347f67ea9SJulia Lawall
8447f67ea9SJulia Lawall@s@
8547f67ea9SJulia Lawallidentifier f;
8647f67ea9SJulia Lawallexpression E,E1;
8747f67ea9SJulia Lawall@@
8847f67ea9SJulia Lawall
8947f67ea9SJulia Lawall E = f(...)
9047f67ea9SJulia Lawall ... when != E = E1
9147f67ea9SJulia Lawall !E
9247f67ea9SJulia Lawall
9347f67ea9SJulia Lawall@script:ocaml depends on s@
9447f67ea9SJulia Lawallf << s.f;
9547f67ea9SJulia Lawall@@
9647f67ea9SJulia Lawall
9747f67ea9SJulia Lawalltry let _ = Hashtbl.find negtable f in ()
9847f67ea9SJulia Lawallwith Not_found -> Hashtbl.add negtable f ()
9947f67ea9SJulia Lawall
10047f67ea9SJulia Lawall@ r disable is_zero,isnt_zero exists @
10147f67ea9SJulia Lawallexpression *E;
10247f67ea9SJulia Lawallidentifier f;
10347f67ea9SJulia Lawall@@
10447f67ea9SJulia Lawall
10547f67ea9SJulia LawallE = f(...)
10647f67ea9SJulia Lawall...
10747f67ea9SJulia Lawall(E == 0
10847f67ea9SJulia Lawall|E != 0
10947f67ea9SJulia Lawall|0 == E
11047f67ea9SJulia Lawall|0 != E
11147f67ea9SJulia Lawall)
11247f67ea9SJulia Lawall
11347f67ea9SJulia Lawall@script:ocaml@
11447f67ea9SJulia Lawallf << r.f;
11547f67ea9SJulia Lawall@@
11647f67ea9SJulia Lawall
11747f67ea9SJulia Lawalltry let _ = Hashtbl.find negtable f in ()
11847f67ea9SJulia Lawallwith Not_found -> include_match false
11947f67ea9SJulia Lawall
12047f67ea9SJulia Lawall// This rule may lead to inconsistent path problems, if E is defined in two
12147f67ea9SJulia Lawall// places
12247f67ea9SJulia Lawall@ depends on patch disable is_zero,isnt_zero @
12347f67ea9SJulia Lawallexpression *E;
12447f67ea9SJulia Lawallexpression E1;
12547f67ea9SJulia Lawallidentifier r.f;
12647f67ea9SJulia Lawall@@
12747f67ea9SJulia Lawall
12847f67ea9SJulia LawallE = f(...)
12947f67ea9SJulia Lawall<...
13047f67ea9SJulia Lawall(
13147f67ea9SJulia Lawall- E == 0
13247f67ea9SJulia Lawall+ !E
13347f67ea9SJulia Lawall|
13447f67ea9SJulia Lawall- E != 0
13547f67ea9SJulia Lawall+ E
13647f67ea9SJulia Lawall|
13747f67ea9SJulia Lawall- 0 == E
13847f67ea9SJulia Lawall+ !E
13947f67ea9SJulia Lawall|
14047f67ea9SJulia Lawall- 0 != E
14147f67ea9SJulia Lawall+ E
14247f67ea9SJulia Lawall)
14347f67ea9SJulia Lawall...>
14447f67ea9SJulia Lawall?E = E1
14547f67ea9SJulia Lawall
14647f67ea9SJulia Lawall@t2 depends on !patch disable is_zero,isnt_zero @
14747f67ea9SJulia Lawallexpression *E;
14847f67ea9SJulia Lawallexpression E1;
14947f67ea9SJulia Lawallidentifier r.f;
15047f67ea9SJulia Lawallposition p1;
15147f67ea9SJulia Lawallposition p2;
15247f67ea9SJulia Lawall@@
15347f67ea9SJulia Lawall
15447f67ea9SJulia LawallE = f(...)
15547f67ea9SJulia Lawall<...
15647f67ea9SJulia Lawall(
15747f67ea9SJulia Lawall* E == 0@p1
15847f67ea9SJulia Lawall|
15947f67ea9SJulia Lawall* E != 0@p2
16047f67ea9SJulia Lawall|
16147f67ea9SJulia Lawall* 0@p1 == E
16247f67ea9SJulia Lawall|
16347f67ea9SJulia Lawall* 0@p1 != E
16447f67ea9SJulia Lawall)
16547f67ea9SJulia Lawall...>
16647f67ea9SJulia Lawall?E = E1
16747f67ea9SJulia Lawall
16847f67ea9SJulia Lawall@script:python depends on org@
16947f67ea9SJulia Lawallp << t2.p1;
17047f67ea9SJulia Lawall@@
17147f67ea9SJulia Lawall
17247f67ea9SJulia Lawallcoccilib.org.print_todo(p[0], "WARNING comparing pointer to 0, suggest !E")
17347f67ea9SJulia Lawall
17447f67ea9SJulia Lawall@script:python depends on org@
17547f67ea9SJulia Lawallp << t2.p2;
17647f67ea9SJulia Lawall@@
17747f67ea9SJulia Lawall
17847f67ea9SJulia Lawallcoccilib.org.print_todo(p[0], "WARNING comparing pointer to 0")
17947f67ea9SJulia Lawall
18047f67ea9SJulia Lawall@script:python depends on report@
18147f67ea9SJulia Lawallp << t2.p1;
18247f67ea9SJulia Lawall@@
18347f67ea9SJulia Lawall
18447f67ea9SJulia Lawallcoccilib.report.print_report(p[0], "WARNING comparing pointer to 0, suggest !E")
18547f67ea9SJulia Lawall
18647f67ea9SJulia Lawall@script:python depends on report@
18747f67ea9SJulia Lawallp << t2.p2;
18847f67ea9SJulia Lawall@@
18947f67ea9SJulia Lawall
19047f67ea9SJulia Lawallcoccilib.report.print_report(p[0], "WARNING comparing pointer to 0")
19147f67ea9SJulia Lawall
19247f67ea9SJulia Lawall@ depends on patch disable is_zero,isnt_zero @
19347f67ea9SJulia Lawallexpression *E;
19447f67ea9SJulia Lawall@@
19547f67ea9SJulia Lawall
19647f67ea9SJulia Lawall(
19747f67ea9SJulia Lawall  E ==
19847f67ea9SJulia Lawall- 0
19947f67ea9SJulia Lawall+ NULL
20047f67ea9SJulia Lawall|
20147f67ea9SJulia Lawall  E !=
20247f67ea9SJulia Lawall- 0
20347f67ea9SJulia Lawall+ NULL
20447f67ea9SJulia Lawall|
20547f67ea9SJulia Lawall- 0
20647f67ea9SJulia Lawall+ NULL
20747f67ea9SJulia Lawall  == E
20847f67ea9SJulia Lawall|
20947f67ea9SJulia Lawall- 0
21047f67ea9SJulia Lawall+ NULL
21147f67ea9SJulia Lawall  != E
21247f67ea9SJulia Lawall)
21347f67ea9SJulia Lawall
21447f67ea9SJulia Lawall@ t3 depends on !patch disable is_zero,isnt_zero @
21547f67ea9SJulia Lawallexpression *E;
21647f67ea9SJulia Lawallposition p;
21747f67ea9SJulia Lawall@@
21847f67ea9SJulia Lawall
21947f67ea9SJulia Lawall(
22047f67ea9SJulia Lawall* E == 0@p
22147f67ea9SJulia Lawall|
22247f67ea9SJulia Lawall* E != 0@p
22347f67ea9SJulia Lawall|
22447f67ea9SJulia Lawall* 0@p == E
22547f67ea9SJulia Lawall|
22647f67ea9SJulia Lawall* 0@p != E
22747f67ea9SJulia Lawall)
22847f67ea9SJulia Lawall
22947f67ea9SJulia Lawall@script:python depends on org@
23047f67ea9SJulia Lawallp << t3.p;
23147f67ea9SJulia Lawall@@
23247f67ea9SJulia Lawall
23347f67ea9SJulia Lawallcoccilib.org.print_todo(p[0], "WARNING comparing pointer to 0")
23447f67ea9SJulia Lawall
23547f67ea9SJulia Lawall@script:python depends on report@
23647f67ea9SJulia Lawallp << t3.p;
23747f67ea9SJulia Lawall@@
23847f67ea9SJulia Lawall
23947f67ea9SJulia Lawallcoccilib.report.print_report(p[0], "WARNING comparing pointer to 0")
240