Skip to content

Commit 6cf575d

Browse files
committed
Query and tests for sum without domain
1 parent 748387a commit 6cf575d

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-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"]
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+
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)