Skip to content

Commit

Permalink
Add CodeQL query to detect redundant assignments
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
  • Loading branch information
ryao committed Jan 24, 2024
1 parent ac944f0 commit dd00447
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/codeql-cpp.yml
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
4 changes: 4 additions & 0 deletions .github/codeql-python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: "Custom CodeQL Analysis"

paths-ignore:
- tests
4 changes: 4 additions & 0 deletions .github/codeql/custom-queries/cpp/qlpack.yml
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
8 changes: 8 additions & 0 deletions .github/codeql/custom-queries/cpp/redundantAssignment.c
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);
}
55 changes: 55 additions & 0 deletions .github/codeql/custom-queries/cpp/redundantAssignment.ql
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."

3 changes: 3 additions & 0 deletions .github/codeql/openzfs-code-scanning.qls
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
1 change: 1 addition & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
config-file: .github/codeql-${{ matrix.language }}.yml
languages: ${{ matrix.language }}

- name: Autobuild
Expand Down

0 comments on commit dd00447

Please sign in to comment.