kmerr.cocci 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /// This semantic patch looks for kmalloc etc that are not followed by a
  3. /// NULL check. It only gives a report in the case where there is some
  4. /// error handling code later in the function, which may be helpful
  5. /// in determining what the error handling code for the call to kmalloc etc
  6. /// should be.
  7. ///
  8. // Confidence: High
  9. // Copyright: (C) 2010 Nicolas Palix, DIKU.
  10. // Copyright: (C) 2010 Julia Lawall, DIKU.
  11. // Copyright: (C) 2010 Gilles Muller, INRIA/LiP6.
  12. // URL: https://coccinelle.gitlabpages.inria.fr/website
  13. // Comments:
  14. // Options: --no-includes --include-headers
  15. virtual context
  16. virtual org
  17. virtual report
  18. @withtest@
  19. expression x;
  20. position p;
  21. identifier f,fld;
  22. @@
  23. x@p = f(...);
  24. ... when != x->fld
  25. \(x == NULL \| x != NULL\)
  26. @fixed depends on context && !org && !report@
  27. expression x,x1;
  28. position p1 != withtest.p;
  29. statement S;
  30. position any withtest.p;
  31. identifier f;
  32. @@
  33. *x@p1 = \(kmalloc\|kzalloc\|kcalloc\)(...);
  34. ...
  35. *x1@p = f(...);
  36. if (!x1) S
  37. // ------------------------------------------------------------------------
  38. @rfixed depends on (org || report) && !context exists@
  39. expression x,x1;
  40. position p1 != withtest.p;
  41. position p2;
  42. statement S;
  43. position any withtest.p;
  44. identifier f;
  45. @@
  46. x@p1 = \(kmalloc\|kzalloc\|kcalloc\)(...);
  47. ...
  48. x1@p = f@p2(...);
  49. if (!x1) S
  50. @script:python depends on org@
  51. p1 << rfixed.p1;
  52. p2 << rfixed.p2;
  53. @@
  54. cocci.print_main("alloc call",p1)
  55. cocci.print_secs("possible model",p2)
  56. @script:python depends on report@
  57. p1 << rfixed.p1;
  58. p2 << rfixed.p2;
  59. @@
  60. msg = "alloc with no test, possible model on line %s" % (p2[0].line)
  61. coccilib.report.print_report(p1[0],msg)