struct_size.cocci 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. ///
  3. /// Check for code that could use struct_size().
  4. ///
  5. // Confidence: Medium
  6. // Author: Jacob Keller <jacob.e.keller@intel.com>
  7. // Copyright: (C) 2023 Intel Corporation
  8. // Options: --no-includes --include-headers
  9. virtual patch
  10. virtual context
  11. virtual org
  12. virtual report
  13. // the overflow Kunit tests have some code which intentionally does not use
  14. // the macros, so we want to ignore this code when reporting potential
  15. // issues.
  16. @overflow_tests@
  17. identifier f = overflow_size_helpers_test;
  18. @@
  19. f
  20. //----------------------------------------------------------
  21. // For context mode
  22. //----------------------------------------------------------
  23. @depends on !overflow_tests && context@
  24. expression E1, E2;
  25. identifier m;
  26. @@
  27. (
  28. * (sizeof(*E1) + (E2 * sizeof(*E1->m)))
  29. )
  30. //----------------------------------------------------------
  31. // For patch mode
  32. //----------------------------------------------------------
  33. @depends on !overflow_tests && patch@
  34. expression E1, E2;
  35. identifier m;
  36. @@
  37. (
  38. - (sizeof(*E1) + (E2 * sizeof(*E1->m)))
  39. + struct_size(E1, m, E2)
  40. )
  41. //----------------------------------------------------------
  42. // For org and report mode
  43. //----------------------------------------------------------
  44. @r depends on !overflow_tests && (org || report)@
  45. expression E1, E2;
  46. identifier m;
  47. position p;
  48. @@
  49. (
  50. (sizeof(*E1)@p + (E2 * sizeof(*E1->m)))
  51. )
  52. @script:python depends on org@
  53. p << r.p;
  54. @@
  55. coccilib.org.print_todo(p[0], "WARNING should use struct_size")
  56. @script:python depends on report@
  57. p << r.p;
  58. @@
  59. msg="WARNING: Use struct_size"
  60. coccilib.report.print_report(p[0], msg)