Skip to content

Commit f56e337

Browse files
authored
Merge pull request github#18164 from Napalys/napalys/ql-validate-predicate-get-returns
Add query to ensure predicates starting with 'get' return a value
2 parents 012ea4b + 7db9b7d commit f56e337

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @name Predicates starting with "get" or "as" should return a value
3+
* @description Checks if predicates that start with "get" or "as" actually return a value.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @id ql/predicates-get-should-return-value
7+
* @tags correctness
8+
* maintainability
9+
* @precision high
10+
*/
11+
12+
import ql
13+
import codeql_ql.ast.Ast
14+
15+
/**
16+
* Identifies predicates whose names start with "get", "as" followed by an uppercase letter.
17+
* This ensures that only predicates like "getValue" are matched, excluding names like "getter".
18+
*/
19+
predicate isGetPredicate(Predicate pred, string prefix) {
20+
prefix = pred.getName().regexpCapture("(get|as)[A-Z].*", 1)
21+
}
22+
23+
/**
24+
* Checks if a predicate has a return type. This is phrased negatively to not flag unresolved aliases.
25+
*/
26+
predicate hasNoReturnType(Predicate pred) {
27+
not exists(pred.getReturnTypeExpr()) and
28+
not pred.(ClasslessPredicate).getAlias() instanceof PredicateExpr
29+
or
30+
hasNoReturnType(pred.(ClasslessPredicate).getAlias().(PredicateExpr).getResolvedPredicate())
31+
}
32+
33+
from Predicate pred, string prefix
34+
where
35+
isGetPredicate(pred, prefix) and
36+
hasNoReturnType(pred)
37+
select pred, "This predicate starts with '" + prefix + "' but does not return a value."
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
| test.qll:4:11:4:18 | ClasslessPredicate getValue | This predicate starts with 'get' but does not return a value. |
2+
| test.qll:25:11:25:28 | ClasslessPredicate getImplementation2 | This predicate starts with 'get' but does not return a value. |
3+
| test.qll:28:11:28:19 | ClasslessPredicate getAlias2 | This predicate starts with 'get' but does not return a value. |
4+
| test.qll:31:11:31:17 | ClasslessPredicate asValue | This predicate starts with 'as' but does not return a value. |
5+
| test.qll:48:11:48:19 | ClasslessPredicate getAlias4 | This predicate starts with 'get' but does not return a value. |
6+
| test.qll:61:11:61:22 | ClasslessPredicate getDistance2 | This predicate starts with 'get' but does not return a value. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/style/ValidatePredicateGetReturns.ql
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import ql
2+
3+
// NOT OK -- Predicate starts with "get" but does not return a value
4+
predicate getValue() { none() }
5+
6+
// OK -- starts with get and returns a value
7+
string getData() { result = "data" }
8+
9+
// OK -- starts with get but followed by a lowercase letter, probably should be ignored
10+
predicate getterFunction() { none() }
11+
12+
// OK -- starts with get and returns a value
13+
string getImplementation() { result = "implementation" }
14+
15+
// OK -- is an alias
16+
predicate getAlias = getImplementation/0;
17+
18+
// OK -- Starts with "get" but followed by a lowercase letter, probably be ignored
19+
predicate getvalue() { none() }
20+
21+
// OK -- Does not start with "get", should be ignored
22+
predicate retrieveValue() { none() }
23+
24+
// NOT OK -- starts with get and does not return value
25+
predicate getImplementation2() { none() }
26+
27+
// NOT OK -- is an alias for a predicate which does not have a return value
28+
predicate getAlias2 = getImplementation2/0;
29+
30+
// NOT OK -- starts with as and does not return value
31+
predicate asValue() { none() }
32+
33+
// OK -- starts with as but followed by a lowercase letter, probably should be ignored
34+
predicate assessment() { none() }
35+
36+
// OK -- starts with as and returns a value
37+
string asString() { result = "string" }
38+
39+
// OK -- starts with get and returns a value
40+
HiddenType getInjectableCompositeActionNode() {
41+
exists(HiddenType hidden | result = hidden.toString())
42+
}
43+
44+
// OK
45+
predicate implementation4() { none() }
46+
47+
// NOT OK -- is an alias
48+
predicate getAlias4 = implementation4/0;
49+
50+
// OK -- is an alias
51+
predicate alias5 = implementation4/0;
52+
53+
int root() { none() }
54+
55+
predicate edge(int x, int y) { none() }
56+
57+
// OK -- Higher-order predicate
58+
int getDistance(int x) = shortestDistances(root/0, edge/2)(_, x, result)
59+
60+
// NOT OK -- Higher-order predicate that does not return a value even though has 'get' in the name
61+
predicate getDistance2(int x, int y) = shortestDistances(root/0, edge/2)(_, x, y)
62+
63+
// OK
64+
predicate unresolvedAlias = unresolved/0;
65+
66+
// OK -- unresolved alias
67+
predicate getUnresolvedAlias = unresolved/0;

0 commit comments

Comments
 (0)