badzero.cocci 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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:
  14. // Options:
  15. virtual patch
  16. virtual context
  17. virtual org
  18. virtual report
  19. @initialize:ocaml@
  20. let negtable = Hashtbl.create 101
  21. @depends on patch@
  22. expression *E;
  23. identifier f;
  24. @@
  25. (
  26. (E = f(...)) ==
  27. - 0
  28. + NULL
  29. |
  30. (E = f(...)) !=
  31. - 0
  32. + NULL
  33. |
  34. - 0
  35. + NULL
  36. == (E = f(...))
  37. |
  38. - 0
  39. + NULL
  40. != (E = f(...))
  41. )
  42. @t1 depends on !patch@
  43. expression *E;
  44. identifier f;
  45. position p;
  46. @@
  47. (
  48. (E = f(...)) ==
  49. * 0@p
  50. |
  51. (E = f(...)) !=
  52. * 0@p
  53. |
  54. * 0@p
  55. == (E = f(...))
  56. |
  57. * 0@p
  58. != (E = f(...))
  59. )
  60. @script:python depends on org@
  61. p << t1.p;
  62. @@
  63. coccilib.org.print_todo(p[0], "WARNING comparing pointer to 0")
  64. @script:python depends on report@
  65. p << t1.p;
  66. @@
  67. coccilib.report.print_report(p[0], "WARNING comparing pointer to 0")
  68. // Tests of returned values
  69. @s@
  70. identifier f;
  71. expression E,E1;
  72. @@
  73. E = f(...)
  74. ... when != E = E1
  75. !E
  76. @script:ocaml depends on s@
  77. f << s.f;
  78. @@
  79. try let _ = Hashtbl.find negtable f in ()
  80. with Not_found -> Hashtbl.add negtable f ()
  81. @ r disable is_zero,isnt_zero exists @
  82. expression *E;
  83. identifier f;
  84. @@
  85. E = f(...)
  86. ...
  87. (E == 0
  88. |E != 0
  89. |0 == E
  90. |0 != E
  91. )
  92. @script:ocaml@
  93. f << r.f;
  94. @@
  95. try let _ = Hashtbl.find negtable f in ()
  96. with Not_found -> include_match false
  97. // This rule may lead to inconsistent path problems, if E is defined in two
  98. // places
  99. @ depends on patch disable is_zero,isnt_zero @
  100. expression *E;
  101. expression E1;
  102. identifier r.f;
  103. @@
  104. E = f(...)
  105. <...
  106. (
  107. - E == 0
  108. + !E
  109. |
  110. - E != 0
  111. + E
  112. |
  113. - 0 == E
  114. + !E
  115. |
  116. - 0 != E
  117. + E
  118. )
  119. ...>
  120. ?E = E1
  121. @t2 depends on !patch disable is_zero,isnt_zero @
  122. expression *E;
  123. expression E1;
  124. identifier r.f;
  125. position p1;
  126. position p2;
  127. @@
  128. E = f(...)
  129. <...
  130. (
  131. * E == 0@p1
  132. |
  133. * E != 0@p2
  134. |
  135. * 0@p1 == E
  136. |
  137. * 0@p1 != E
  138. )
  139. ...>
  140. ?E = E1
  141. @script:python depends on org@
  142. p << t2.p1;
  143. @@
  144. coccilib.org.print_todo(p[0], "WARNING comparing pointer to 0, suggest !E")
  145. @script:python depends on org@
  146. p << t2.p2;
  147. @@
  148. coccilib.org.print_todo(p[0], "WARNING comparing pointer to 0")
  149. @script:python depends on report@
  150. p << t2.p1;
  151. @@
  152. coccilib.report.print_report(p[0], "WARNING comparing pointer to 0, suggest !E")
  153. @script:python depends on report@
  154. p << t2.p2;
  155. @@
  156. coccilib.report.print_report(p[0], "WARNING comparing pointer to 0")
  157. @ depends on patch disable is_zero,isnt_zero @
  158. expression *E;
  159. @@
  160. (
  161. E ==
  162. - 0
  163. + NULL
  164. |
  165. E !=
  166. - 0
  167. + NULL
  168. |
  169. - 0
  170. + NULL
  171. == E
  172. |
  173. - 0
  174. + NULL
  175. != E
  176. )
  177. @ t3 depends on !patch disable is_zero,isnt_zero @
  178. expression *E;
  179. position p;
  180. @@
  181. (
  182. * E == 0@p
  183. |
  184. * E != 0@p
  185. |
  186. * 0@p == E
  187. |
  188. * 0@p != E
  189. )
  190. @script:python depends on org@
  191. p << t3.p;
  192. @@
  193. coccilib.org.print_todo(p[0], "WARNING comparing pointer to 0")
  194. @script:python depends on report@
  195. p << t3.p;
  196. @@
  197. coccilib.report.print_report(p[0], "WARNING comparing pointer to 0")