simple_open.cocci 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // SPDX-License-Identifier: GPL-2.0
  2. /// Remove an open coded simple_open() function
  3. /// and replace file operations references to the function
  4. /// with simple_open() instead.
  5. ///
  6. // Confidence: High
  7. // Comments:
  8. // Options: --no-includes --include-headers
  9. virtual patch
  10. virtual report
  11. @ open depends on patch @
  12. identifier open_f != simple_open;
  13. identifier i, f;
  14. @@
  15. -int open_f(struct inode *i, struct file *f)
  16. -{
  17. (
  18. -if (i->i_private)
  19. -f->private_data = i->i_private;
  20. |
  21. -f->private_data = i->i_private;
  22. )
  23. -return 0;
  24. -}
  25. @ has_open depends on open @
  26. identifier fops;
  27. identifier open.open_f;
  28. @@
  29. struct file_operations fops = {
  30. ...,
  31. -.open = open_f,
  32. +.open = simple_open,
  33. ...
  34. };
  35. @ openr depends on report @
  36. identifier open_f != simple_open;
  37. identifier i, f;
  38. position p;
  39. @@
  40. int open_f@p(struct inode *i, struct file *f)
  41. {
  42. (
  43. if (i->i_private)
  44. f->private_data = i->i_private;
  45. |
  46. f->private_data = i->i_private;
  47. )
  48. return 0;
  49. }
  50. @ has_openr depends on openr @
  51. identifier fops;
  52. identifier openr.open_f;
  53. position p;
  54. @@
  55. struct file_operations fops = {
  56. ...,
  57. .open = open_f@p,
  58. ...
  59. };
  60. @script:python@
  61. pf << openr.p;
  62. ps << has_openr.p;
  63. @@
  64. coccilib.report.print_report(pf[0],"WARNING opportunity for simple_open, see also structure on line %s"%(ps[0].line))