newline_in_nl_msg.cocci 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. ///
  3. /// Catch strings ending in newline with GENL_SET_ERR_MSG, NL_SET_ERR_MSG,
  4. /// NL_SET_ERR_MSG_MOD.
  5. ///
  6. // Confidence: Very High
  7. // Copyright: (C) 2020 Intel Corporation
  8. // URL: https://coccinelle.gitlabpages.inria.fr/website
  9. // Options: --no-includes --include-headers
  10. virtual context
  11. virtual org
  12. virtual report
  13. @r depends on context || org || report@
  14. expression e;
  15. constant m;
  16. position p;
  17. @@
  18. \(GENL_SET_ERR_MSG\|NL_SET_ERR_MSG\|NL_SET_ERR_MSG_MOD\)(e,m@p)
  19. @script:python@
  20. m << r.m;
  21. @@
  22. if not m.endswith("\\n\""):
  23. cocci.include_match(False)
  24. @r1 depends on r@
  25. identifier fname;
  26. expression r.e;
  27. constant r.m;
  28. position r.p;
  29. @@
  30. fname(e,m@p)
  31. //----------------------------------------------------------
  32. // For context mode
  33. //----------------------------------------------------------
  34. @depends on context && r@
  35. identifier r1.fname;
  36. expression r.e;
  37. constant r.m;
  38. @@
  39. * fname(e,m)
  40. //----------------------------------------------------------
  41. // For org mode
  42. //----------------------------------------------------------
  43. @script:python depends on org@
  44. fname << r1.fname;
  45. m << r.m;
  46. p << r.p;
  47. @@
  48. if m.endswith("\\n\""):
  49. msg="WARNING avoid newline at end of message in %s" % (fname)
  50. msg_safe=msg.replace("[","@(").replace("]",")")
  51. coccilib.org.print_todo(p[0], msg_safe)
  52. //----------------------------------------------------------
  53. // For report mode
  54. //----------------------------------------------------------
  55. @script:python depends on report@
  56. fname << r1.fname;
  57. m << r.m;
  58. p << r.p;
  59. @@
  60. if m.endswith("\\n\""):
  61. msg="WARNING avoid newline at end of message in %s" % (fname)
  62. coccilib.report.print_report(p[0], msg)