pool_zalloc-simple.cocci 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. ///
  3. /// Use *_pool_zalloc rather than *_pool_alloc followed by memset with 0
  4. ///
  5. // Copyright: (C) 2015 Intel Corp.
  6. // Options: --no-includes --include-headers
  7. //
  8. // Keywords: dma_pool_zalloc, pci_pool_zalloc
  9. //
  10. virtual context
  11. virtual patch
  12. virtual org
  13. virtual report
  14. //----------------------------------------------------------
  15. // For context mode
  16. //----------------------------------------------------------
  17. @depends on context@
  18. expression x;
  19. statement S;
  20. @@
  21. * x = \(dma_pool_alloc\|pci_pool_alloc\)(...);
  22. if ((x==NULL) || ...) S
  23. * memset(x,0, ...);
  24. //----------------------------------------------------------
  25. // For patch mode
  26. //----------------------------------------------------------
  27. @depends on patch@
  28. expression x;
  29. expression a,b,c;
  30. statement S;
  31. @@
  32. - x = dma_pool_alloc(a,b,c);
  33. + x = dma_pool_zalloc(a,b,c);
  34. if ((x==NULL) || ...) S
  35. - memset(x,0,...);
  36. @depends on patch@
  37. expression x;
  38. expression a,b,c;
  39. statement S;
  40. @@
  41. - x = pci_pool_alloc(a,b,c);
  42. + x = pci_pool_zalloc(a,b,c);
  43. if ((x==NULL) || ...) S
  44. - memset(x,0,...);
  45. //----------------------------------------------------------
  46. // For org and report mode
  47. //----------------------------------------------------------
  48. @r depends on org || report@
  49. expression x;
  50. expression a,b,c;
  51. statement S;
  52. position p;
  53. @@
  54. x = @p\(dma_pool_alloc\|pci_pool_alloc\)(a,b,c);
  55. if ((x==NULL) || ...) S
  56. memset(x,0, ...);
  57. @script:python depends on org@
  58. p << r.p;
  59. x << r.x;
  60. @@
  61. msg="%s" % (x)
  62. msg_safe=msg.replace("[","@(").replace("]",")")
  63. coccilib.org.print_todo(p[0], msg_safe)
  64. @script:python depends on report@
  65. p << r.p;
  66. x << r.x;
  67. @@
  68. msg="WARNING: *_pool_zalloc should be used for %s, instead of *_pool_alloc/memset" % (x)
  69. coccilib.report.print_report(p[0], msg)