badty.cocci 1.7 KB

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