deref_null.cocci 4.7 KB

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