flexible_array.cocci 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. ///
  3. /// Zero-length and one-element arrays are deprecated, see
  4. /// Documentation/process/deprecated.rst
  5. /// Flexible-array members should be used instead.
  6. ///
  7. //
  8. // Confidence: High
  9. // Copyright: (C) 2020 Denis Efremov ISPRAS.
  10. // Comments:
  11. // Options: --no-includes --include-headers
  12. virtual context
  13. virtual report
  14. virtual org
  15. virtual patch
  16. @initialize:python@
  17. @@
  18. def relevant(positions):
  19. for p in positions:
  20. if "uapi" in p.file:
  21. return False
  22. return True
  23. @r depends on !patch@
  24. identifier name, array;
  25. type T;
  26. position p : script:python() { relevant(p) };
  27. @@
  28. (
  29. struct name {
  30. ...
  31. * T array@p[\(0\|1\)];
  32. };
  33. |
  34. struct {
  35. ...
  36. * T array@p[\(0\|1\)];
  37. };
  38. |
  39. union name {
  40. ...
  41. * T array@p[\(0\|1\)];
  42. };
  43. |
  44. union {
  45. ...
  46. * T array@p[\(0\|1\)];
  47. };
  48. )
  49. @only_field depends on patch@
  50. identifier name, array;
  51. type T;
  52. position q;
  53. @@
  54. (
  55. struct name {@q
  56. T array[0];
  57. };
  58. |
  59. struct {@q
  60. T array[0];
  61. };
  62. )
  63. @depends on patch@
  64. identifier name, array;
  65. type T;
  66. position p : script:python() { relevant(p) };
  67. // position @q with rule "only_field" simplifies
  68. // handling of bitfields, arrays, etc.
  69. position q != only_field.q;
  70. @@
  71. (
  72. struct name {@q
  73. ...
  74. T array@p[
  75. - 0
  76. ];
  77. };
  78. |
  79. struct {@q
  80. ...
  81. T array@p[
  82. - 0
  83. ];
  84. };
  85. )
  86. @script: python depends on report@
  87. p << r.p;
  88. @@
  89. msg = "WARNING use flexible-array member instead (https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays)"
  90. coccilib.report.print_report(p[0], msg)
  91. @script: python depends on org@
  92. p << r.p;
  93. @@
  94. msg = "WARNING use flexible-array member instead (https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays)"
  95. coccilib.org.print_todo(p[0], msg)