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 <[email protected]>
  • Loading branch information
ryao committed Jan 25, 2024
1 parent d9cb42d commit ee8b7a2
Show file tree
Hide file tree
Showing 6 changed files with 67 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/deprecatedFunctionUsage.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
50 changes: 50 additions & 0 deletions .github/codeql/custom-queries/cpp/deprecatedFunctionUsage.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @name Deprecated function usage detection
* @description Detects functions whose usage is banned from the OpenZFS
* codebase due to QA concerns.
* @kind problem
* @severity error
* @id cpp/deprecated-function-usage
*/

import cpp

predicate isDeprecatedFunction(Function f) {
f.getName() = "strtok" or
f.getName() = "__xpg_basename" or
f.getName() = "basename" or
f.getName() = "dirname" or
f.getName() = "bcopy" or
f.getName() = "bcmp" or
f.getName() = "bzero" or
f.getName() = "asctime" or
f.getName() = "asctime_r"
}

string getReplacementMessage(Function f) {
if f.getName() = "strtok" then
result = "strtok_r(3)"
else if f.getName() = "__xpg_basename" then
result = "zfs_basename()"
else if f.getName() = "basename" then
result = "zfs_basename()"
else if f.getName() = "dirname" then
result = "zfs_dirnamelen()"
else if f.getName() = "bcopy" then
result = "memcpy(3)/memmove(3)"
else if f.getName() = "bcmp" then
result = "memcmp(3)"
else if f.getName() = "bzero" then
result = "memset(3)"
else if f.getName() = "asctime" then
result = "strftime(3)"
else
result = "strftime(3)"
}

from FunctionCall fc, Function f
where
fc.getTarget() = f and
isDeprecatedFunction(f)
select fc, "Usage of '" + f.getName() + "' is deprecated, consider using '" +
getReplacementMessage(f) + "' instead."
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
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 ee8b7a2

Please sign in to comment.