LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Yury Norov <yury.norov@gmail.com>
To: Petr Mladek <pmladek@suse.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Andy Shevchenko <andy.shevchenko@gmail.com>,
	Bartosz Golaszewski <bgolaszewski@baylibre.com>,
	Chris Down <chris@chrisdown.name>,
	Gilles Muller <Gilles.Muller@inria.fr>,
	Ingo Molnar <mingo@kernel.org>,
	Jacob Keller <jacob.e.keller@intel.com>,
	Joe Perches <joe@perches.com>,
	Julia Lawall <Julia.Lawall@inria.fr>,
	Michal Marek <michal.lkml@markovi.net>,
	Nicolas Palix <nicolas.palix@imag.fr>,
	Peter Zijlstra <peterz@infradead.org>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Sergey Senozhatsky <senozhatsky@chromium.org>,
	Stephen Boyd <swboyd@chromium.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	linux-kernel@vger.kernel.org, cocci@systeme.lip6.fr
Cc: Yury Norov <yury.norov@gmail.com>
Subject: [PATCH 2/2] coccinelle: add script for sputchar()
Date: Sat,  4 Sep 2021 16:10:20 -0700	[thread overview]
Message-ID: <20210904231020.331185-3-yury.norov@gmail.com> (raw)
In-Reply-To: <20210904231020.331185-1-yury.norov@gmail.com>

This script find 47 candidates for sputchar() replacement, 
none of them is false-positive.

Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
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


      parent reply	other threads:[~2021-09-04 23:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-04 23:10 [PATCH 0/2] lib: add sputchar() helper Yury Norov
2021-09-04 23:10 ` [PATCH v2 1/2] " Yury Norov
2021-09-05  1:36   ` Joe Perches
2021-09-05  3:20     ` Yury Norov
2021-09-06 11:51   ` Rasmus Villemoes
2021-09-07  9:35     ` Petr Mladek
2021-09-04 23:10 ` Yury Norov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210904231020.331185-3-yury.norov@gmail.com \
    --to=yury.norov@gmail.com \
    --cc=Gilles.Muller@inria.fr \
    --cc=Julia.Lawall@inria.fr \
    --cc=akpm@linux-foundation.org \
    --cc=andy.shevchenko@gmail.com \
    --cc=bgolaszewski@baylibre.com \
    --cc=chris@chrisdown.name \
    --cc=cocci@systeme.lip6.fr \
    --cc=jacob.e.keller@intel.com \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=michal.lkml@markovi.net \
    --cc=mingo@kernel.org \
    --cc=nicolas.palix@imag.fr \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=rostedt@goodmis.org \
    --cc=senozhatsky@chromium.org \
    --cc=swboyd@chromium.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).