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
76 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,51 @@ | ||
/** | ||
* @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 | ||
* @severity warning | ||
*/ | ||
|
||
import cpp | ||
import semmle.code.cpp.dataflow.DataFlow | ||
|
||
class RedundantAssignmentConfig extends DataFlow::Configuration { | ||
RedundantAssignmentConfig() { this = "RedundantAssignmentConfig" } | ||
|
||
override predicate isSource(DataFlow::Node source) { | ||
exists(Assignment assign | | ||
assign = source.asExpr() and | ||
assign.getRValue().(VariableAccess).getTarget() instanceof Variable | ||
) | ||
} | ||
|
||
override predicate isSink(DataFlow::Node sink) { | ||
exists(Assignment assign | | ||
assign = sink.asExpr() and | ||
assign.getLValue().(VariableAccess).getTarget() instanceof Variable | ||
) | ||
} | ||
} | ||
|
||
from Variable v1, Field f, Assignment assign1, Assignment assign2, RedundantAssignmentConfig config | ||
where | ||
// First assignment from v1 to a struct field f | ||
assign1.getLValue().(FieldAccess).getTarget() = f and | ||
assign1.getRValue().(VariableAccess).getTarget() = v1 and | ||
// Second assignment from the struct field f back to v1 | ||
assign2.getLValue().(VariableAccess).getTarget() = v1 and | ||
assign2.getRValue().(FieldAccess).getTarget() = f and | ||
// Check for data flow between the assignments | ||
config.hasFlow(DataFlow::exprNode(assign1.getRValue()), DataFlow::exprNode(assign2.getRValue())) and | ||
// Ensure there is no modification to v1 and f 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().(FieldAccess).getTarget() = f | ||
) | ||
) | ||
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