ifnullfree.cocci 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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)
  18. (
  19. - kfree(E);
  20. + kfree(E);
  21. |
  22. - debugfs_remove(E);
  23. + debugfs_remove(E);
  24. |
  25. - debugfs_remove_recursive(E);
  26. + debugfs_remove_recursive(E);
  27. |
  28. - usb_free_urb(E);
  29. + usb_free_urb(E);
  30. )
  31. @r depends on context || report || org @
  32. expression E;
  33. position p;
  34. @@
  35. * if (E)
  36. * \(kfree@p\|debugfs_remove@p\|debugfs_remove_recursive@p\|usb_free_urb\)(E);
  37. @script:python depends on org@
  38. p << r.p;
  39. @@
  40. cocci.print_main("NULL check before that freeing function is not needed", p)
  41. @script:python depends on report@
  42. p << r.p;
  43. @@
  44. 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."
  45. coccilib.report.print_report(p[0], msg)