badty.cocci 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /// Correct the size argument to alloc functions
  3. ///
  4. //# This makes an effort to find cases where the argument to sizeof is wrong
  5. //# in memory allocation functions by checking the type of the allocated memory
  6. //# when it is a double pointer and ensuring the sizeof argument takes a pointer
  7. //# to the the memory being allocated. There are false positives in cases the
  8. //# sizeof argument is not used in constructing the return value. The result
  9. //# may need some reformatting.
  10. //
  11. // Confidence: Moderate
  12. // Copyright: (C) 2014 Himangi Saraogi.
  13. // Comments:
  14. // Options:
  15. virtual patch
  16. virtual context
  17. virtual org
  18. virtual report
  19. //----------------------------------------------------------
  20. // For context mode
  21. //----------------------------------------------------------
  22. @depends on context disable sizeof_type_expr@
  23. type T;
  24. T **x;
  25. @@
  26. x =
  27. <+...sizeof(
  28. * T
  29. )...+>
  30. //----------------------------------------------------------
  31. // For patch mode
  32. //----------------------------------------------------------
  33. @depends on patch disable sizeof_type_expr@
  34. type T;
  35. T **x;
  36. @@
  37. x =
  38. <+...sizeof(
  39. - T
  40. + *x
  41. )...+>
  42. //----------------------------------------------------------
  43. // For org and report mode
  44. //----------------------------------------------------------
  45. @r depends on (org || report) disable sizeof_type_expr@
  46. type T;
  47. T **x;
  48. position p;
  49. @@
  50. x =
  51. <+...sizeof(
  52. T@p
  53. )...+>
  54. @script:python depends on org@
  55. p << r.p;
  56. @@
  57. coccilib.org.print_todo(p[0], "WARNING sizeof argument should be pointer type, not structure type")
  58. @script:python depends on report@
  59. p << r.p;
  60. @@
  61. msg="WARNING: Use correct pointer type argument for sizeof"
  62. coccilib.report.print_report(p[0], msg)