forked from openzfs/zfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CodeQL query to detect redundant assignments
Signed-off-by: Richard Yao <[email protected]>
- Loading branch information
Showing
7 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name: "Custom CodeQL Analysis" | ||
|
||
queries: | ||
- uses: ./.github/codeql/custom-queries/cpp/redundantAssignment.ql | ||
# - uses: ./.github/codeql/openzfs-code-scanning.qls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
name: "Custom CodeQL Analysis" | ||
|
||
paths-ignore: | ||
- tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
name: openzfs-cpp-queries | ||
version: 0.0.0 | ||
libraryPathDependencies: codeql-cpp | ||
suites: openzfs-cpp-suite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
int | ||
main(void) { | ||
int a = 0; | ||
int b = a; | ||
int c = 1; | ||
a = b; | ||
return (a*b*c); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* @name Redundant assignment detection | ||
* @description Detects redundant assignments where a variable is assigned to another, and then the second variable is assigned back to the first without any intervening modification. | ||
* @kind problem | ||
* @id cpp/redundant-assignment | ||
*/ | ||
|
||
import cpp | ||
import semmle.code.cpp.dataflow.DataFlow | ||
|
||
from Variable v1, Variable v2, Assignment assign1, Assignment assign2 | ||
where | ||
// First assignment from v1 to v2 | ||
assign1.getLValue().(VariableAccess).getTarget() = v2 and | ||
assign1.getRValue().(VariableAccess).getTarget() = v1 and | ||
// Second assignment from v2 back to v1 | ||
assign2.getLValue().(VariableAccess).getTarget() = v1 and | ||
assign2.getRValue().(VariableAccess).getTarget() = v2 and | ||
// Check for data flow between the assignments | ||
exists(DataFlow::Node node1, DataFlow::Node node2 | | ||
node1.asExpr() = assign1.getRValue() and | ||
node2.asExpr() = assign2.getRValue() and | ||
DataFlow::localFlow(node1, node2) | ||
) and | ||
// Ensure there is no modification to v1 and v2 in between | ||
not exists(Assignment modAssign | | ||
modAssign.getEnclosingFunction() = assign1.getEnclosingFunction() and | ||
modAssign.getLocation().getFile() = assign1.getLocation().getFile() and | ||
modAssign.getLocation().getStartLine() > assign1.getLocation().getStartLine() and | ||
modAssign.getLocation().getStartLine() < assign2.getLocation().getStartLine() and | ||
( | ||
modAssign.getLValue().(VariableAccess).getTarget() = v1 or | ||
modAssign.getLValue().(VariableAccess).getTarget() = v2 | ||
) | ||
) | ||
select assign2, "This assignment to " + v1.getName() + " is redundant." | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Reusing existing QL Pack | ||
- import: codeql-suites/cpp-code-scanning.qls | ||
from: codeql-cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters