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
61 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,36 @@ | ||
/** | ||
* @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 path-problem | ||
* @id cpp/redundant-assignment | ||
*/ | ||
|
||
import cpp | ||
import semmle.code.cpp.dataflow.DataFlow | ||
import DataFlow::PathGraph | ||
|
||
class RedundantAssignmentConfiguration extends DataFlow::Configuration { | ||
RedundantAssignmentConfiguration() { this = "RedundantAssignmentConfiguration" } | ||
|
||
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 RedundantAssignmentConfiguration config, DataFlow::PathNode source, DataFlow::PathNode sink, Variable v1, Variable v2 | ||
where config.hasFlowPath(source, sink) and | ||
source.getNode().asExpr() = v1.getAnAccess() and | ||
sink.getNode().asExpr() = v2.getAnAccess() and | ||
v1 = v2 | ||
select sink, source, sink, "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