ifnullfree.cocci 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /// NULL check before some freeing functions is not needed.
  2. ///
  3. /// Based on checkpatch warning
  4. /// "kfree(NULL) is safe this check is probably not required"
  5. /// and kfreeaddr.cocci by Julia Lawall.
  6. ///
  7. // Copyright: (C) 2014 Fabian Frederick. GPLv2.
  8. // Comments: -
  9. // Options: --no-includes --include-headers
  10. virtual patch
  11. virtual org
  12. virtual report
  13. virtual context
  14. @r2 depends on patch@
  15. expression E;
  16. @@
  17. - if (E != NULL)
  18. (
  19. kfree(E);
  20. |
  21. debugfs_remove(E);
  22. |
  23. debugfs_remove_recursive(E);
  24. |
  25. usb_free_urb(E);
  26. |
  27. kmem_cache_destroy(E);
  28. |
  29. mempool_destroy(E);
  30. |
  31. dma_pool_destroy(E);
  32. )
  33. @r depends on context || report || org @
  34. expression E;
  35. position p;
  36. @@
  37. * if (E != NULL)
  38. * \(kfree@p\|debugfs_remove@p\|debugfs_remove_recursive@p\|
  39. * usb_free_urb@p\|kmem_cache_destroy@p\|mempool_destroy@p\|
  40. * dma_pool_destroy@p\)(E);
  41. @script:python depends on org@
  42. p << r.p;
  43. @@
  44. cocci.print_main("NULL check before that freeing function is not needed", p)
  45. @script:python depends on report@
  46. p << r.p;
  47. @@
  48. msg = "WARNING: NULL check before freeing functions like kfree, debugfs_remove, debugfs_remove_recursive or usb_free_urb is not needed. Maybe consider reorganizing relevant code to avoid passing NULL values."
  49. coccilib.report.print_report(p[0], msg)