kmerr.cocci 1.6 KB

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