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
80 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,55 @@ | ||
/** | ||
* @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 | ||
*/ | ||
|
||
/** | ||
* @name Mutual assignment detection | ||
* @description Detects mutual assignments between variables and structure fields. | ||
* @kind problem | ||
* @id cpp/mutual-assignment | ||
* @severity warning | ||
*/ | ||
|
||
import cpp | ||
import semmle.code.cpp.dataflow.DataFlow | ||
|
||
class MutualAssignmentConfig extends DataFlow::Configuration { | ||
MutualAssignmentConfig() { this = "MutualAssignmentConfig" } | ||
|
||
override predicate isSource(DataFlow::Node source) { | ||
exists(Assignment assign | | ||
assign = source.asExpr() and | ||
( | ||
assign.getRValue() instanceof VariableAccess or | ||
assign.getRValue() instanceof FieldAccess | ||
) | ||
) | ||
} | ||
|
||
override predicate isSink(DataFlow::Node sink) { | ||
exists(Assignment assign | | ||
assign = sink.asExpr() and | ||
( | ||
assign.getLValue() instanceof VariableAccess or | ||
assign.getLValue() instanceof FieldAccess | ||
) | ||
) | ||
} | ||
} | ||
|
||
from MutualAssignmentConfig config, Assignment assign1, Assignment assign2 | ||
where | ||
config.hasFlow(DataFlow::exprNode(assign1.getRValue()), DataFlow::exprNode(assign2.getRValue())) and | ||
assign1.getLValue() = assign2.getRValue() and | ||
assign2.getLValue() = assign1.getRValue() and | ||
not exists(FunctionCall fc | | ||
fc.getEnclosingFunction() = assign1.getEnclosingFunction() and | ||
fc.getArgument(0).getFullyConverted().(VariableAccess).getTarget() = assign1.getLValue().(VariableAccess).getTarget() and | ||
fc.getLocation().isBetween(assign1.getLocation(), assign2.getLocation()) | ||
) | ||
select assign2, "This assignment to " + assign2.getLValue().toString() + " is potentially 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