Skip to content

Commit 516b086

Browse files
committed
Declarations1: add rule DCL37-C
1 parent e8ecc1b commit 516b086

File tree

9 files changed

+145
-1
lines changed

9 files changed

+145
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# DCL37-C: Do not declare or define a reserved identifier
2+
3+
This query implements the CERT-C rule DCL37-C:
4+
5+
> Do not declare or define a reserved identifier
6+
7+
## CERT
8+
9+
** REPLACE THIS BY RUNNING THE SCRIPT `scripts/help/cert-help-extraction.py` **
10+
11+
## Implementation notes
12+
13+
None
14+
15+
## References
16+
17+
* CERT-C: [DCL37-C: Do not declare or define a reserved identifier](https://wiki.sei.cmu.edu/confluence/display/c)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @id c/cert/do-not-declare-or-define-a-reserved-identifier
3+
* @name DCL37-C: Do not declare or define a reserved identifier
4+
* @description Declaring a reserved identifier can lead to undefined behaviour.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity warning
8+
* @tags external/cert/id/dcl37-c
9+
* correctness
10+
* maintainability
11+
* readability
12+
* external/cert/obligation/rule
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.cert
17+
import codingstandards.cpp.Naming
18+
import codingstandards.c.Keywords
19+
20+
from Element m, string name
21+
where
22+
not isExcluded(m, Declarations1Package::doNotDeclareOrDefineAReservedIdentifierQuery()) and
23+
(
24+
m.(Macro).hasName(name) or
25+
m.(Declaration).hasGlobalName(name)
26+
) and
27+
(
28+
Naming::Cpp14::hasStandardLibraryMacroName(name)
29+
or
30+
Naming::Cpp14::hasStandardLibraryObjectName(name)
31+
or
32+
Naming::Cpp14::hasStandardLibraryFunctionName(name)
33+
or
34+
name.regexpMatch("_[A-Z_].*")
35+
or
36+
name.regexpMatch("_.*") and m.(Declaration).hasGlobalName(name)
37+
or
38+
Keywords::isKeyword(name)
39+
)
40+
select m, "Reserved identifier '" + name + "' is declared."
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| test.c:2:1:2:23 | #define _RESERVED_MACRO | Reserved identifier '_RESERVED_MACRO' is declared. |
2+
| test.c:11:8:11:9 | _s | Reserved identifier '_s' is declared. |
3+
| test.c:15:6:15:7 | _f | Reserved identifier '_f' is declared. |
4+
| test.c:19:7:19:12 | malloc | Reserved identifier 'malloc' is declared. |
5+
| test.c:24:12:24:16 | errno | Reserved identifier 'errno' is declared. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/DCL37-C/DoNotDeclareOrDefineAReservedIdentifier.ql

c/cert/test/rules/DCL37-C/test.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef _RESERVED_MACRO
2+
#define _RESERVED_MACRO // NON_COMPLIANT
3+
#endif /* _RESERVED_MACRO */
4+
5+
#ifndef _not_reserved_MACRO
6+
#define _not_reserved_MACRO // COMPLIANT
7+
#endif /* _not_reserved_MACRO */
8+
9+
static const int INT_LIMIT_MAX = 12000; // COMPLIANT future library directions
10+
11+
struct _s { // NON_COMPLIANT
12+
struct _s *_next; // COMPLIANT not file scope
13+
};
14+
15+
void _f() { // NON_COMPLIANT
16+
int _p; // COMPLIANT not file scope
17+
}
18+
19+
void *malloc(int bytes) { // NON_COMPLIANT
20+
void *ptr;
21+
return ptr;
22+
}
23+
24+
extern int errno; // NON_COMPLIANT
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype Declarations1Query = TDoNotDeclareOrDefineAReservedIdentifierQuery()
7+
8+
predicate isDeclarations1QueryMetadata(Query query, string queryId, string ruleId) {
9+
query =
10+
// `Query` instance for the `doNotDeclareOrDefineAReservedIdentifier` query
11+
Declarations1Package::doNotDeclareOrDefineAReservedIdentifierQuery() and
12+
queryId =
13+
// `@id` for the `doNotDeclareOrDefineAReservedIdentifier` query
14+
"c/cert/do-not-declare-or-define-a-reserved-identifier" and
15+
ruleId = "DCL37-C"
16+
}
17+
18+
module Declarations1Package {
19+
Query doNotDeclareOrDefineAReservedIdentifierQuery() {
20+
//autogenerate `Query` type
21+
result =
22+
// `Query` type for `doNotDeclareOrDefineAReservedIdentifier` query
23+
TQueryC(TDeclarations1PackageQuery(TDoNotDeclareOrDefineAReservedIdentifierQuery()))
24+
}
25+
}

cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import codingstandards.cpp.exclusions.RuleMetadata
55
import Banned
66
import Concurrency1
77
import Concurrency2
8+
import Declarations1
89
import IO1
910
import IO2
1011
import IO3
@@ -27,6 +28,7 @@ newtype TCQuery =
2728
TBannedPackageQuery(BannedQuery q) or
2829
TConcurrency1PackageQuery(Concurrency1Query q) or
2930
TConcurrency2PackageQuery(Concurrency2Query q) or
31+
TDeclarations1PackageQuery(Declarations1Query q) or
3032
TIO1PackageQuery(IO1Query q) or
3133
TIO2PackageQuery(IO2Query q) or
3234
TIO3PackageQuery(IO3Query q) or
@@ -49,6 +51,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId) {
4951
isBannedQueryMetadata(query, queryId, ruleId) or
5052
isConcurrency1QueryMetadata(query, queryId, ruleId) or
5153
isConcurrency2QueryMetadata(query, queryId, ruleId) or
54+
isDeclarations1QueryMetadata(query, queryId, ruleId) or
5255
isIO1QueryMetadata(query, queryId, ruleId) or
5356
isIO2QueryMetadata(query, queryId, ruleId) or
5457
isIO3QueryMetadata(query, queryId, ruleId) or

rule_packages/c/Declarations1.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"CERT-C": {
3+
"DCL37-C": {
4+
"properties": {
5+
"obligation": "rule"
6+
},
7+
"queries": [
8+
{
9+
"description": "Declaring a reserved identifier can lead to undefined behaviour.",
10+
"kind": "problem",
11+
"name": "Do not declare or define a reserved identifier",
12+
"precision": "very-high",
13+
"severity": "warning",
14+
"short_name": "DoNotDeclareOrDefineAReservedIdentifier",
15+
"tags": [
16+
"correctness",
17+
"maintainability",
18+
"readability"
19+
],
20+
"implementation_scope": {
21+
"description": "This query does not consider identifiers described in the future library directions section of the standard. This query also checks for any reserved identifier as declared regardless of whether its header file is included or not.",
22+
"items": []
23+
}
24+
}
25+
],
26+
"title": "Do not declare or define a reserved identifier"
27+
}
28+
}
29+
}

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ c,CERT-C,CON43-C,OutOfScope,Rule,,,Do not allow data races in multithreaded code
501501
c,CERT-C,DCL30-C,Yes,Rule,,,Declare objects with appropriate storage durations,,Declarations,Hard,
502502
c,CERT-C,DCL31-C,Yes,Rule,,,Declare identifiers before using them,,Declarations,Medium,
503503
c,CERT-C,DCL36-C,Yes,Rule,,,Do not declare an identifier with conflicting linkage classifications,,Declarations,Medium,
504-
c,CERT-C,DCL37-C,Yes,Rule,,,Do not declare or define a reserved identifier,,Declarations,Easy,
504+
c,CERT-C,DCL37-C,Yes,Rule,,,Do not declare or define a reserved identifier,,Declarations1,Easy,
505505
c,CERT-C,DCL38-C,Yes,Rule,,,Use the correct syntax when declaring a flexible array member,,Declarations,Easy,
506506
c,CERT-C,DCL39-C,Yes,Rule,,,Avoid information leakage when passing a structure across a trust boundary,,Declarations,Hard,
507507
c,CERT-C,DCL40-C,Yes,Rule,,,Do not create incompatible declarations of the same function or object,,Declarations,Hard,

0 commit comments

Comments
 (0)