Skip to content

Commit f2aeea5

Browse files
committed
1-3
1 parent 4148a57 commit f2aeea5

File tree

6 files changed

+72
-6
lines changed

6 files changed

+72
-6
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import cpp
2+
import codingstandards.cpp.UndefinedBehavior
3+
4+
/**
5+
* Library for modeling undefined behavior.
6+
*/
7+
abstract class CUndefinedBehavior extends UndefinedBehavior { }
8+
9+
class C99MainFunction extends Function {
10+
C99MainFunction() {
11+
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+
or
17+
this.getNumberOfParameters() = 0 and
18+
this.getType() instanceof VoidType
19+
}
20+
}
21+
22+
class CUndefinedMainDefinition extends CUndefinedBehavior, Function {
23+
CUndefinedMainDefinition() {
24+
// for testing purposes, we use the prefix ____codeql_coding_standards`
25+
(this.getName() = "main" or this.getName().indexOf("____codeql_coding_standards") = 0) and
26+
not this instanceof C99MainFunction
27+
}
28+
}

c/misra/src/rules/RULE-1-3/OccurrenceOfUndefinedBehavior.ql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
import cpp
1515
import codingstandards.c.misra
16+
import codingstandards.c.UndefinedBehavior
1617

17-
from
18-
where
19-
not isExcluded(x, Language3Package::occurrenceOfUndefinedBehaviorQuery()) and
20-
select
18+
from CUndefinedBehavior c
19+
where not isExcluded(c, Language3Package::occurrenceOfUndefinedBehaviorQuery())
20+
select c, "May result in undefined behavior."
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
No expected results have yet been specified
1+
| test.c:8:6:8:35 | ____codeql_coding_standards_m2 | May result in undefined behavior. |
2+
| test.c:11:5:11:34 | ____codeql_coding_standards_m3 | May result in undefined behavior. |
3+
| test.c:15:5:15:34 | ____codeql_coding_standards_m4 | May result in undefined behavior. |
4+
| test.c:19:5:19:34 | ____codeql_coding_standards_m5 | May result in undefined behavior. |
5+
| test.c:23:5:23:34 | ____codeql_coding_standards_m6 | May result in undefined behavior. |

c/misra/test/rules/RULE-1-3/test.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
void main(void) { // COMPLIANT
2+
}
3+
4+
int ____codeql_coding_standards_m1(int argc, char **argv) { // NON_COMPLIANT
5+
return 0;
6+
}
7+
8+
void ____codeql_coding_standards_m2(char *argc, char **argv) { // NON_COMPLIANT
9+
}
10+
11+
int ____codeql_coding_standards_m3(int argc, char *argv) { // NON_COMPLIANT
12+
return 0;
13+
}
14+
15+
int ____codeql_coding_standards_m4() { // NON_COMPLIANT
16+
return 0;
17+
}
18+
19+
int ____codeql_coding_standards_m5(int argc, int *argv) { // NON_COMPLIANT
20+
return 0;
21+
}
22+
23+
int ____codeql_coding_standards_m6(int argc, int **argv) { // NON_COMPLIANT
24+
return 0;
25+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import cpp
2+
/**
3+
* Library for modeling undefined behavior.
4+
*/
5+
abstract class UndefinedBehavior extends Locatable {}
6+
abstract class CPPUndefinedBehavior extends UndefinedBehavior {}

rule_packages/c/Language3.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838
"tags": [
3939
"maintainability",
4040
"readability"
41-
]
41+
],
42+
"implementation_scope": {
43+
"description": "This implementation only considers alternate forms of `main` and the undefined behavior that results. Additional cases from Appendix J of the C99 standard are not currently considered."
44+
}
4245
}
4346
],
4447
"title": "There shall be no occurrence of undefined or critical unspecified behavior"

0 commit comments

Comments
 (0)