-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into redsun82/rust-tweaks
- Loading branch information
Showing
19 changed files
with
340 additions
and
141 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
Validating CODEOWNERS rules …
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
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
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,37 @@ | ||
/** | ||
* @name Predicates starting with "get" or "as" should return a value | ||
* @description Checks if predicates that start with "get" or "as" actually return a value. | ||
* @kind problem | ||
* @problem.severity warning | ||
* @id ql/predicates-get-should-return-value | ||
* @tags correctness | ||
* maintainability | ||
* @precision high | ||
*/ | ||
|
||
import ql | ||
import codeql_ql.ast.Ast | ||
|
||
/** | ||
* Identifies predicates whose names start with "get", "as" followed by an uppercase letter. | ||
* This ensures that only predicates like "getValue" are matched, excluding names like "getter". | ||
*/ | ||
predicate isGetPredicate(Predicate pred, string prefix) { | ||
prefix = pred.getName().regexpCapture("(get|as)[A-Z].*", 1) | ||
} | ||
|
||
/** | ||
* Checks if a predicate has a return type. This is phrased negatively to not flag unresolved aliases. | ||
*/ | ||
predicate hasNoReturnType(Predicate pred) { | ||
not exists(pred.getReturnTypeExpr()) and | ||
not pred.(ClasslessPredicate).getAlias() instanceof PredicateExpr | ||
or | ||
hasNoReturnType(pred.(ClasslessPredicate).getAlias().(PredicateExpr).getResolvedPredicate()) | ||
} | ||
|
||
from Predicate pred, string prefix | ||
where | ||
isGetPredicate(pred, prefix) and | ||
hasNoReturnType(pred) | ||
select pred, "This predicate starts with '" + prefix + "' but does not return a value." |
6 changes: 6 additions & 0 deletions
6
ql/ql/test/queries/style/ValidatePredicateGetReturns/ValidatePredicateGetReturns.expected
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,6 @@ | ||
| test.qll:4:11:4:18 | ClasslessPredicate getValue | This predicate starts with 'get' but does not return a value. | | ||
| test.qll:25:11:25:28 | ClasslessPredicate getImplementation2 | This predicate starts with 'get' but does not return a value. | | ||
| test.qll:28:11:28:19 | ClasslessPredicate getAlias2 | This predicate starts with 'get' but does not return a value. | | ||
| test.qll:31:11:31:17 | ClasslessPredicate asValue | This predicate starts with 'as' but does not return a value. | | ||
| test.qll:48:11:48:19 | ClasslessPredicate getAlias4 | This predicate starts with 'get' but does not return a value. | | ||
| test.qll:61:11:61:22 | ClasslessPredicate getDistance2 | This predicate starts with 'get' but does not return a value. | |
1 change: 1 addition & 0 deletions
1
ql/ql/test/queries/style/ValidatePredicateGetReturns/ValidatePredicateGetReturns.qlref
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 @@ | ||
queries/style/ValidatePredicateGetReturns.ql |
67 changes: 67 additions & 0 deletions
67
ql/ql/test/queries/style/ValidatePredicateGetReturns/test.qll
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,67 @@ | ||
import ql | ||
|
||
// NOT OK -- Predicate starts with "get" but does not return a value | ||
predicate getValue() { none() } | ||
|
||
// OK -- starts with get and returns a value | ||
string getData() { result = "data" } | ||
|
||
// OK -- starts with get but followed by a lowercase letter, probably should be ignored | ||
predicate getterFunction() { none() } | ||
|
||
// OK -- starts with get and returns a value | ||
string getImplementation() { result = "implementation" } | ||
|
||
// OK -- is an alias | ||
predicate getAlias = getImplementation/0; | ||
|
||
// OK -- Starts with "get" but followed by a lowercase letter, probably be ignored | ||
predicate getvalue() { none() } | ||
|
||
// OK -- Does not start with "get", should be ignored | ||
predicate retrieveValue() { none() } | ||
|
||
// NOT OK -- starts with get and does not return value | ||
predicate getImplementation2() { none() } | ||
|
||
// NOT OK -- is an alias for a predicate which does not have a return value | ||
predicate getAlias2 = getImplementation2/0; | ||
|
||
// NOT OK -- starts with as and does not return value | ||
predicate asValue() { none() } | ||
|
||
// OK -- starts with as but followed by a lowercase letter, probably should be ignored | ||
predicate assessment() { none() } | ||
|
||
// OK -- starts with as and returns a value | ||
string asString() { result = "string" } | ||
|
||
// OK -- starts with get and returns a value | ||
HiddenType getInjectableCompositeActionNode() { | ||
exists(HiddenType hidden | result = hidden.toString()) | ||
} | ||
|
||
// OK | ||
predicate implementation4() { none() } | ||
|
||
// NOT OK -- is an alias | ||
predicate getAlias4 = implementation4/0; | ||
|
||
// OK -- is an alias | ||
predicate alias5 = implementation4/0; | ||
|
||
int root() { none() } | ||
|
||
predicate edge(int x, int y) { none() } | ||
|
||
// OK -- Higher-order predicate | ||
int getDistance(int x) = shortestDistances(root/0, edge/2)(_, x, result) | ||
|
||
// NOT OK -- Higher-order predicate that does not return a value even though has 'get' in the name | ||
predicate getDistance2(int x, int y) = shortestDistances(root/0, edge/2)(_, x, y) | ||
|
||
// OK | ||
predicate unresolvedAlias = unresolved/0; | ||
|
||
// OK -- unresolved alias | ||
predicate getUnresolvedAlias = unresolved/0; |
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
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
17 changes: 17 additions & 0 deletions
17
rust/ql/lib/codeql/rust/dataflow/internal/DataFlowConsistency.qll
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,17 @@ | ||
import codeql.rust.dataflow.DataFlow::DataFlow as DataFlow | ||
private import rust | ||
private import codeql.rust.dataflow.internal.DataFlowImpl | ||
private import codeql.rust.dataflow.internal.TaintTrackingImpl | ||
private import codeql.dataflow.internal.DataFlowImplConsistency | ||
|
||
private module Input implements InputSig<Location, RustDataFlow> { | ||
predicate uniqueNodeLocationExclude(RustDataFlow::Node n) { | ||
// Exclude nodes where the missing location can be explained by the | ||
// underlying AST node not having a location. | ||
not exists(n.asExpr().getLocation()) | ||
} | ||
|
||
predicate missingLocationExclude(RustDataFlow::Node n) { not exists(n.asExpr().getLocation()) } | ||
} | ||
|
||
import MakeConsistency<Location, RustDataFlow, RustTaintTracking, Input> |
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
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
42 changes: 42 additions & 0 deletions
42
rust/ql/test/extractor-tests/generated/MacroItems/CONSISTENCY/AstConsistency.expected
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,42 @@ | ||
noLocation | ||
| file://:0:0:0:0 | ... .parent(...) | | ||
| file://:0:0:0:0 | ... .unwrap(...) | | ||
| file://:0:0:0:0 | ...: ... | | ||
| file://:0:0:0:0 | ...::Path | | ||
| file://:0:0:0:0 | ...::path | | ||
| file://:0:0:0:0 | ArgList | | ||
| file://:0:0:0:0 | ArgList | | ||
| file://:0:0:0:0 | MacroItems | | ||
| file://:0:0:0:0 | ParamList | | ||
| file://:0:0:0:0 | Path | | ||
| file://:0:0:0:0 | Path | | ||
| file://:0:0:0:0 | Path | | ||
| file://:0:0:0:0 | Path | | ||
| file://:0:0:0:0 | Path | | ||
| file://:0:0:0:0 | Path | | ||
| file://:0:0:0:0 | Path | | ||
| file://:0:0:0:0 | Path | | ||
| file://:0:0:0:0 | Path | | ||
| file://:0:0:0:0 | Path | | ||
| file://:0:0:0:0 | RefType | | ||
| file://:0:0:0:0 | RefType | | ||
| file://:0:0:0:0 | RetType | | ||
| file://:0:0:0:0 | StmtList | | ||
| file://:0:0:0:0 | Use | | ||
| file://:0:0:0:0 | UseTree | | ||
| file://:0:0:0:0 | fn get_parent | | ||
| file://:0:0:0:0 | get_parent | | ||
| file://:0:0:0:0 | parent | | ||
| file://:0:0:0:0 | path | | ||
| file://:0:0:0:0 | path | | ||
| file://:0:0:0:0 | path | | ||
| file://:0:0:0:0 | path | | ||
| file://:0:0:0:0 | path | | ||
| file://:0:0:0:0 | path | | ||
| file://:0:0:0:0 | path | | ||
| file://:0:0:0:0 | path | | ||
| file://:0:0:0:0 | std | | ||
| file://:0:0:0:0 | std | | ||
| file://:0:0:0:0 | std | | ||
| file://:0:0:0:0 | unwrap | | ||
| file://:0:0:0:0 | { ... } | |
11 changes: 0 additions & 11 deletions
11
rust/ql/test/extractor-tests/generated/MacroItems/CONSISTENCY/DataFlowConsistency.expected
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
| Multiple parents | 0 | | ||
| Multiple primary QL classes | 0 | | ||
| Multiple toStrings | 0 | | ||
| No location | 0 | |
Oops, something went wrong.