DPDK patches and discussions
 help / color / mirror / Atom feed
* [PATCH] devtools/cocci: change boolean negation to bitwise negation
@ 2022-03-04 15:31 Weiguo Li
  2023-07-06 18:02 ` Stephen Hemminger
  0 siblings, 1 reply; 2+ messages in thread
From: Weiguo Li @ 2022-03-04 15:31 UTC (permalink / raw)
  To: dev, stephen; +Cc: ferruh.yigit

Combining boolean operator with bitwise operator is suspicious. When
this happens, it has a chance that the bitwise negation operation is
mistakenly written as a boolean negation operation. This script is
used to find this kind of problems.

example:
	if (x & !BITS_MASK)
changed to:
	if (x & ~BITS_MASK)

The idea came from a demo script in coccinelle website:
https://coccinelle.gitlabpages.inria.fr/website/rules/notand.html

Signed-off-by: Weiguo Li <liwg06@foxmail.com>
---
 devtools/cocci/bitwise_negation.cocci | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100644 devtools/cocci/bitwise_negation.cocci

diff --git a/devtools/cocci/bitwise_negation.cocci b/devtools/cocci/bitwise_negation.cocci
new file mode 100644
index 0000000000..78fc677e4e
--- /dev/null
+++ b/devtools/cocci/bitwise_negation.cocci
@@ -0,0 +1,18 @@
+//
+// The bitwise negation operation is easy to be mistakenly written as a boolean
+// negation operation, this script is used to find this kind of problem.
+//
+// Note: If it is confirmed to be a boolean negation operation, it is recommended
+// that change & to && to avoid false positives.
+//
+@@ expression E; constant C; @@
+(
+  !E & !C
+|
+- !E & C
++ !(E & C)
+|
+- E & !C
++ E & ~C
+)
+
-- 
2.25.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-07-06 18:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-04 15:31 [PATCH] devtools/cocci: change boolean negation to bitwise negation Weiguo Li
2023-07-06 18:02 ` Stephen Hemminger

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).