|
6 | 6 |
|
7 | 7 | import cpp
|
8 | 8 |
|
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. */ |
10 | 23 | private predicate hasZeroParamDecl(Function f) {
|
11 | 24 | exists(FunctionDeclarationEntry fde | fde = f.getADeclarationEntry() |
|
12 |
| - not fde.hasVoidParamList() and fde.getNumberOfParameters() = 0 and not fde.isDefinition() |
| 25 | + not hasDefiniteNumberOfParameters(fde) |
13 | 26 | )
|
14 | 27 | }
|
15 | 28 |
|
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. */ |
17 | 30 | private predicate isCompiledAsC(File f) {
|
18 | 31 | f.compiledAsC()
|
19 | 32 | or
|
20 | 33 | exists(File src | isCompiledAsC(src) | src.getAnIncludedFile() = f)
|
21 | 34 | }
|
22 | 35 |
|
| 36 | +/** Holds if `fc` is a call to `f` with too few arguments. */ |
23 | 37 | predicate tooFewArguments(FunctionCall fc, Function f) {
|
24 | 38 | f = fc.getTarget() and
|
25 | 39 | not f.isVarargs() and
|
26 | 40 | 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. |
27 | 44 | hasZeroParamDecl(f) and
|
28 | 45 | 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 | + | |
32 | 53 | fde.getNumberOfParameters() > fc.getNumberOfArguments()
|
33 | 54 | )
|
34 | 55 | }
|
0 commit comments