Skip to content

Commit 1dd95a6

Browse files
authored
Merge pull request github#12292 from github/calumgrant/aggregate-domain
Query and tests for sum without domain
2 parents 25043f5 + 10aad99 commit 1dd95a6

File tree

5 files changed

+26
-1
lines changed

5 files changed

+26
-1
lines changed

ql/ql/src/codeql_ql/ast/Ast.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1808,7 +1808,7 @@ class FullAggregate extends TFullAggregate, Aggregate {
18081808

18091809
/**
18101810
* Gets the kind of aggregate.
1811-
* E.g. for `min(int i | foo(i))` the result is "foo".
1811+
* E.g. for `min(int i | foo(i))` the result is "min".
18121812
*/
18131813
override string getKind() { result = kind }
18141814

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @name Sum is missing a domain
3+
* @description An aggregate like 'sum' should work over a domain, otherwise duplicate values will not be counted.
4+
* @kind problem
5+
* @problem.severity error
6+
* @id ql/sum-missing-domain
7+
* @tags correctness
8+
* @precision medium
9+
*/
10+
11+
import ql
12+
13+
from ExprAggregate agg
14+
where agg.getKind() = ["sum", "strictsum", "avg"]
15+
select agg,
16+
"This " + agg.getKind() + " does not have a domain argument, so may produce surprising results."
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| Test.qll:3:12:3:25 | ExprAggregate[sum] | This sum does not have a domain argument, so may produce surprising results. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/bugs/SumWithoutDomain.ql
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Result is 3 and not 4
2+
int foo() {
3+
result = sum([1, 1, 2]) // <- Alert here
4+
}
5+
6+
// Ok - false negative
7+
predicate bar() { sum(int x | x = [1, 1, 2] | x) = 3 }

0 commit comments

Comments
 (0)