Skip to content

Commit c9d6ce0

Browse files
Merge branch 'main' into enhancement/yaml
2 parents 15539f2 + d39bdf4 commit c9d6ce0

File tree

58 files changed

+1055
-31
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1055
-31
lines changed

c/cert/src/rules/DCL31-C/DeclareIdentifiersBeforeUsingThem.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ Search for [vulnerabilities](https://wiki.sei.cmu.edu/confluence/display/c/BB.+D
153153

154154
## Implementation notes
155155

156-
This query does not check for implicit function declarations as this is partially compiler checked.
156+
This query does not check for implicitly typed parameters, typedefs or member declarations as this is partially compiler checked.
157157

158158
## References
159159

c/cert/src/rules/DCL31-C/DeclareIdentifiersBeforeUsingThem.ql

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,10 @@
1313

1414
import cpp
1515
import codingstandards.c.cert
16+
import codingstandards.cpp.rules.typeomitted.TypeOmitted
1617

17-
from Declaration d
18-
where
19-
not isExcluded(d, Declarations1Package::declareIdentifiersBeforeUsingThemQuery()) and
20-
d.hasSpecifier("implicit_int") and
21-
exists(Type t |
22-
(d.(Variable).getType() = t or d.(Function).getType() = t) and
23-
// Exclude "short" or "long", as opposed to "short int" or "long int".
24-
t instanceof IntType and
25-
// Exclude "signed" or "unsigned", as opposed to "signed int" or "unsigned int".
26-
not exists(IntegralType it | it = t | it.isExplicitlySigned() or it.isExplicitlyUnsigned())
27-
)
28-
select d, "Declaration " + d.getName() + " is missing a type specifier."
18+
class DeclareIdentifiersBeforeUsingThem extends TypeOmittedSharedQuery {
19+
DeclareIdentifiersBeforeUsingThem() {
20+
this = Declarations1Package::declareIdentifiersBeforeUsingThemQuery()
21+
}
22+
}

c/cert/test/rules/DCL31-C/DeclareIdentifiersBeforeUsingThem.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c/common/test/rules/typeomitted/TypeOmitted.ql
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/** Provides a library for errno-setting functions. */
2+
3+
import cpp
4+
5+
/*
6+
* An errno-setting function
7+
*/
8+
9+
abstract class ErrnoSettingFunction extends Function { }
10+
11+
/*
12+
* An errno-setting function that return out-of-band errors indicators
13+
*/
14+
15+
class OutOfBandErrnoSettingFunction extends ErrnoSettingFunction {
16+
OutOfBandErrnoSettingFunction() {
17+
this.hasGlobalName(["ftell", "fgetpos", "fsetpos", "mbrtowc", "wcrtomb", "wcsrtombs"])
18+
}
19+
}
20+
21+
/*
22+
* An errno-setting function that return in-band errors indicators
23+
*/
24+
25+
class InBandErrnoSettingFunction extends ErrnoSettingFunction {
26+
InBandErrnoSettingFunction() {
27+
this.hasGlobalName([
28+
"fgetwc", "fputwc", "strtol", "wcstol", "strtoll", "wcstoll", "strtoul", "wcstoul",
29+
"strtoull", "wcstoull", "strtoumax", "wcstoumax", "strtod", "wcstod", "strtof", "wcstof",
30+
"strtold", "wcstold", "strtoimax", "wcstoimax"
31+
])
32+
}
33+
}
34+
35+
/*
36+
* A assignment expression setting `errno` to 0
37+
*/
38+
39+
class ErrnoZeroed extends AssignExpr {
40+
ErrnoZeroed() {
41+
this.getLValue() = any(MacroInvocation ma | ma.getMacroName() = "errno").getExpr() and
42+
this.getRValue().getValue() = "0"
43+
}
44+
}
45+
46+
/*
47+
* A guard controlled by a errno comparison
48+
*/
49+
50+
abstract class ErrnoGuard extends StmtParent {
51+
abstract ControlFlowNode getZeroedSuccessor();
52+
53+
abstract ControlFlowNode getNonZeroedSuccessor();
54+
}
55+
56+
class ErrnoIfGuard extends EqualityOperation, ErrnoGuard {
57+
ControlStructure i;
58+
59+
ErrnoIfGuard() {
60+
this.getAnOperand() = any(MacroInvocation ma | ma.getMacroName() = "errno").getExpr() and
61+
this.getAnOperand().getValue() = "0" and
62+
i.getControllingExpr() = this
63+
}
64+
65+
Stmt getThenSuccessor() {
66+
i.getControllingExpr() = this and
67+
(result = i.(IfStmt).getThen() or result = i.(Loop).getStmt())
68+
}
69+
70+
Stmt getElseSuccessor() {
71+
i.getControllingExpr() = this and
72+
(
73+
i.(IfStmt).hasElse() and result = i.(IfStmt).getElse()
74+
or
75+
result = i.getFollowingStmt()
76+
)
77+
}
78+
79+
override ControlFlowNode getZeroedSuccessor() {
80+
if this instanceof EQExpr then result = this.getThenSuccessor() else result = getElseSuccessor()
81+
}
82+
83+
override ControlFlowNode getNonZeroedSuccessor() {
84+
if this instanceof NEExpr then result = this.getThenSuccessor() else result = getElseSuccessor()
85+
}
86+
}
87+
88+
class ErrnoSwitchGuard extends SwitchCase, ErrnoGuard {
89+
ErrnoSwitchGuard() {
90+
this.getSwitchStmt().getExpr() = any(MacroInvocation ma | ma.getMacroName() = "errno").getExpr()
91+
}
92+
93+
override ControlFlowNode getZeroedSuccessor() {
94+
result = this.getAStmt() and this.getExpr().getValue() = "0"
95+
}
96+
97+
override ControlFlowNode getNonZeroedSuccessor() {
98+
result = this.getAStmt() and this.getExpr().getValue() != "0"
99+
}
100+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import cpp
2+
3+
//Identifiers that are candidates for checking uniqueness
4+
class InterestingIdentifiers extends Declaration {
5+
InterestingIdentifiers() {
6+
not this.isFromTemplateInstantiation(_) and
7+
not this.isFromUninstantiatedTemplate(_) and
8+
not this instanceof TemplateParameter and
9+
not this.hasDeclaringType() and
10+
not this instanceof Operator and
11+
not this.hasName("main") and
12+
exists(this.getADeclarationLocation())
13+
}
14+
15+
string getSignificantName() { result = this.getName().prefix(31) }
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| test.c:4:7:4:9 | id1 | Variable is hiding variable $@. | test.c:1:5:1:7 | id1 | id1 |
2+
| test.c:7:13:7:15 | id1 | Variable is hiding variable $@. | test.c:1:5:1:7 | id1 | id1 |
3+
| test.c:10:12:10:14 | id1 | Variable is hiding variable $@. | test.c:1:5:1:7 | id1 | id1 |
4+
| test.c:11:14:11:16 | id1 | Variable is hiding variable $@. | test.c:10:12:10:14 | id1 | id1 |
5+
| test.c:24:24:24:26 | id2 | Variable is hiding variable $@. | test.c:22:5:22:7 | id2 | id2 |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.identifierhidden.IdentifierHidden
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
int id1;
2+
3+
void f1() {
4+
int id1; // NON_COMPLIANT
5+
}
6+
7+
void f2(int id1) {} // NON_COMPLIANT
8+
9+
void f3() {
10+
for (int id1; id1 < 1; id1++) { // NON_COMPLIANT
11+
for (int id1; id1 < 1; id1++) {
12+
} // NON_COMPLIANT
13+
}
14+
}
15+
16+
struct astruct {
17+
int id1;
18+
};
19+
20+
extern void g(struct astruct *p);
21+
22+
int id2 = 0;
23+
24+
void f4(struct astruct id2) { // NON_COMPLIANT
25+
g(&id2);
26+
}
27+
28+
void f5(struct astruct id3) { // COMPLIANT
29+
g(&id2);
30+
}

0 commit comments

Comments
 (0)