[PATCH 2/2] coccinelle: add script for sputchar()
From: Yury Norov
Date: Sat Sep 04 2021 - 19:12:28 EST
This script find 47 candidates for sputchar() replacement,
none of them is false-positive.
Suggested-by: Andy Shevchenko <andy.shevchenko@xxxxxxxxx>
Signed-off-by: Yury Norov <yury.norov@xxxxxxxxx>
---
One test case is commented-out because it causes spatch crash.
Coccinelle is installed from ubuntu deb package.
yury:linux$ spatch --version
spatch version 1.0.8 compiled with OCaml version 4.11.1
Flags passed to the configure script: --prefix=/usr --sysconfdir=/etc --libdir=/usr/lib --enable-ocaml --enable-python --enable-opt
OCaml scripting support: yes
Python scripting support: yes
Syntax of regular expressions: PCRE
scripts/coccinelle/misc/sputchar.cocci | 190 +++++++++++++++++++++++++
1 file changed, 190 insertions(+)
create mode 100644 scripts/coccinelle/misc/sputchar.cocci
diff --git a/scripts/coccinelle/misc/sputchar.cocci b/scripts/coccinelle/misc/sputchar.cocci
new file mode 100644
index 000000000000..23fb7546252f
--- /dev/null
+++ b/scripts/coccinelle/misc/sputchar.cocci
@@ -0,0 +1,190 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Check for opencoded sputchar() implementation.
+///
+// Confidence: High
+// Copyright: (C) 2021 Yury Norov
+// Options: --no-includes --include-headers
+//
+// Keywords: sputchar
+//
+
+virtual patch
+virtual org
+virtual report
+virtual context
+
+@rpostfix depends on !patch@
+identifier func;
+expression buf, end, c;
+position p;
+@@
+
+func(...)
+{
+ <...
+* if ((buf) < (end)) {
+* *(buf) = (c);
+* }
+* (buf)++;@p
+ ...>
+}
+
+@rprefix depends on !patch@
+identifier func;
+expression buf, end, c;
+position p;
+@@
+
+func(...)
+{
+ <...
+* if ((buf) < (end))
+* *(buf) = (c);
+* ++(buf);@p
+ ...>
+}
+
+@rinc1 depends on !patch@
+identifier func;
+expression buf, end, c;
+position p;
+@@
+
+func(...)
+{
+ <...
+* if ((buf) < (end)) {
+* *(buf) = (c);
+* }
+* (buf) += 1;@p
+ ...>
+}
+
+@rinc2 depends on !patch@
+identifier func;
+expression buf, end, c;
+position p;
+@@
+
+func(...)
+{
+ <...
+* if ((buf) < (end)) {
+* *(buf) = (c);
+* }
+* (buf) = (buf) + 1;@p
+ ...>
+}
+
+@ppostfix depends on patch@
+identifier func;
+expression buf, end, c;
+position p;
+@@
+
+func(...)
+{
+ <...
+- if ((buf) < (end)) {
+- *(buf) = (c);
+- }
+- (buf)++;@p
++ buf = sputchar(buf, end, c);
+ ...>
+}
+
+// @pprefix depends on patch@
+// identifier func;
+// expression buf, end, c;
+// position p;
+// @@
+//
+// func(...)
+// {
+// <...
+// - if ((buf) < (end)) {
+// - *(buf) = (c);
+// - }
+// - ++(buf);
+// + buf = sputchar(buf, end, c);
+// ...>
+// }
+
+@pinc1 depends on patch@
+identifier func;
+expression buf, end, c;
+position p;
+@@
+
+func(...)
+{
+ <...
+- if ((buf) < (end)) {
+- *(buf) = (c);
+- }
+- (buf) += 1;
++ buf = sputchar(buf, end, c);
+ ...>
+}
+
+@pinc2 depends on patch@
+identifier func;
+expression buf, end, c;
+position p;
+@@
+
+func(...)
+{
+ <...
+- if ((buf) < (end)) {
+- *(buf) = (c);
+- }
+- (buf) = (buf) + 1;
++ buf = sputchar(buf, end, c);
+ ...>
+}
+
+@script:python depends on report@
+p << rpostfix.p;
+@@
+
+for p0 in p:
+ coccilib.report.print_report(p0, "WARNING opportunity for sputchar()")
+
+@script:python depends on org@
+p << rpostfix.p;
+@@
+
+for p0 in p:
+ coccilib.report.print_report(p0, "WARNING opportunity for sputchar()")
+
+
+@script:python depends on report@
+p << rprefix.p;
+@@
+
+for p0 in p:
+ coccilib.report.print_report(p0, "WARNING opportunity for sputchar()")
+
+@script:python depends on org@
+p << rprefix.p;
+@@
+
+for p0 in p:
+ coccilib.report.print_report(p0, "WARNING opportunity for sputchar()")
+
+@script:python depends on report@
+p << rinc1.p;
+@@
+
+for p0 in p:
+ coccilib.report.print_report(p0, "WARNING opportunity for sputchar()")
+
+@script:python depends on org@
+p << rinc1.p;
+@@
+
+for p0 in p:
+ coccilib.report.print_report(p0, "WARNING opportunity for sputchar()")
+
--
2.30.2