simple_open.cocci 1.2 KB

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