Skip to content

Commit 6c7c9b6

Browse files
authored
Merge pull request #9082 from erik-krogh/countZero
QL: add query warning about `count(...) = 0`.
2 parents 53f32f5 + f5329a3 commit 6c7c9b6

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

python/ql/src/Security/CWE-022/TarSlip.ql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ class ExcludeTarFilePy extends Sanitizer {
8181

8282
/* Any call to an extractall method */
8383
class ExtractAllSink extends TaintSink {
84-
CallNode call;
85-
8684
ExtractAllSink() {
87-
this = call.getFunction().(AttrNode).getObject("extractall") and
88-
count(call.getAnArg()) = 0
85+
exists(CallNode call |
86+
this = call.getFunction().(AttrNode).getObject("extractall") and
87+
not exists(call.getAnArg())
88+
)
8989
}
9090

9191
override predicate sinks(TaintKind kind) { kind instanceof OpenTarFile }

python/ql/src/analysis/Consistency.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ predicate function_object_consistency(string clsname, string problem, string wha
194194
exists(FunctionObject func | clsname = func.getAQlClass() |
195195
what = func.getName() and
196196
(
197-
count(func.descriptiveString()) = 0 and problem = "no descriptiveString()"
197+
not exists(func.descriptiveString()) and problem = "no descriptiveString()"
198198
or
199199
exists(int c | c = strictcount(func.descriptiveString()) and c > 1 |
200200
problem = c + "descriptiveString()s"

ql/ql/src/queries/performance/ClassPredicateDoesntMentionThis.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ predicate isLiteralComparison(ComparisonFormula eq) {
4040
or
4141
exists(NewTypeBranch nt |
4242
rhs.(Call).getTarget() = nt and
43-
count(nt.getField(_)) = 0
43+
not exists(nt.getField(_))
4444
)
4545
)
4646
)
@@ -69,7 +69,7 @@ predicate isSingleton(Type ty) {
6969
or
7070
isSingleton(ty.getASuperType())
7171
or
72-
exists(NewTypeBranch br | count(br.getField(_)) = 0 |
72+
exists(NewTypeBranch br | not exists(br.getField(_)) |
7373
ty.(NewTypeBranchType).getDeclaration() = br
7474
or
7575
br = unique(NewTypeBranch br2 | br2 = ty.(NewTypeType).getDeclaration().getABranch())
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @name Counting zero elements
3+
* @description Use not exists instead of checking that there is zero elements in a set.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @id ql/counting-to-zero
7+
* @precision high
8+
*/
9+
10+
import ql
11+
12+
from ComparisonFormula comp, int c, string msg
13+
where
14+
c = 0 and // [0, 1] and // skipping the 1 case for now, as it seems too noisy.
15+
comp.getOperator() = ["=", "!="] and
16+
comp.getAnOperand().(Integer).getValue() = c and
17+
comp.getAnOperand().(Aggregate).getKind() = ["count", "strictcount"] and
18+
if c = 0
19+
then msg = "Use not exists(..) instead of checking that there is zero elements in a set."
20+
else msg = "Use unique(.. | |) instead of checking that there is one element in a set."
21+
select comp, msg

0 commit comments

Comments
 (0)