badzero.cocci 3.2 KB

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