^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /// Compare pointer-typed values to NULL rather than 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) ///
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) //# This makes an effort to choose between !x and x == NULL. !x is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) //# if it has previously been used with the function used to initialize x.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) //# This relies on type information. More type information can be obtained
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) //# using the option -all_includes and the option -I to specify an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) //# include path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) // Confidence: High
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) // Copyright: (C) 2012 Julia Lawall, INRIA/LIP6.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) // Copyright: (C) 2012 Gilles Muller, INRIA/LiP6.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) // URL: http://coccinelle.lip6.fr/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) // Requires: 1.0.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) // Options:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) virtual patch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) virtual context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) virtual org
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) virtual report
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) @initialize:ocaml@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) let negtable = Hashtbl.create 101
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) @depends on patch@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) expression *E;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) identifier f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) (
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) (E = f(...)) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) - 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) + NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) (E = f(...)) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) - 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) + NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) - 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) + NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) == (E = f(...))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) - 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) + NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) != (E = f(...))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) @t1 depends on !patch@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) expression *E;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) identifier f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) position p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) (
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) (E = f(...)) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * 0@p
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) (E = f(...)) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * 0@p
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * 0@p
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) == (E = f(...))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * 0@p
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) != (E = f(...))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) @script:python depends on org@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) p << t1.p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) coccilib.org.print_todo(p[0], "WARNING comparing pointer to 0")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) @script:python depends on report@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) p << t1.p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) coccilib.report.print_report(p[0], "WARNING comparing pointer to 0")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) // Tests of returned values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) @s@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) identifier f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) expression E,E1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) E = f(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) ... when != E = E1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) !E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) @script:ocaml depends on s@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) f << s.f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) try let _ = Hashtbl.find negtable f in ()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) with Not_found -> Hashtbl.add negtable f ()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) @ r disable is_zero,isnt_zero exists @
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) expression *E;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) identifier f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) E = f(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) (E == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) |E != 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) |0 == E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) |0 != E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) @script:ocaml@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) f << r.f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) try let _ = Hashtbl.find negtable f in ()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) with Not_found -> include_match false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) // This rule may lead to inconsistent path problems, if E is defined in two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) // places
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) @ depends on patch disable is_zero,isnt_zero @
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) expression *E;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) expression E1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) identifier r.f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) E = f(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) <...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) (
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) - E == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) + !E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) - E != 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) + E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) - 0 == E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) + !E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) - 0 != E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) + E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) ...>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) ?E = E1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) @t2 depends on !patch disable is_zero,isnt_zero @
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) expression *E;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) expression E1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) identifier r.f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) position p1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) position p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) E = f(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) <...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) (
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * E == 0@p1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * E != 0@p2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * 0@p1 == E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * 0@p1 != E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ...>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) ?E = E1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) @script:python depends on org@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) p << t2.p1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) coccilib.org.print_todo(p[0], "WARNING comparing pointer to 0, suggest !E")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) @script:python depends on org@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) p << t2.p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) coccilib.org.print_todo(p[0], "WARNING comparing pointer to 0")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) @script:python depends on report@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) p << t2.p1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) coccilib.report.print_report(p[0], "WARNING comparing pointer to 0, suggest !E")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) @script:python depends on report@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) p << t2.p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) coccilib.report.print_report(p[0], "WARNING comparing pointer to 0")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) @ depends on patch disable is_zero,isnt_zero @
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) expression *E;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) (
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) E ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) - 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) + NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) E !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) - 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) + NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) - 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) + NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) == E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) - 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) + NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) != E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) @ t3 depends on !patch disable is_zero,isnt_zero @
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) expression *E;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) position p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) (
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * E == 0@p
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * E != 0@p
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * 0@p == E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * 0@p != E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) @script:python depends on org@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) p << t3.p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) coccilib.org.print_todo(p[0], "WARNING comparing pointer to 0")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) @script:python depends on report@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) p << t3.p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) coccilib.report.print_report(p[0], "WARNING comparing pointer to 0")