malloc-error-check.cocci 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. @malloced@
  2. expression x;
  3. position p1;
  4. identifier func =~ "(SCMalloc|SCStrdup|SCCalloc|SCMallocAligned|SCRealloc)";
  5. @@
  6. x@p1 = func(...)
  7. @inlinetested@
  8. expression x, E;
  9. statement S;
  10. position malloced.p1;
  11. identifier func =~ "(SCMalloc|SCStrdup|SCCalloc|SCMallocAligned|SCRealloc)";
  12. @@
  13. (
  14. if ((x@p1 = func(...)) == NULL) S
  15. |
  16. if (E && (x@p1 = func(...)) == NULL) S
  17. )
  18. @realloc exists@
  19. position malloced.p1;
  20. expression x, E1;
  21. identifier func =~ "(SCMalloc|SCCalloc|SCMallocAligned)";
  22. @@
  23. x@p1 = func(...)
  24. ... when != x
  25. x = SCRealloc(x, E1)
  26. @istested depends on !realloc exists@
  27. expression x, E1;
  28. position malloced.p1;
  29. statement S1, S2;
  30. identifier func =~ "(SCMalloc|SCStrdup|SCCalloc|SCMallocAligned|SCRealloc)";
  31. @@
  32. x@p1 = func(...)
  33. ... when != x
  34. (
  35. if (unlikely(x == NULL)) S1
  36. |
  37. if (unlikely(x == NULL)) S1 else S2
  38. |
  39. if (likely(x != NULL)) S1
  40. |
  41. if (x == NULL) S1
  42. |
  43. if (x != NULL) S1 else S2
  44. |
  45. if (x && E1) S1
  46. |
  47. BUG_ON(x == NULL)
  48. |
  49. FAIL_IF(x == NULL)
  50. |
  51. FAIL_IF(unlikely(x == NULL))
  52. |
  53. FAIL_IF_NULL(x)
  54. )
  55. @script:python depends on !realloc && !istested && !inlinetested@
  56. p1 << malloced.p1;
  57. @@
  58. print("Structure malloced at %s:%s but error is not checked." % (p1[0].file, p1[0].line))
  59. import sys
  60. sys.exit(1)