odd_ptr_err.cocci 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /// PTR_ERR should access the value just tested by IS_ERR
  3. //# There can be false positives in the patch case, where it is the call to
  4. //# IS_ERR that is wrong.
  5. ///
  6. // Confidence: High
  7. // Copyright: (C) 2012, 2015 Julia Lawall, INRIA.
  8. // Copyright: (C) 2012, 2015 Gilles Muller, INRIA.
  9. // URL: https://coccinelle.gitlabpages.inria.fr/website
  10. // Options: --no-includes --include-headers
  11. virtual patch
  12. virtual context
  13. virtual org
  14. virtual report
  15. @ok1 exists@
  16. expression x,e;
  17. position p;
  18. @@
  19. if (IS_ERR(x=e) || ...) {
  20. <...
  21. PTR_ERR@p(x)
  22. ...>
  23. }
  24. @ok2 exists@
  25. expression x,e1,e2;
  26. position p;
  27. @@
  28. if (IS_ERR(x) || ...) {
  29. <...
  30. (
  31. PTR_ERR@p(\(e1 ? e2 : x\|e1 ? x : e2\))
  32. |
  33. PTR_ERR@p(x)
  34. )
  35. ...>
  36. }
  37. @r1 depends on patch && !context && !org && !report exists@
  38. expression x,y;
  39. position p != {ok1.p,ok2.p};
  40. @@
  41. if (IS_ERR(x) || ...) {
  42. ... when any
  43. when != IS_ERR(...)
  44. (
  45. PTR_ERR(x)
  46. |
  47. PTR_ERR@p(
  48. - y
  49. + x
  50. )
  51. )
  52. ... when any
  53. }
  54. // ----------------------------------------------------------------------------
  55. @r1_context depends on !patch && (context || org || report) exists@
  56. position p != {ok1.p,ok2.p};
  57. expression x, y;
  58. position j0, j1;
  59. @@
  60. if (IS_ERR@j0(x) || ...) {
  61. ... when any
  62. when != IS_ERR(...)
  63. (
  64. PTR_ERR(x)
  65. |
  66. PTR_ERR@j1@p(
  67. y
  68. )
  69. )
  70. ... when any
  71. }
  72. @r1_disj depends on !patch && (context || org || report) exists@
  73. position p != {ok1.p,ok2.p};
  74. expression x, y;
  75. position r1_context.j0, r1_context.j1;
  76. @@
  77. * if (IS_ERR@j0(x) || ...) {
  78. ... when any
  79. when != IS_ERR(...)
  80. * PTR_ERR@j1@p(
  81. y
  82. )
  83. ... when any
  84. }
  85. // ----------------------------------------------------------------------------
  86. @script:python r1_org depends on org@
  87. j0 << r1_context.j0;
  88. j1 << r1_context.j1;
  89. @@
  90. msg = "inconsistent IS_ERR and PTR_ERR"
  91. coccilib.org.print_todo(j0[0], msg)
  92. coccilib.org.print_link(j1[0], "")
  93. // ----------------------------------------------------------------------------
  94. @script:python r1_report depends on report@
  95. j0 << r1_context.j0;
  96. j1 << r1_context.j1;
  97. @@
  98. msg = "inconsistent IS_ERR and PTR_ERR on line %s." % (j1[0].line)
  99. coccilib.report.print_report(j0[0], msg)