Skip to content

Commit 64aefc6

Browse files
authored
Merge pull request github#3554 from jbj/too-few-arguments-ambiguous
Approved by dbartol
2 parents 5c20d56 + b4c32a0 commit 64aefc6

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,50 @@
66

77
import cpp
88

9-
// True if function was ()-declared, but not (void)-declared or K&R-defined
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+
22+
/* Holds 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

16-
// True if this file (or header) was compiled as a C file
29+
/* Holds if this file (or header) was compiled as a C file. */
1730
private predicate isCompiledAsC(File f) {
1831
f.compiledAsC()
1932
or
2033
exists(File src | isCompiledAsC(src) | src.getAnIncludedFile() = f)
2134
}
2235

36+
/** Holds if `fc` is a call to `f` with too few arguments. */
2337
predicate tooFewArguments(FunctionCall fc, Function f) {
2438
f = fc.getTarget() and
2539
not f.isVarargs() and
2640
not f instanceof BuiltInFunction and
41+
// This query should only have results on C (not C++) functions that have a
42+
// `()` parameter list somewhere. If it has results on other functions, then
43+
// it's probably because the extractor only saw a partial compilation.
2744
hasZeroParamDecl(f) and
2845
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() |
46+
// Produce an alert when all declarations that are authoritative on the
47+
// parameter count specify a parameter count larger than the number of call
48+
// arguments.
49+
forex(FunctionDeclarationEntry fde |
50+
fde = f.getADeclarationEntry() and
51+
hasDefiniteNumberOfParameters(fde)
52+
|
3253
fde.getNumberOfParameters() > fc.getNumberOfArguments()
3354
)
3455
}

0 commit comments

Comments
 (0)