itnull.cocci 1.6 KB

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