From: Markus Elfring <Markus.Elfring@web.de>
To: cocci@inria.fr, kernel-janitors@vger.kernel.org,
Julia Lawall <Julia.Lawall@inria.fr>,
Nicolas Palix <nicolas.palix@imag.fr>
Cc: LKML <linux-kernel@vger.kernel.org>
Subject: [cocci] [PATCH] Coccinelle: misc: Add SmPL script “show_questionable_count_limit_checks.cocci”
Date: Sat, 5 Jul 2025 22:05:44 +0200 [thread overview]
Message-ID: <30a3a2b1-deef-4f3f-b7da-4164360190ed@web.de> (raw)
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Sat, 5 Jul 2025 21:46:23 +0200
Some known programming mistakes are repeated.
See also:
* https://lore.kernel.org/all/?q=%22Fix+off+by+one%22
* https://wiki.sei.cmu.edu/confluence/display/c/ARR30-C.+Do+not+form+or+use+out-of-bounds+pointers+or+array+subscripts
Thus provide design options for the adjustment of affected source code
also by the means of the semantic patch language (Coccinelle software).
Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
A parse check for the operation mode “report” provides the following hint.
“…
Grep query
for
…”
It seems that such a key word tends to occur in a lot of source files.
Thus this source code search pattern will probably trigger the demand for
special computation resources.
I became curious on corresponding collateral evolution.
...show_questionable_count_limit_checks.cocci | 72 +++++++++++++++++++
1 file changed, 72 insertions(+)
create mode 100644 scripts/coccinelle/misc/show_questionable_count_limit_checks.cocci
diff --git a/scripts/coccinelle/misc/show_questionable_count_limit_checks.cocci b/scripts/coccinelle/misc/show_questionable_count_limit_checks.cocci
new file mode 100644
index 000000000000..d565e306711f
--- /dev/null
+++ b/scripts/coccinelle/misc/show_questionable_count_limit_checks.cocci
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: GPL-2.0
+/// Reconsider questionable count limit checks.
+//
+// Keywords: detection programming mistakes
+// Confidence: Medium
+// Options: --no-includes --include-headers
+
+virtual context, patch, report, org
+
+@depends on context@
+expression limit;
+identifier i;
+statement s;
+@@
+ for (
+ <+... i = 0 ...+> ;
+* i <= limit
+ ;
+ <+...
+( i++
+| ++i
+) ...+>
+ )
+ s
+
+@depends on patch disable gtr_lss_eq@
+expression limit;
+identifier i;
+statement s;
+@@
+ for (
+ <+... i = 0 ...+> ;
+(
+- i <= limit
++ i < limit
+|
+- limit >= i
++ limit > i
+) ;
+ <+...
+( i++
+| ++i
+) ...+>
+ )
+ s
+
+@x depends on org || report@
+expression limit;
+identifier i;
+position p;
+statement s;
+@@
+ for (
+ <+... i = 0 ...+> ;
+ i <= limit
+ ;@p
+ <+...
+( i++
+| ++i
+) ...+>
+ )
+ s
+
+@script:python depends on org@
+p << x.p;
+@@
+coccilib.org.print_todo(p[0], "WARNING: Reconsider questionable count limit check once more.")
+
+@script:python depends on report@
+p << x.p;
+@@
+coccilib.report.print_report(p[0], "WARNING: Reconsider questionable count limit check once more.")
--
2.50.0
reply other threads:[~2025-07-05 20:06 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=30a3a2b1-deef-4f3f-b7da-4164360190ed@web.de \
--to=markus.elfring@web.de \
--cc=Julia.Lawall@inria.fr \
--cc=cocci@inria.fr \
--cc=kernel-janitors@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nicolas.palix@imag.fr \
/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 read-only IMAP folder(s) and NNTP newsgroup(s).