Skip to content

Commit b1edc1d

Browse files
committed
C++: Only give alert when no def fits arg count
The `cpp/too-few-arguments` query produced alerts for ambiguous databases where a function had multiple possible declarations, with some declarations having the right number of parameters and some having too many. With this change, the query errs on the side of caution in those cases and does not produce an alert. This fixes false positives on racket/racket. The new `hasDefiniteNumberOfParameters` is exactly the negation of the old `hasZeroParamDecl`.
1 parent 0d75c6a commit b1edc1d

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

cpp/ql/src/Likely Bugs/Underspecified Functions/TooFewArguments.qll

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,23 @@
66

77
import cpp
88

9+
/**
10+
* Holds if `fde` has a parameter declaration that's clear on the minimum
11+
* number of parameters. This is essentially true for everything except
12+
* `()`-declarations.
13+
*/
14+
private predicate hasDefiniteNumberOfParameters(FunctionDeclarationEntry fde) {
15+
fde.hasVoidParamList()
16+
or
17+
fde.getNumberOfParameters() > 0
18+
or
19+
fde.isDefinition()
20+
}
21+
922
// True if function was ()-declared, but not (void)-declared or K&R-defined
1023
private predicate hasZeroParamDecl(Function f) {
1124
exists(FunctionDeclarationEntry fde | fde = f.getADeclarationEntry() |
12-
not fde.hasVoidParamList() and fde.getNumberOfParameters() = 0 and not fde.isDefinition()
25+
not hasDefiniteNumberOfParameters(fde)
1326
)
1427
}
1528

@@ -24,11 +37,18 @@ predicate tooFewArguments(FunctionCall fc, Function f) {
2437
f = fc.getTarget() and
2538
not f.isVarargs() and
2639
not f instanceof BuiltInFunction and
40+
// This query should only have results on C (not C++) functions that have a
41+
// `()` parameter list somewhere. If it has results on other functions, then
42+
// it's probably because the extractor only saw a partial compilation.
2743
hasZeroParamDecl(f) and
2844
isCompiledAsC(f.getFile()) and
29-
// There is an explicit declaration of the function whose parameter count is larger
30-
// than the number of call arguments
31-
exists(FunctionDeclarationEntry fde | fde = f.getADeclarationEntry() |
45+
// Produce an alert when all declarations that are authoritative on the
46+
// parameter count specify a parameter count larger than the number of call
47+
// arguments.
48+
forex(FunctionDeclarationEntry fde |
49+
fde = f.getADeclarationEntry() and
50+
hasDefiniteNumberOfParameters(fde)
51+
|
3252
fde.getNumberOfParameters() > fc.getNumberOfArguments()
3353
)
3454
}

0 commit comments

Comments
 (0)