devtools/cocci: change boolean negation to bitwise negation

Message ID tencent_87824F07FB7B648445042BE686E7FE542205@qq.com (mailing list archive)
State Rejected, archived
Delegated to: Thomas Monjalon
Headers
Series devtools/cocci: change boolean negation to bitwise negation |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/github-robot: build success github build: passed
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-abi-testing success Testing PASS

Commit Message

Weiguo Li March 4, 2022, 3:31 p.m. UTC
  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
  

Comments

Stephen Hemminger July 6, 2023, 6:02 p.m. UTC | #1
On Fri,  4 Mar 2022 23:31:53 +0800
Weiguo Li <liwg06@foxmail.com> wrote:

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

This never got merged probably because it address something that is not
happening in DPDK. Ran the script and it found nothing.

Also, script adds empty blank line at EOF which cause a warning
when merging in git.

Dropping this patch as not needed.
  

Patch

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