Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 394a864

Browse files
committed
C++: Factored the body of TooManyArguments.ql out into a library file
1 parent 6cff36b commit 394a864

2 files changed

Lines changed: 40 additions & 29 deletions

File tree

cpp/ql/src/Likely Bugs/Underspecified Functions/TooManyArguments.ql

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,8 @@
1212
*/
1313

1414
import cpp
15-
16-
// True if function was ()-declared, but not (void)-declared or K&R-defined
17-
// or implicitly declared (i.e., lacking a prototype)
18-
predicate hasZeroParamDecl(Function f) {
19-
exists(FunctionDeclarationEntry fde | fde = f.getADeclarationEntry() |
20-
not fde.isImplicit() and
21-
not fde.hasVoidParamList() and
22-
fde.getNumberOfParameters() = 0 and
23-
not fde.isDefinition()
24-
)
25-
}
26-
27-
// True if this file (or header) was compiled as a C file
28-
predicate isCompiledAsC(File f) {
29-
f.compiledAsC()
30-
or
31-
exists(File src | isCompiledAsC(src) | src.getAnIncludedFile() = f)
32-
}
15+
import TooManyArguments
3316

3417
from FunctionCall fc, Function f
35-
where
36-
f = fc.getTarget() and
37-
not f.isVarargs() and
38-
hasZeroParamDecl(f) and
39-
isCompiledAsC(f.getFile()) and
40-
exists(f.getBlock()) and
41-
// There must not exist a declaration with the number of parameters
42-
// at least as large as the number of call arguments
43-
not exists(FunctionDeclarationEntry fde | fde = f.getADeclarationEntry() |
44-
fde.getNumberOfParameters() >= fc.getNumberOfArguments()
45-
)
18+
where tooManyArguments(fc, f)
4619
select fc, "This call has more arguments than required by $@.", f, f.toString()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Provides the implementation of the TooManyArguments query. The
3+
* query is implemented as a library, so that we can avoid producing
4+
* duplicate results in other similar queries.
5+
*/
6+
7+
import cpp
8+
9+
// True if function was ()-declared, but not (void)-declared or K&R-defined
10+
// or implicitly declared (i.e., lacking a prototype)
11+
private predicate hasZeroParamDecl(Function f) {
12+
exists(FunctionDeclarationEntry fde | fde = f.getADeclarationEntry() |
13+
not fde.isImplicit() and
14+
not fde.hasVoidParamList() and
15+
fde.getNumberOfParameters() = 0 and
16+
not fde.isDefinition()
17+
)
18+
}
19+
20+
// True if this file (or header) was compiled as a C file
21+
private predicate isCompiledAsC(File f) {
22+
f.compiledAsC()
23+
or
24+
exists(File src | isCompiledAsC(src) | src.getAnIncludedFile() = f)
25+
}
26+
27+
predicate tooManyArguments(FunctionCall fc, Function f) {
28+
f = fc.getTarget() and
29+
not f.isVarargs() and
30+
hasZeroParamDecl(f) and
31+
isCompiledAsC(f.getFile()) and
32+
exists(f.getBlock()) and
33+
// There must not exist a declaration with the number of parameters
34+
// at least as large as the number of call arguments
35+
not exists(FunctionDeclarationEntry fde | fde = f.getADeclarationEntry() |
36+
fde.getNumberOfParameters() >= fc.getNumberOfArguments()
37+
)
38+
}

0 commit comments

Comments
 (0)