|
1 | 1 | import cpp
|
| 2 | +import codingstandards.cpp.Pointers |
2 | 3 | import codingstandards.cpp.UndefinedBehavior
|
3 | 4 |
|
4 | 5 | /**
|
5 | 6 | * Library for modeling undefined behavior.
|
6 | 7 | */
|
7 | 8 | abstract class CUndefinedBehavior extends UndefinedBehavior { }
|
8 | 9 |
|
| 10 | +/** |
| 11 | + * A function which has the signature - but not the name - of a main function. |
| 12 | + */ |
9 | 13 | class C99MainFunction extends Function {
|
10 | 14 | C99MainFunction() {
|
11 | 15 | this.getNumberOfParameters() = 2 and
|
12 |
| - this.getType() instanceof IntType and |
13 |
| - this.getParameter(0).getType() instanceof IntType and |
14 |
| - this.getParameter(1).getType().(PointerType).getBaseType().(PointerType).getBaseType() |
15 |
| - instanceof CharType |
| 16 | + this.getType().getUnderlyingType() instanceof IntType and |
| 17 | + this.getParameter(0).getType().getUnderlyingType() instanceof IntType and |
| 18 | + this.getParameter(1) |
| 19 | + .getType() |
| 20 | + .getUnderlyingType() |
| 21 | + .(UnspecifiedPointerOrArrayType) |
| 22 | + .getBaseType() |
| 23 | + .(UnspecifiedPointerOrArrayType) |
| 24 | + .getBaseType() instanceof CharType |
16 | 25 | or
|
17 | 26 | this.getNumberOfParameters() = 0 and
|
18 |
| - this.getType() instanceof VoidType |
| 27 | + // Must be explicitly declared as `int main(void)`. |
| 28 | + this.getADeclarationEntry().hasVoidParamList() and |
| 29 | + this.getType().getUnderlyingType() instanceof IntType |
19 | 30 | }
|
20 | 31 | }
|
21 | 32 |
|
22 | 33 | class CUndefinedMainDefinition extends CUndefinedBehavior, Function {
|
23 | 34 | CUndefinedMainDefinition() {
|
24 | 35 | // for testing purposes, we use the prefix ____codeql_coding_standards`
|
25 |
| - (this.getName() = "main" or this.getName().indexOf("____codeql_coding_standards") = 0) and |
| 36 | + (this.getName() = "main" or this.getName().indexOf("____codeql_coding_standards_main") = 0) and |
26 | 37 | not this instanceof C99MainFunction
|
27 | 38 | }
|
28 | 39 |
|
29 | 40 | override string getReason() {
|
30 | 41 | result =
|
31 |
| - "The behavior of the program is undefined because the main function is not defined according to the C standard." |
| 42 | + "main function may trigger undefined behavior because it is not in one of the formats specified by the C standard." |
32 | 43 | }
|
33 | 44 | }
|
0 commit comments