itnull.cocci 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /// Many iterators have the property that the first argument is always bound
  2. /// to a real list element, never NULL.
  3. //# False positives arise for some iterators that do not have this property,
  4. //# or in cases when the loop cursor is reassigned. The latter should only
  5. //# happen when the matched code is on the way to a loop exit (break, goto,
  6. //# or return).
  7. ///
  8. // Confidence: Moderate
  9. // Copyright: (C) 2010-2012 Nicolas Palix. GPLv2.
  10. // Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6. GPLv2.
  11. // Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6. GPLv2.
  12. // URL: http://coccinelle.lip6.fr/
  13. // Comments:
  14. // Options: --no-includes --include-headers
  15. virtual patch
  16. virtual context
  17. virtual org
  18. virtual report
  19. @depends on patch@
  20. iterator I;
  21. expression x,E,E1,E2;
  22. statement S,S1,S2;
  23. @@
  24. I(x,...) { <...
  25. (
  26. - if (x == NULL && ...) S
  27. |
  28. - if (x != NULL || ...)
  29. S
  30. |
  31. - (x == NULL) ||
  32. E
  33. |
  34. - (x != NULL) &&
  35. E
  36. |
  37. - (x == NULL && ...) ? E1 :
  38. E2
  39. |
  40. - (x != NULL || ...) ?
  41. E1
  42. - : E2
  43. |
  44. - if (x == NULL && ...) S1 else
  45. S2
  46. |
  47. - if (x != NULL || ...)
  48. S1
  49. - else S2
  50. |
  51. + BAD(
  52. x == NULL
  53. + )
  54. |
  55. + BAD(
  56. x != NULL
  57. + )
  58. )
  59. ...> }
  60. @r depends on !patch exists@
  61. iterator I;
  62. expression x,E;
  63. position p1,p2;
  64. @@
  65. *I@p1(x,...)
  66. { ... when != x = E
  67. (
  68. * x@p2 == NULL
  69. |
  70. * x@p2 != NULL
  71. )
  72. ... when any
  73. }
  74. @script:python depends on org@
  75. p1 << r.p1;
  76. p2 << r.p2;
  77. @@
  78. cocci.print_main("iterator-bound variable",p1)
  79. cocci.print_secs("useless NULL test",p2)
  80. @script:python depends on report@
  81. p1 << r.p1;
  82. p2 << r.p2;
  83. @@
  84. msg = "ERROR: iterator variable bound on line %s cannot be NULL" % (p1[0].line)
  85. coccilib.report.print_report(p2[0], msg)