deref_null.cocci 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. ///
  2. /// A variable is dereference under a NULL test.
  3. /// Even though it is know to be NULL.
  4. ///
  5. // Confidence: Moderate
  6. // Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2.
  7. // Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2.
  8. // Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2.
  9. // URL: http://coccinelle.lip6.fr/
  10. // Comments: -I ... -all_includes can give more complete results
  11. // Options:
  12. virtual context
  13. virtual org
  14. virtual report
  15. @ifm@
  16. expression *E;
  17. statement S1,S2;
  18. position p1;
  19. @@
  20. if@p1 ((E == NULL && ...) || ...) S1 else S2
  21. // The following two rules are separate, because both can match a single
  22. // expression in different ways
  23. @pr1 expression@
  24. expression *ifm.E;
  25. identifier f;
  26. position p1;
  27. @@
  28. (E != NULL && ...) ? <+...E->f@p1...+> : ...
  29. @pr2 expression@
  30. expression *ifm.E;
  31. identifier f;
  32. position p2;
  33. @@
  34. (
  35. (E != NULL) && ... && <+...E->f@p2...+>
  36. |
  37. (E == NULL) || ... || <+...E->f@p2...+>
  38. |
  39. sizeof(<+...E->f@p2...+>)
  40. )
  41. // For org and report modes
  42. @r depends on !context && (org || report) exists@
  43. expression subE <= ifm.E;
  44. expression *ifm.E;
  45. expression E1,E2;
  46. identifier f;
  47. statement S1,S2,S3,S4;
  48. iterator iter;
  49. position p!={pr1.p1,pr2.p2};
  50. position ifm.p1;
  51. @@
  52. if@p1 ((E == NULL && ...) || ...)
  53. {
  54. ... when != if (...) S1 else S2
  55. (
  56. iter(subE,...) S4 // no use
  57. |
  58. list_remove_head(E2,subE,...)
  59. |
  60. subE = E1
  61. |
  62. for(subE = E1;...;...) S4
  63. |
  64. subE++
  65. |
  66. ++subE
  67. |
  68. --subE
  69. |
  70. subE--
  71. |
  72. &subE
  73. |
  74. E->f@p // bad use
  75. )
  76. ... when any
  77. return ...;
  78. }
  79. else S3
  80. @script:python depends on !context && !org && report@
  81. p << r.p;
  82. p1 << ifm.p1;
  83. x << ifm.E;
  84. @@
  85. msg="ERROR: %s is NULL but dereferenced." % (x)
  86. coccilib.report.print_report(p[0], msg)
  87. cocci.include_match(False)
  88. @script:python depends on !context && org && !report@
  89. p << r.p;
  90. p1 << ifm.p1;
  91. x << ifm.E;
  92. @@
  93. msg="ERROR: %s is NULL but dereferenced." % (x)
  94. msg_safe=msg.replace("[","@(").replace("]",")")
  95. cocci.print_main(msg_safe,p)
  96. cocci.include_match(False)
  97. @s depends on !context && (org || report) exists@
  98. expression subE <= ifm.E;
  99. expression *ifm.E;
  100. expression E1,E2;
  101. identifier f;
  102. statement S1,S2,S3,S4;
  103. iterator iter;
  104. position p!={pr1.p1,pr2.p2};
  105. position ifm.p1;
  106. @@
  107. if@p1 ((E == NULL && ...) || ...)
  108. {
  109. ... when != if (...) S1 else S2
  110. (
  111. iter(subE,...) S4 // no use
  112. |
  113. list_remove_head(E2,subE,...)
  114. |
  115. subE = E1
  116. |
  117. for(subE = E1;...;...) S4
  118. |
  119. subE++
  120. |
  121. ++subE
  122. |
  123. --subE
  124. |
  125. subE--
  126. |
  127. &subE
  128. |
  129. E->f@p // bad use
  130. )
  131. ... when any
  132. }
  133. else S3
  134. @script:python depends on !context && !org && report@
  135. p << s.p;
  136. p1 << ifm.p1;
  137. x << ifm.E;
  138. @@
  139. msg="ERROR: %s is NULL but dereferenced." % (x)
  140. coccilib.report.print_report(p[0], msg)
  141. @script:python depends on !context && org && !report@
  142. p << s.p;
  143. p1 << ifm.p1;
  144. x << ifm.E;
  145. @@
  146. msg="ERROR: %s is NULL but dereferenced." % (x)
  147. msg_safe=msg.replace("[","@(").replace("]",")")
  148. cocci.print_main(msg_safe,p)
  149. // For context mode
  150. @depends on context && !org && !report exists@
  151. expression subE <= ifm.E;
  152. expression *ifm.E;
  153. expression E1,E2;
  154. identifier f;
  155. statement S1,S2,S3,S4;
  156. iterator iter;
  157. position p!={pr1.p1,pr2.p2};
  158. position ifm.p1;
  159. @@
  160. if@p1 ((E == NULL && ...) || ...)
  161. {
  162. ... when != if (...) S1 else S2
  163. (
  164. iter(subE,...) S4 // no use
  165. |
  166. list_remove_head(E2,subE,...)
  167. |
  168. subE = E1
  169. |
  170. for(subE = E1;...;...) S4
  171. |
  172. subE++
  173. |
  174. ++subE
  175. |
  176. --subE
  177. |
  178. subE--
  179. |
  180. &subE
  181. |
  182. * E->f@p // bad use
  183. )
  184. ... when any
  185. return ...;
  186. }
  187. else S3
  188. // The following three rules are duplicates of ifm, pr1 and pr2 respectively.
  189. // It is need because the previous rule as already made a "change".
  190. @ifm1@
  191. expression *E;
  192. statement S1,S2;
  193. position p1;
  194. @@
  195. if@p1 ((E == NULL && ...) || ...) S1 else S2
  196. @pr11 expression@
  197. expression *ifm1.E;
  198. identifier f;
  199. position p1;
  200. @@
  201. (E != NULL && ...) ? <+...E->f@p1...+> : ...
  202. @pr12 expression@
  203. expression *ifm1.E;
  204. identifier f;
  205. position p2;
  206. @@
  207. (
  208. (E != NULL) && ... && <+...E->f@p2...+>
  209. |
  210. (E == NULL) || ... || <+...E->f@p2...+>
  211. |
  212. sizeof(<+...E->f@p2...+>)
  213. )
  214. @depends on context && !org && !report exists@
  215. expression subE <= ifm1.E;
  216. expression *ifm1.E;
  217. expression E1,E2;
  218. identifier f;
  219. statement S1,S2,S3,S4;
  220. iterator iter;
  221. position p!={pr11.p1,pr12.p2};
  222. position ifm1.p1;
  223. @@
  224. if@p1 ((E == NULL && ...) || ...)
  225. {
  226. ... when != if (...) S1 else S2
  227. (
  228. iter(subE,...) S4 // no use
  229. |
  230. list_remove_head(E2,subE,...)
  231. |
  232. subE = E1
  233. |
  234. for(subE = E1;...;...) S4
  235. |
  236. subE++
  237. |
  238. ++subE
  239. |
  240. --subE
  241. |
  242. subE--
  243. |
  244. &subE
  245. |
  246. * E->f@p // bad use
  247. )
  248. ... when any
  249. }
  250. else S3