From 6f91612a520340cf204a794da0c58ec7d3540ffc Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Thu, 5 Jan 2023 10:58:59 -0500 Subject: [PATCH 01/11] Declarations6: add RULE-17-3 and refactor Identifiers to include new predicate --- .vscode/tasks.json | 1 + .../RULE-17-3/FunctionDeclaredImplicitly.ql | 29 ++++++++++++ .../FunctionDeclaredImplicitly.expected | 2 + .../FunctionDeclaredImplicitly.qlref | 1 + c/misra/test/rules/RULE-17-3/test.c | 13 ++++++ .../src/codingstandards/cpp/Identifiers.qll | 46 +++++++++++++++++++ .../cpp/exclusions/c/Declarations6.qll | 26 +++++++++++ .../cpp/exclusions/c/RuleMetadata.qll | 3 ++ .../cpp/rules/typeomitted/TypeOmitted.qll | 10 +--- rule_packages/c/Declarations6.json | 24 ++++++++++ rules.csv | 2 +- 11 files changed, 148 insertions(+), 9 deletions(-) create mode 100644 c/misra/src/rules/RULE-17-3/FunctionDeclaredImplicitly.ql create mode 100644 c/misra/test/rules/RULE-17-3/FunctionDeclaredImplicitly.expected create mode 100644 c/misra/test/rules/RULE-17-3/FunctionDeclaredImplicitly.qlref create mode 100644 c/misra/test/rules/RULE-17-3/test.c create mode 100644 cpp/common/src/codingstandards/cpp/Identifiers.qll create mode 100644 cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll create mode 100644 rule_packages/c/Declarations6.json diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 3147f97957..81c4937a26 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -206,6 +206,7 @@ "Declarations", "Declarations1", "Declarations2", + "Declarations6", "Exceptions1", "Exceptions2", "Expressions", diff --git a/c/misra/src/rules/RULE-17-3/FunctionDeclaredImplicitly.ql b/c/misra/src/rules/RULE-17-3/FunctionDeclaredImplicitly.ql new file mode 100644 index 0000000000..304d0a9bf6 --- /dev/null +++ b/c/misra/src/rules/RULE-17-3/FunctionDeclaredImplicitly.ql @@ -0,0 +1,29 @@ +/** + * @id c/misra/function-declared-implicitly + * @name RULE-17-3: A function shall not be declared implicitly + * @description Omission of type specifiers may not be supported by some compilers. Additionally + * implicit typing can lead to undefined behaviour. + * @kind problem + * @precision very-high + * @problem.severity error + * @tags external/misra/id/rule-17-3 + * correctness + * readability + * external/misra/obligation/mandatory + */ + +import cpp +import codingstandards.c.misra +import codingstandards.cpp.Identifiers + +from FunctionDeclarationEntry fde +where + not isExcluded(fde, Declarations6Package::functionDeclaredImplicitlyQuery()) and + ( + //use before declaration + fde.isImplicit() + or + //declared but type not explicit + isDeclaredImplicit(fde.getDeclaration()) + ) +select fde, "Function declaration is implicit." diff --git a/c/misra/test/rules/RULE-17-3/FunctionDeclaredImplicitly.expected b/c/misra/test/rules/RULE-17-3/FunctionDeclaredImplicitly.expected new file mode 100644 index 0000000000..9d7cb66d70 --- /dev/null +++ b/c/misra/test/rules/RULE-17-3/FunctionDeclaredImplicitly.expected @@ -0,0 +1,2 @@ +| test.c:3:1:3:2 | declaration of f2 | Function declaration is implicit. | +| test.c:11:17:11:17 | declaration of f3 | Function declaration is implicit. | diff --git a/c/misra/test/rules/RULE-17-3/FunctionDeclaredImplicitly.qlref b/c/misra/test/rules/RULE-17-3/FunctionDeclaredImplicitly.qlref new file mode 100644 index 0000000000..24df819bf7 --- /dev/null +++ b/c/misra/test/rules/RULE-17-3/FunctionDeclaredImplicitly.qlref @@ -0,0 +1 @@ +rules/RULE-17-3/FunctionDeclaredImplicitly.ql \ No newline at end of file diff --git a/c/misra/test/rules/RULE-17-3/test.c b/c/misra/test/rules/RULE-17-3/test.c new file mode 100644 index 0000000000..6c9dd64836 --- /dev/null +++ b/c/misra/test/rules/RULE-17-3/test.c @@ -0,0 +1,13 @@ +// semmle-extractor-options:--clang -std=c11 -nostdinc +// -I../../../../common/test/includes/standard-library +double f1(double x); // COMPLIANT +f2(double x); // NON_COMPLIANT + +void f() { + double l = 1; + double l1 = f1(l); + + double l2 = f2(l); + + double l3 = f3(l); // NON_COMPLIANT +} \ No newline at end of file diff --git a/cpp/common/src/codingstandards/cpp/Identifiers.qll b/cpp/common/src/codingstandards/cpp/Identifiers.qll new file mode 100644 index 0000000000..665acbeb6b --- /dev/null +++ b/cpp/common/src/codingstandards/cpp/Identifiers.qll @@ -0,0 +1,46 @@ +import cpp +import codingstandards.cpp.Linkage + +class ExternalIdentifiers extends InterestingIdentifiers { + ExternalIdentifiers() { + hasExternalLinkage(this) and + getNamespace() instanceof GlobalNamespace + } + + string getSignificantName() { + //C99 states the first 31 characters of external identifiers are significant + //C90 states the first 6 characters of external identifiers are significant and case is not required to be significant + //C90 is not currently considered by this rule + result = this.getName().prefix(31) + } +} + +//Identifiers that are candidates for checking uniqueness +class InterestingIdentifiers extends Declaration { + InterestingIdentifiers() { + not this.isFromTemplateInstantiation(_) and + not this.isFromUninstantiatedTemplate(_) and + not this instanceof TemplateParameter and + not this.hasDeclaringType() and + not this instanceof Operator and + not this.hasName("main") and + exists(this.getADeclarationLocation()) + } + + //this definition of significant relies on the number of significant characters for a macro name (C99) + //this is used on macro name comparisons only + //not necessarily against other types of identifiers + string getSignificantNameComparedToMacro() { result = this.getName().prefix(63) } +} + +//Declarations that omit type - C90 compiler assumes int +predicate isDeclaredImplicit(Declaration d) { + d.hasSpecifier("implicit_int") and + exists(Type t | + (d.(Variable).getType() = t or d.(Function).getType() = t) and + // Exclude "short" or "long", as opposed to "short int" or "long int". + t instanceof IntType and + // Exclude "signed" or "unsigned", as opposed to "signed int" or "unsigned int". + not exists(IntegralType it | it = t | it.isExplicitlySigned() or it.isExplicitlyUnsigned()) + ) +} diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll new file mode 100644 index 0000000000..fe3ec8b508 --- /dev/null +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll @@ -0,0 +1,26 @@ +//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ +import cpp +import RuleMetadata +import codingstandards.cpp.exclusions.RuleMetadata + +newtype Declarations6Query = TFunctionDeclaredImplicitlyQuery() + +predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleId, string category) { + query = + // `Query` instance for the `functionDeclaredImplicitly` query + Declarations6Package::functionDeclaredImplicitlyQuery() and + queryId = + // `@id` for the `functionDeclaredImplicitly` query + "c/misra/function-declared-implicitly" and + ruleId = "RULE-17-3" and + category = "mandatory" +} + +module Declarations6Package { + Query functionDeclaredImplicitlyQuery() { + //autogenerate `Query` type + result = + // `Query` type for `functionDeclaredImplicitly` query + TQueryC(TDeclarations6PackageQuery(TFunctionDeclaredImplicitlyQuery())) + } +} diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll index 11994dd9fd..a197511e3b 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll @@ -15,6 +15,7 @@ import Contracts4 import Declarations1 import Declarations2 import Declarations3 +import Declarations6 import Expressions import IO1 import IO2 @@ -53,6 +54,7 @@ newtype TCQuery = TDeclarations1PackageQuery(Declarations1Query q) or TDeclarations2PackageQuery(Declarations2Query q) or TDeclarations3PackageQuery(Declarations3Query q) or + TDeclarations6PackageQuery(Declarations6Query q) or TExpressionsPackageQuery(ExpressionsQuery q) or TIO1PackageQuery(IO1Query q) or TIO2PackageQuery(IO2Query q) or @@ -91,6 +93,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat isDeclarations1QueryMetadata(query, queryId, ruleId, category) or isDeclarations2QueryMetadata(query, queryId, ruleId, category) or isDeclarations3QueryMetadata(query, queryId, ruleId, category) or + isDeclarations6QueryMetadata(query, queryId, ruleId, category) or isExpressionsQueryMetadata(query, queryId, ruleId, category) or isIO1QueryMetadata(query, queryId, ruleId, category) or isIO2QueryMetadata(query, queryId, ruleId, category) or diff --git a/cpp/common/src/codingstandards/cpp/rules/typeomitted/TypeOmitted.qll b/cpp/common/src/codingstandards/cpp/rules/typeomitted/TypeOmitted.qll index 420a384208..8c1cb3b80a 100644 --- a/cpp/common/src/codingstandards/cpp/rules/typeomitted/TypeOmitted.qll +++ b/cpp/common/src/codingstandards/cpp/rules/typeomitted/TypeOmitted.qll @@ -5,6 +5,7 @@ import cpp import codingstandards.cpp.Customizations import codingstandards.cpp.Exclusions +import codingstandards.cpp.Identifiers abstract class TypeOmittedSharedQuery extends Query { } @@ -12,13 +13,6 @@ Query getQuery() { result instanceof TypeOmittedSharedQuery } query predicate problems(Declaration d, string message) { not isExcluded(d, getQuery()) and - d.hasSpecifier("implicit_int") and - exists(Type t | - (d.(Variable).getType() = t or d.(Function).getType() = t) and - // Exclude "short" or "long", as opposed to "short int" or "long int". - t instanceof IntType and - // Exclude "signed" or "unsigned", as opposed to "signed int" or "unsigned int". - not exists(IntegralType it | it = t | it.isExplicitlySigned() or it.isExplicitlyUnsigned()) - ) and + isDeclaredImplicit(d) and message = "Declaration " + d.getName() + " is missing a type specifier." } diff --git a/rule_packages/c/Declarations6.json b/rule_packages/c/Declarations6.json new file mode 100644 index 0000000000..af2b15e868 --- /dev/null +++ b/rule_packages/c/Declarations6.json @@ -0,0 +1,24 @@ +{ + "MISRA-C-2012": { + "RULE-17-3": { + "properties": { + "obligation": "mandatory" + }, + "queries": [ + { + "description": "Omission of type specifiers may not be supported by some compilers. Additionally implicit typing can lead to undefined behaviour.", + "kind": "problem", + "name": "A function shall not be declared implicitly", + "precision": "very-high", + "severity": "error", + "short_name": "FunctionDeclaredImplicitly", + "tags": [ + "correctness", + "readability" + ] + } + ], + "title": "A function shall not be declared implicitly" + } + } +} \ No newline at end of file diff --git a/rules.csv b/rules.csv index bb4456877e..05473821fe 100644 --- a/rules.csv +++ b/rules.csv @@ -713,7 +713,7 @@ c,MISRA-C-2012,RULE-16-6,Yes,Required,,,Every switch statement shall have at lea c,MISRA-C-2012,RULE-16-7,Yes,Required,,,A switch-expression shall not have essentially Boolean type,M6-4-7,Statements,Medium, c,MISRA-C-2012,RULE-17-1,Yes,Required,,,The features of shall not be used,,Banned,Easy, c,MISRA-C-2012,RULE-17-2,Yes,Required,,,"Functions shall not call themselves, either directly or indirectly",A7-5-2,Statements,Import, -c,MISRA-C-2012,RULE-17-3,Yes,Mandatory,,,A function shall not be declared implicitly,,Declarations,Medium, +c,MISRA-C-2012,RULE-17-3,Yes,Mandatory,,,A function shall not be declared implicitly,,Declarations6,Medium, c,MISRA-C-2012,RULE-17-4,Yes,Mandatory,,,All exit paths from a function with non-void return type shall have an explicit return statement with an expression,MSC52-CPP,Statements,Medium, c,MISRA-C-2012,RULE-17-5,Yes,Advisory,,,The function argument corresponding to a parameter declared to have an array type shall have an appropriate number of elements,,Contracts,Hard, c,MISRA-C-2012,RULE-17-6,No,Mandatory,,,The declaration of an array parameter shall not contain the static keyword between the [ ],,,, From 6a06a24264c57d8a74e965c01de6ebf77821b856 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Mon, 9 Jan 2023 15:07:10 -0500 Subject: [PATCH 02/11] Declarations6: add RULE-5-8 and RULE-5-9 --- ...IdentifiersWithExternalLinkageNotUnique.ql | 24 +++++++++++ ...IdentifiersWithInternalLinkageNotUnique.ql | 32 +++++++++++++++ ...fiersWithExternalLinkageNotUnique.expected | 2 + ...ntifiersWithExternalLinkageNotUnique.qlref | 1 + c/misra/test/rules/RULE-5-8/test.c | 3 ++ c/misra/test/rules/RULE-5-8/test1.c | 5 +++ ...fiersWithInternalLinkageNotUnique.expected | 4 ++ ...ntifiersWithInternalLinkageNotUnique.qlref | 1 + c/misra/test/rules/RULE-5-9/test.c | 2 + c/misra/test/rules/RULE-5-9/test1.c | 12 ++++++ .../cpp/exclusions/c/Declarations6.qll | 37 ++++++++++++++++- rule_packages/c/Declarations6.json | 40 +++++++++++++++++++ rules.csv | 4 +- 13 files changed, 164 insertions(+), 3 deletions(-) create mode 100644 c/misra/src/rules/RULE-5-8/IdentifiersWithExternalLinkageNotUnique.ql create mode 100644 c/misra/src/rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.ql create mode 100644 c/misra/test/rules/RULE-5-8/IdentifiersWithExternalLinkageNotUnique.expected create mode 100644 c/misra/test/rules/RULE-5-8/IdentifiersWithExternalLinkageNotUnique.qlref create mode 100644 c/misra/test/rules/RULE-5-8/test.c create mode 100644 c/misra/test/rules/RULE-5-8/test1.c create mode 100644 c/misra/test/rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.expected create mode 100644 c/misra/test/rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.qlref create mode 100644 c/misra/test/rules/RULE-5-9/test.c create mode 100644 c/misra/test/rules/RULE-5-9/test1.c diff --git a/c/misra/src/rules/RULE-5-8/IdentifiersWithExternalLinkageNotUnique.ql b/c/misra/src/rules/RULE-5-8/IdentifiersWithExternalLinkageNotUnique.ql new file mode 100644 index 0000000000..ff20ceed18 --- /dev/null +++ b/c/misra/src/rules/RULE-5-8/IdentifiersWithExternalLinkageNotUnique.ql @@ -0,0 +1,24 @@ +/** + * @id c/misra/identifiers-with-external-linkage-not-unique + * @name RULE-5-8: Identifiers that define objects or functions with external linkage shall be unique + * @description Using non-unique identifiers can lead to developer confusion. + * @kind problem + * @precision very-high + * @problem.severity error + * @tags external/misra/id/rule-5-8 + * maintainability + * readability + * external/misra/obligation/required + */ + +import cpp +import codingstandards.c.misra +import codingstandards.cpp.Identifiers + +from Declaration de, ExternalIdentifiers e +where + not isExcluded(de, Declarations6Package::identifiersWithExternalLinkageNotUniqueQuery()) and + not isExcluded(e, Declarations6Package::identifiersWithExternalLinkageNotUniqueQuery()) and + not de = e and + de.getName() = e.getName() +select de, "Identifier conflicts with external identifier $@", e, e.getName() diff --git a/c/misra/src/rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.ql b/c/misra/src/rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.ql new file mode 100644 index 0000000000..45f63a3207 --- /dev/null +++ b/c/misra/src/rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.ql @@ -0,0 +1,32 @@ +/** + * @id c/misra/identifiers-with-internal-linkage-not-unique + * @name RULE-5-9: Identifiers that define objects or functions with internal linkage should be unique + * @description Using non-unique identifiers can lead to developer confusion. + * @kind problem + * @precision very-high + * @problem.severity error + * @tags external/misra/id/rule-5-9 + * maintainability + * readability + * external/misra/obligation/advisory + */ + +import cpp +import codingstandards.c.misra + +from Declaration d1, Declaration d2 +where + not isExcluded(d1, Declarations6Package::identifiersWithInternalLinkageNotUniqueQuery()) and + not isExcluded(d2, Declarations6Package::identifiersWithInternalLinkageNotUniqueQuery()) and + d1.isStatic() and + d1.isTopLevel() and + not d1 = d2 and + d1.getName() = d2.getName() and + // Apply an ordering based on location to enforce that (d1, d2) = (d2, d1) and we only report (d1, d2). + ( + d1.getFile().getAbsolutePath() < d2.getFile().getAbsolutePath() + or + d1.getFile().getAbsolutePath() = d2.getFile().getAbsolutePath() and + d1.getLocation().getStartLine() < d2.getLocation().getStartLine() + ) +select d2, "Identifier conflicts with identifier $@ with internal linkage.", d1, d1.getName() diff --git a/c/misra/test/rules/RULE-5-8/IdentifiersWithExternalLinkageNotUnique.expected b/c/misra/test/rules/RULE-5-8/IdentifiersWithExternalLinkageNotUnique.expected new file mode 100644 index 0000000000..b9f237be3f --- /dev/null +++ b/c/misra/test/rules/RULE-5-8/IdentifiersWithExternalLinkageNotUnique.expected @@ -0,0 +1,2 @@ +| test1.c:1:13:1:13 | f | Identifier conflicts with external identifier $@ | test.c:3:6:3:6 | f | f | +| test1.c:2:7:2:7 | g | Identifier conflicts with external identifier $@ | test.c:1:5:1:5 | g | g | diff --git a/c/misra/test/rules/RULE-5-8/IdentifiersWithExternalLinkageNotUnique.qlref b/c/misra/test/rules/RULE-5-8/IdentifiersWithExternalLinkageNotUnique.qlref new file mode 100644 index 0000000000..1eb56b955d --- /dev/null +++ b/c/misra/test/rules/RULE-5-8/IdentifiersWithExternalLinkageNotUnique.qlref @@ -0,0 +1 @@ +rules/RULE-5-8/IdentifiersWithExternalLinkageNotUnique.ql \ No newline at end of file diff --git a/c/misra/test/rules/RULE-5-8/test.c b/c/misra/test/rules/RULE-5-8/test.c new file mode 100644 index 0000000000..119c4548c4 --- /dev/null +++ b/c/misra/test/rules/RULE-5-8/test.c @@ -0,0 +1,3 @@ +int g; +extern int g1; // COMPLIANT +void f() { int i; } \ No newline at end of file diff --git a/c/misra/test/rules/RULE-5-8/test1.c b/c/misra/test/rules/RULE-5-8/test1.c new file mode 100644 index 0000000000..027d6ac046 --- /dev/null +++ b/c/misra/test/rules/RULE-5-8/test1.c @@ -0,0 +1,5 @@ +static void f() { // NON_COMPLIANT + int g; // NON_COMPLIANT + int i; // COMPLIANT +} +int g1; //COMPLIANT \ No newline at end of file diff --git a/c/misra/test/rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.expected b/c/misra/test/rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.expected new file mode 100644 index 0000000000..6fc2c006a2 --- /dev/null +++ b/c/misra/test/rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.expected @@ -0,0 +1,4 @@ +| test1.c:3:12:3:13 | g1 | Identifier conflicts with identifier $@ with internal linkage. | test.c:2:12:2:13 | g1 | g1 | +| test1.c:5:13:5:13 | f | Identifier conflicts with identifier $@ with internal linkage. | test.c:3:13:3:13 | f | f | +| test1.c:6:7:6:7 | g | Identifier conflicts with identifier $@ with internal linkage. | test1.c:2:12:2:12 | g | g | +| test1.c:11:7:11:7 | g | Identifier conflicts with identifier $@ with internal linkage. | test1.c:2:12:2:12 | g | g | diff --git a/c/misra/test/rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.qlref b/c/misra/test/rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.qlref new file mode 100644 index 0000000000..becc4e9e43 --- /dev/null +++ b/c/misra/test/rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.qlref @@ -0,0 +1 @@ +rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.ql \ No newline at end of file diff --git a/c/misra/test/rules/RULE-5-9/test.c b/c/misra/test/rules/RULE-5-9/test.c new file mode 100644 index 0000000000..cd5e2ad888 --- /dev/null +++ b/c/misra/test/rules/RULE-5-9/test.c @@ -0,0 +1,2 @@ +static int g1; // NON_COMPLIANT +static void f(); // NON_COMPLIANT \ No newline at end of file diff --git a/c/misra/test/rules/RULE-5-9/test1.c b/c/misra/test/rules/RULE-5-9/test1.c new file mode 100644 index 0000000000..0fa1261614 --- /dev/null +++ b/c/misra/test/rules/RULE-5-9/test1.c @@ -0,0 +1,12 @@ +static int g; // COMPLIANT +static int g1; // NON_COMPLIANT + +static void f() { // NON_COMPLIANT + int g; // NON_COMPLIANT + int g2; // COMPLIANT +} + +void f1() { // COMPLIANT + int g; // NON_COMPLIANT + int g2; // COMPLIANT +} \ No newline at end of file diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll index fe3ec8b508..3cae883ebf 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll @@ -3,7 +3,10 @@ import cpp import RuleMetadata import codingstandards.cpp.exclusions.RuleMetadata -newtype Declarations6Query = TFunctionDeclaredImplicitlyQuery() +newtype Declarations6Query = + TFunctionDeclaredImplicitlyQuery() or + TIdentifiersWithExternalLinkageNotUniqueQuery() or + TIdentifiersWithInternalLinkageNotUniqueQuery() predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleId, string category) { query = @@ -14,6 +17,24 @@ predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleI "c/misra/function-declared-implicitly" and ruleId = "RULE-17-3" and category = "mandatory" + or + query = + // `Query` instance for the `identifiersWithExternalLinkageNotUnique` query + Declarations6Package::identifiersWithExternalLinkageNotUniqueQuery() and + queryId = + // `@id` for the `identifiersWithExternalLinkageNotUnique` query + "c/misra/identifiers-with-external-linkage-not-unique" and + ruleId = "RULE-5-8" and + category = "required" + or + query = + // `Query` instance for the `identifiersWithInternalLinkageNotUnique` query + Declarations6Package::identifiersWithInternalLinkageNotUniqueQuery() and + queryId = + // `@id` for the `identifiersWithInternalLinkageNotUnique` query + "c/misra/identifiers-with-internal-linkage-not-unique" and + ruleId = "RULE-5-9" and + category = "advisory" } module Declarations6Package { @@ -23,4 +44,18 @@ module Declarations6Package { // `Query` type for `functionDeclaredImplicitly` query TQueryC(TDeclarations6PackageQuery(TFunctionDeclaredImplicitlyQuery())) } + + Query identifiersWithExternalLinkageNotUniqueQuery() { + //autogenerate `Query` type + result = + // `Query` type for `identifiersWithExternalLinkageNotUnique` query + TQueryC(TDeclarations6PackageQuery(TIdentifiersWithExternalLinkageNotUniqueQuery())) + } + + Query identifiersWithInternalLinkageNotUniqueQuery() { + //autogenerate `Query` type + result = + // `Query` type for `identifiersWithInternalLinkageNotUnique` query + TQueryC(TDeclarations6PackageQuery(TIdentifiersWithInternalLinkageNotUniqueQuery())) + } } diff --git a/rule_packages/c/Declarations6.json b/rule_packages/c/Declarations6.json index af2b15e868..8120db9ac5 100644 --- a/rule_packages/c/Declarations6.json +++ b/rule_packages/c/Declarations6.json @@ -19,6 +19,46 @@ } ], "title": "A function shall not be declared implicitly" + }, + "RULE-5-8": { + "properties": { + "obligation": "required" + }, + "queries": [ + { + "description": "Using non-unique identifiers can lead to developer confusion.", + "kind": "problem", + "name": "Identifiers that define objects or functions with external linkage shall be unique", + "precision": "very-high", + "severity": "error", + "short_name": "IdentifiersWithExternalLinkageNotUnique", + "tags": [ + "maintainability", + "readability" + ] + } + ], + "title": "Identifiers that define objects or functions with external linkage shall be unique" + }, + "RULE-5-9": { + "properties": { + "obligation": "advisory" + }, + "queries": [ + { + "description": "Using non-unique identifiers can lead to developer confusion.", + "kind": "problem", + "name": "Identifiers that define objects or functions with internal linkage should be unique", + "precision": "very-high", + "severity": "error", + "short_name": "IdentifiersWithInternalLinkageNotUnique", + "tags": [ + "maintainability", + "readability" + ] + } + ], + "title": "Identifiers that define objects or functions with internal linkage should be unique" } } } \ No newline at end of file diff --git a/rules.csv b/rules.csv index 05473821fe..20171b1d9c 100644 --- a/rules.csv +++ b/rules.csv @@ -638,8 +638,8 @@ c,MISRA-C-2012,RULE-5-4,Yes,Required,,,Macro identifiers shall be distinct,,Decl c,MISRA-C-2012,RULE-5-5,Yes,Required,,,Identifiers shall be distinct from macro names,,Declarations3,Easy, c,MISRA-C-2012,RULE-5-6,Yes,Required,,,A typedef name shall be a unique identifier,,Declarations3,Easy, c,MISRA-C-2012,RULE-5-7,Yes,Required,,,A tag name shall be a unique identifier,,Declarations3,Easy, -c,MISRA-C-2012,RULE-5-8,Yes,Required,,,Identifiers that define objects or functions with external linkage shall be unique,,Declarations,Easy, -c,MISRA-C-2012,RULE-5-9,Yes,Advisory,,,Identifiers that define objects or functions with internal linkage should be unique,,Declarations,Easy, +c,MISRA-C-2012,RULE-5-8,Yes,Required,,,Identifiers that define objects or functions with external linkage shall be unique,,Declarations6,Easy, +c,MISRA-C-2012,RULE-5-9,Yes,Advisory,,,Identifiers that define objects or functions with internal linkage should be unique,,Declarations6,Easy, c,MISRA-C-2012,RULE-6-1,Yes,Required,,,Bit-fields shall only be declared with an appropriate type,M9-6-4,Types,Medium, c,MISRA-C-2012,RULE-6-2,Yes,Required,,,Single-bit named bit fields shall not be of a signed type,M9-6-4,Types,Import, c,MISRA-C-2012,RULE-7-1,Yes,Required,,,Octal constants shall not be used,M2-13-2,Banned,Import, From 874a5182e8387a27be8faee310c7be61a7be9ecb Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Mon, 9 Jan 2023 15:13:11 -0500 Subject: [PATCH 03/11] Declarations6: fix formatting testcase RULE-5-8 --- c/misra/test/rules/RULE-5-8/test1.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/c/misra/test/rules/RULE-5-8/test1.c b/c/misra/test/rules/RULE-5-8/test1.c index 027d6ac046..8c138f0bb4 100644 --- a/c/misra/test/rules/RULE-5-8/test1.c +++ b/c/misra/test/rules/RULE-5-8/test1.c @@ -2,4 +2,4 @@ static void f() { // NON_COMPLIANT int g; // NON_COMPLIANT int i; // COMPLIANT } -int g1; //COMPLIANT \ No newline at end of file +int g1; // COMPLIANT \ No newline at end of file From e7185594ce1c0b4c36d8c80187e525ce259c12e6 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Mon, 9 Jan 2023 16:01:37 -0500 Subject: [PATCH 04/11] Declarations6: fix formatting testcase RULE-17-3 --- .../test/rules/RULE-17-3/FunctionDeclaredImplicitly.expected | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/c/misra/test/rules/RULE-17-3/FunctionDeclaredImplicitly.expected b/c/misra/test/rules/RULE-17-3/FunctionDeclaredImplicitly.expected index 9d7cb66d70..8b53c721bb 100644 --- a/c/misra/test/rules/RULE-17-3/FunctionDeclaredImplicitly.expected +++ b/c/misra/test/rules/RULE-17-3/FunctionDeclaredImplicitly.expected @@ -1,2 +1,2 @@ -| test.c:3:1:3:2 | declaration of f2 | Function declaration is implicit. | -| test.c:11:17:11:17 | declaration of f3 | Function declaration is implicit. | +| test.c:4:1:4:2 | declaration of f2 | Function declaration is implicit. | +| test.c:12:15:12:15 | declaration of f3 | Function declaration is implicit. | From 2a6a85edd30486576a3f7c4983954776b0df5a5a Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Tue, 10 Jan 2023 11:16:06 -0500 Subject: [PATCH 05/11] Declarations6: add RULE-8-11 --- ...yExternalLinkageSizeExplicitlySpecified.ql | 28 +++++++++++++++++++ ...nalLinkageSizeExplicitlySpecified.expected | 1 + ...ternalLinkageSizeExplicitlySpecified.qlref | 1 + c/misra/test/rules/RULE-8-11/test.c | 6 ++++ .../cpp/exclusions/c/Declarations6.qll | 19 ++++++++++++- rule_packages/c/Declarations6.json | 20 +++++++++++++ rules.csv | 2 +- 7 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 c/misra/src/rules/RULE-8-11/ArrayExternalLinkageSizeExplicitlySpecified.ql create mode 100644 c/misra/test/rules/RULE-8-11/ArrayExternalLinkageSizeExplicitlySpecified.expected create mode 100644 c/misra/test/rules/RULE-8-11/ArrayExternalLinkageSizeExplicitlySpecified.qlref create mode 100644 c/misra/test/rules/RULE-8-11/test.c diff --git a/c/misra/src/rules/RULE-8-11/ArrayExternalLinkageSizeExplicitlySpecified.ql b/c/misra/src/rules/RULE-8-11/ArrayExternalLinkageSizeExplicitlySpecified.ql new file mode 100644 index 0000000000..ada18c805d --- /dev/null +++ b/c/misra/src/rules/RULE-8-11/ArrayExternalLinkageSizeExplicitlySpecified.ql @@ -0,0 +1,28 @@ +/** + * @id c/misra/array-external-linkage-size-explicitly-specified + * @name RULE-8-11: When an array with external linkage is declared, its size should be explicitly specified + * @description Declaring an array without an explicit size disallows the compiler and static + * checkers from doing array bounds analysis and can lead to less readable, unsafe + * code. + * @kind problem + * @precision very-high + * @problem.severity error + * @tags external/misra/id/rule-8-11 + * correctness + * readability + * external/misra/obligation/advisory + */ + +import cpp +import codingstandards.c.misra +import codingstandards.cpp.Identifiers + +from VariableDeclarationEntry v, ArrayType t +where + not isExcluded(v, Declarations6Package::arrayExternalLinkageSizeExplicitlySpecifiedQuery()) and + v.getDeclaration() instanceof ExternalIdentifiers and + v.getType() = t and + not exists(t.getSize()) and + //this rule applies to non-defining declarations only + not v.isDefinition() +select v, "Array declared without explicit size." diff --git a/c/misra/test/rules/RULE-8-11/ArrayExternalLinkageSizeExplicitlySpecified.expected b/c/misra/test/rules/RULE-8-11/ArrayExternalLinkageSizeExplicitlySpecified.expected new file mode 100644 index 0000000000..1ee0702d2b --- /dev/null +++ b/c/misra/test/rules/RULE-8-11/ArrayExternalLinkageSizeExplicitlySpecified.expected @@ -0,0 +1 @@ +| test.c:2:12:2:13 | declaration of a1 | Array declared without explicit size. | diff --git a/c/misra/test/rules/RULE-8-11/ArrayExternalLinkageSizeExplicitlySpecified.qlref b/c/misra/test/rules/RULE-8-11/ArrayExternalLinkageSizeExplicitlySpecified.qlref new file mode 100644 index 0000000000..4f011bfafe --- /dev/null +++ b/c/misra/test/rules/RULE-8-11/ArrayExternalLinkageSizeExplicitlySpecified.qlref @@ -0,0 +1 @@ +rules/RULE-8-11/ArrayExternalLinkageSizeExplicitlySpecified.ql \ No newline at end of file diff --git a/c/misra/test/rules/RULE-8-11/test.c b/c/misra/test/rules/RULE-8-11/test.c new file mode 100644 index 0000000000..6b7d8be8c7 --- /dev/null +++ b/c/misra/test/rules/RULE-8-11/test.c @@ -0,0 +1,6 @@ +extern int a[1]; // COMPLIANT +extern int a1[]; // NON_COMPLIANT +extern int a2[] = { + 1}; // COMPLIANT - this rule applies to non-defining declarations only +static int a3[]; // COMPLIANT - not external linkage +int a4[]; // COMPLIANT - is a definition \ No newline at end of file diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll index 3cae883ebf..b8556b7089 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll @@ -6,7 +6,8 @@ import codingstandards.cpp.exclusions.RuleMetadata newtype Declarations6Query = TFunctionDeclaredImplicitlyQuery() or TIdentifiersWithExternalLinkageNotUniqueQuery() or - TIdentifiersWithInternalLinkageNotUniqueQuery() + TIdentifiersWithInternalLinkageNotUniqueQuery() or + TArrayExternalLinkageSizeExplicitlySpecifiedQuery() predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleId, string category) { query = @@ -35,6 +36,15 @@ predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleI "c/misra/identifiers-with-internal-linkage-not-unique" and ruleId = "RULE-5-9" and category = "advisory" + or + query = + // `Query` instance for the `arrayExternalLinkageSizeExplicitlySpecified` query + Declarations6Package::arrayExternalLinkageSizeExplicitlySpecifiedQuery() and + queryId = + // `@id` for the `arrayExternalLinkageSizeExplicitlySpecified` query + "c/misra/array-external-linkage-size-explicitly-specified" and + ruleId = "RULE-8-11" and + category = "advisory" } module Declarations6Package { @@ -58,4 +68,11 @@ module Declarations6Package { // `Query` type for `identifiersWithInternalLinkageNotUnique` query TQueryC(TDeclarations6PackageQuery(TIdentifiersWithInternalLinkageNotUniqueQuery())) } + + Query arrayExternalLinkageSizeExplicitlySpecifiedQuery() { + //autogenerate `Query` type + result = + // `Query` type for `arrayExternalLinkageSizeExplicitlySpecified` query + TQueryC(TDeclarations6PackageQuery(TArrayExternalLinkageSizeExplicitlySpecifiedQuery())) + } } diff --git a/rule_packages/c/Declarations6.json b/rule_packages/c/Declarations6.json index 8120db9ac5..369c7152dc 100644 --- a/rule_packages/c/Declarations6.json +++ b/rule_packages/c/Declarations6.json @@ -59,6 +59,26 @@ } ], "title": "Identifiers that define objects or functions with internal linkage should be unique" + }, + "RULE-8-11": { + "properties": { + "obligation": "advisory" + }, + "queries": [ + { + "description": "Declaring an array without an explicit size disallows the compiler and static checkers from doing array bounds analysis and can lead to less readable, unsafe code.", + "kind": "problem", + "name": "When an array with external linkage is declared, its size should be explicitly specified", + "precision": "very-high", + "severity": "error", + "short_name": "ArrayExternalLinkageSizeExplicitlySpecified", + "tags": [ + "correctness", + "readability" + ] + } + ], + "title": "When an array with external linkage is declared, its size should be explicitly specified" } } } \ No newline at end of file diff --git a/rules.csv b/rules.csv index 0aa41f4992..eb02047df1 100644 --- a/rules.csv +++ b/rules.csv @@ -656,7 +656,7 @@ c,MISRA-C-2012,RULE-8-7,Yes,Advisory,,,Functions and objects should not be defin c,MISRA-C-2012,RULE-8-8,Yes,Required,,,The static storage class specifier shall be used in all declarations of objects and functions that have internal linkage,M3-3-2,Declarations,Medium, c,MISRA-C-2012,RULE-8-9,Yes,Advisory,,,An object should be defined at block scope if its identifier only appears in a single function,M3-4-1,Declarations,Medium, c,MISRA-C-2012,RULE-8-10,Yes,Required,,,An inline function shall be declared with the static storage class,,Declarations,Medium, -c,MISRA-C-2012,RULE-8-11,Yes,Advisory,,,"When an array with external linkage is declared, its size should be explicitly specified",,Declarations,Medium, +c,MISRA-C-2012,RULE-8-11,Yes,Advisory,,,"When an array with external linkage is declared, its size should be explicitly specified",,Declarations6,Medium, c,MISRA-C-2012,RULE-8-12,Yes,Required,,,"Within an enumerator list, the value of an implicitly-specified enumeration constant shall be unique",,Declarations,Medium, c,MISRA-C-2012,RULE-8-13,Yes,Advisory,,,A pointer should point to a const-qualified type whenever possible,,Pointers1,Medium, c,MISRA-C-2012,RULE-8-14,Yes,Required,,,The restrict type qualifier shall not be used,,Banned,Easy, From 7b1024ebc839118d222145e92ad2bf64832e1325 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Tue, 10 Jan 2023 12:54:28 -0500 Subject: [PATCH 06/11] Declarations6: fix formatting testcase RULE-5-9 --- .../IdentifiersWithInternalLinkageNotUnique.expected | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/c/misra/test/rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.expected b/c/misra/test/rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.expected index 6fc2c006a2..f5e22f36f3 100644 --- a/c/misra/test/rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.expected +++ b/c/misra/test/rules/RULE-5-9/IdentifiersWithInternalLinkageNotUnique.expected @@ -1,4 +1,4 @@ -| test1.c:3:12:3:13 | g1 | Identifier conflicts with identifier $@ with internal linkage. | test.c:2:12:2:13 | g1 | g1 | -| test1.c:5:13:5:13 | f | Identifier conflicts with identifier $@ with internal linkage. | test.c:3:13:3:13 | f | f | -| test1.c:6:7:6:7 | g | Identifier conflicts with identifier $@ with internal linkage. | test1.c:2:12:2:12 | g | g | -| test1.c:11:7:11:7 | g | Identifier conflicts with identifier $@ with internal linkage. | test1.c:2:12:2:12 | g | g | +| test1.c:2:12:2:13 | g1 | Identifier conflicts with identifier $@ with internal linkage. | test.c:1:12:1:13 | g1 | g1 | +| test1.c:4:13:4:13 | f | Identifier conflicts with identifier $@ with internal linkage. | test.c:2:13:2:13 | f | f | +| test1.c:5:7:5:7 | g | Identifier conflicts with identifier $@ with internal linkage. | test1.c:1:12:1:12 | g | g | +| test1.c:10:7:10:7 | g | Identifier conflicts with identifier $@ with internal linkage. | test1.c:1:12:1:12 | g | g | From afc029e92964d21a51a496f0d8c1406f021bd58b Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Tue, 10 Jan 2023 13:20:47 -0500 Subject: [PATCH 07/11] Declarations6: add RULE-8-7 --- .../ShouldNotBeDefinedWithExternalLinkage.ql | 46 +++++++++++++++++++ ...ldNotBeDefinedWithExternalLinkage.expected | 3 ++ ...houldNotBeDefinedWithExternalLinkage.qlref | 1 + c/misra/test/rules/RULE-8-7/test.c | 9 ++++ c/misra/test/rules/RULE-8-7/test.h | 7 +++ c/misra/test/rules/RULE-8-7/test1.c | 5 ++ .../cpp/exclusions/c/Declarations6.qll | 19 +++++++- rule_packages/c/Declarations6.json | 21 +++++++++ rules.csv | 2 +- 9 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 c/misra/src/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.ql create mode 100644 c/misra/test/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.expected create mode 100644 c/misra/test/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.qlref create mode 100644 c/misra/test/rules/RULE-8-7/test.c create mode 100644 c/misra/test/rules/RULE-8-7/test.h create mode 100644 c/misra/test/rules/RULE-8-7/test1.c diff --git a/c/misra/src/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.ql b/c/misra/src/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.ql new file mode 100644 index 0000000000..e5649400c8 --- /dev/null +++ b/c/misra/src/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.ql @@ -0,0 +1,46 @@ +/** + * @id c/misra/should-not-be-defined-with-external-linkage + * @name RULE-8-7: Functions and objects should not be defined with external linkage if they are referenced in only one + * @description Declarations with external linkage that are referenced in only one translation unit + * can indicate an intention to only have those identifiers accessible in that + * translation unit and accidental future accesses in other translation units can lead + * to confusing program behaviour. + * @kind problem + * @precision very-high + * @problem.severity error + * @tags external/misra/id/rule-8-7 + * correctness + * maintainability + * readability + * external/misra/obligation/advisory + */ + +import cpp +import codingstandards.c.misra +import codingstandards.cpp.Identifiers +import codingstandards.cpp.Scope + +/** + * Re-introduce function calls into access description as + * "any reference" + */ +class Reference extends NameQualifiableElement { + Reference() { + this instanceof Access or + this instanceof FunctionCall + } +} + +from ExternalIdentifiers e, Reference a1, TranslationUnit t1 +where + not isExcluded(e, Declarations6Package::shouldNotBeDefinedWithExternalLinkageQuery()) and + (a1.(Access).getTarget() = e or a1.(FunctionCall).getTarget() = e) and + a1.getFile() = t1 and + //not accessed in any other translation unit + not exists(TranslationUnit t2, Reference a2 | + not t1 = t2 and + (a2.(Access).getTarget() = e or a2.(FunctionCall).getTarget() = e) and + a2.getFile() = t2 + ) +select e, "Declaration with external linkage is accessed in only one translation unit $@.", a1, + a1.toString() diff --git a/c/misra/test/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.expected b/c/misra/test/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.expected new file mode 100644 index 0000000000..e36706411a --- /dev/null +++ b/c/misra/test/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.expected @@ -0,0 +1,3 @@ +| test.h:2:12:2:13 | i1 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:4:5:4:6 | i1 | i1 | +| test.h:3:5:3:6 | i2 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:5:5:5:6 | i2 | i2 | +| test.h:5:13:5:14 | f2 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:7:5:7:6 | call to f2 | call to f2 | diff --git a/c/misra/test/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.qlref b/c/misra/test/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.qlref new file mode 100644 index 0000000000..8b41068eef --- /dev/null +++ b/c/misra/test/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.qlref @@ -0,0 +1 @@ +rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.ql \ No newline at end of file diff --git a/c/misra/test/rules/RULE-8-7/test.c b/c/misra/test/rules/RULE-8-7/test.c new file mode 100644 index 0000000000..02f5e27daf --- /dev/null +++ b/c/misra/test/rules/RULE-8-7/test.c @@ -0,0 +1,9 @@ +#include "test.h" +void f(){ + i = 0; + i1 = 0; + i2 = 0; + f1(); + f2(); + f3(); +} \ No newline at end of file diff --git a/c/misra/test/rules/RULE-8-7/test.h b/c/misra/test/rules/RULE-8-7/test.h new file mode 100644 index 0000000000..692bb8e3db --- /dev/null +++ b/c/misra/test/rules/RULE-8-7/test.h @@ -0,0 +1,7 @@ +extern int i; // COMPLIANT - accessed multiple translation units +extern int i1; // NON_COMPLIANT - accessed one translation unit +int i2; // NON_COMPLIANT - accessed one translation unit +extern void f1(); // COMPLIANT - accessed multiple translation units +extern void f2(); // NON_COMPLIANT - accessed one translation unit +static void f3(); // COMPLIANT - internal linkage +extern void f3(); // COMPLIANT - internal linkage \ No newline at end of file diff --git a/c/misra/test/rules/RULE-8-7/test1.c b/c/misra/test/rules/RULE-8-7/test1.c new file mode 100644 index 0000000000..ff2354db16 --- /dev/null +++ b/c/misra/test/rules/RULE-8-7/test1.c @@ -0,0 +1,5 @@ +#include "test.h" +void f(){ + i = 0; + f1(); +} \ No newline at end of file diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll index b8556b7089..3fbae509f3 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll @@ -7,7 +7,8 @@ newtype Declarations6Query = TFunctionDeclaredImplicitlyQuery() or TIdentifiersWithExternalLinkageNotUniqueQuery() or TIdentifiersWithInternalLinkageNotUniqueQuery() or - TArrayExternalLinkageSizeExplicitlySpecifiedQuery() + TArrayExternalLinkageSizeExplicitlySpecifiedQuery() or + TShouldNotBeDefinedWithExternalLinkageQuery() predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleId, string category) { query = @@ -45,6 +46,15 @@ predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleI "c/misra/array-external-linkage-size-explicitly-specified" and ruleId = "RULE-8-11" and category = "advisory" + or + query = + // `Query` instance for the `shouldNotBeDefinedWithExternalLinkage` query + Declarations6Package::shouldNotBeDefinedWithExternalLinkageQuery() and + queryId = + // `@id` for the `shouldNotBeDefinedWithExternalLinkage` query + "c/misra/should-not-be-defined-with-external-linkage" and + ruleId = "RULE-8-7" and + category = "advisory" } module Declarations6Package { @@ -75,4 +85,11 @@ module Declarations6Package { // `Query` type for `arrayExternalLinkageSizeExplicitlySpecified` query TQueryC(TDeclarations6PackageQuery(TArrayExternalLinkageSizeExplicitlySpecifiedQuery())) } + + Query shouldNotBeDefinedWithExternalLinkageQuery() { + //autogenerate `Query` type + result = + // `Query` type for `shouldNotBeDefinedWithExternalLinkage` query + TQueryC(TDeclarations6PackageQuery(TShouldNotBeDefinedWithExternalLinkageQuery())) + } } diff --git a/rule_packages/c/Declarations6.json b/rule_packages/c/Declarations6.json index 369c7152dc..84410719b0 100644 --- a/rule_packages/c/Declarations6.json +++ b/rule_packages/c/Declarations6.json @@ -79,6 +79,27 @@ } ], "title": "When an array with external linkage is declared, its size should be explicitly specified" + }, + "RULE-8-7": { + "properties": { + "obligation": "advisory" + }, + "queries": [ + { + "description": "Declarations with external linkage that are referenced in only one translation unit can indicate an intention to only have those identifiers accessible in that translation unit and accidental future accesses in other translation units can lead to confusing program behaviour.", + "kind": "problem", + "name": "Functions and objects should not be defined with external linkage if they are referenced in only one", + "precision": "very-high", + "severity": "error", + "short_name": "ShouldNotBeDefinedWithExternalLinkage", + "tags": [ + "correctness", + "maintainability", + "readability" + ] + } + ], + "title": "Functions and objects should not be defined with external linkage if they are referenced in only one translation unit" } } } \ No newline at end of file diff --git a/rules.csv b/rules.csv index eb02047df1..abb94b1b76 100644 --- a/rules.csv +++ b/rules.csv @@ -652,7 +652,7 @@ c,MISRA-C-2012,RULE-8-3,Yes,Required,,,All declarations of an object or function c,MISRA-C-2012,RULE-8-4,Yes,Required,,,A compatible declaration shall be visible when an object or function with external linkage is defined,,Declarations4,Medium, c,MISRA-C-2012,RULE-8-5,Yes,Required,,,An external object or function shall be declared once in one and only one file,,Declarations,Medium, c,MISRA-C-2012,RULE-8-6,Yes,Required,,,An identifier with external linkage shall have exactly one external definition,M3-2-4,Declarations4,Import, -c,MISRA-C-2012,RULE-8-7,Yes,Advisory,,,Functions and objects should not be defined with external linkage if they are referenced in only one translation unit,,Declarations,Medium, +c,MISRA-C-2012,RULE-8-7,Yes,Advisory,,,Functions and objects should not be defined with external linkage if they are referenced in only one translation unit,,Declarations6,Medium, c,MISRA-C-2012,RULE-8-8,Yes,Required,,,The static storage class specifier shall be used in all declarations of objects and functions that have internal linkage,M3-3-2,Declarations,Medium, c,MISRA-C-2012,RULE-8-9,Yes,Advisory,,,An object should be defined at block scope if its identifier only appears in a single function,M3-4-1,Declarations,Medium, c,MISRA-C-2012,RULE-8-10,Yes,Required,,,An inline function shall be declared with the static storage class,,Declarations,Medium, From 6672156b23fb4ea049800b101c347515858c3c12 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Tue, 10 Jan 2023 14:03:58 -0500 Subject: [PATCH 08/11] Declarations6: fix formatting testcase RULE-8-7 --- .../ShouldNotBeDefinedWithExternalLinkage.expected | 6 +++--- c/misra/test/rules/RULE-8-7/test.c | 14 +++++++------- c/misra/test/rules/RULE-8-7/test1.c | 6 +++--- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/c/misra/test/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.expected b/c/misra/test/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.expected index e36706411a..b6a53071d9 100644 --- a/c/misra/test/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.expected +++ b/c/misra/test/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.expected @@ -1,3 +1,3 @@ -| test.h:2:12:2:13 | i1 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:4:5:4:6 | i1 | i1 | -| test.h:3:5:3:6 | i2 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:5:5:5:6 | i2 | i2 | -| test.h:5:13:5:14 | f2 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:7:5:7:6 | call to f2 | call to f2 | +| test.h:2:12:2:13 | i1 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:4:3:4:4 | i1 | i1 | +| test.h:3:5:3:6 | i2 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:5:3:5:4 | i2 | i2 | +| test.h:5:13:5:14 | f2 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:7:3:7:4 | call to f2 | call to f2 | diff --git a/c/misra/test/rules/RULE-8-7/test.c b/c/misra/test/rules/RULE-8-7/test.c index 02f5e27daf..b2cc2a0684 100644 --- a/c/misra/test/rules/RULE-8-7/test.c +++ b/c/misra/test/rules/RULE-8-7/test.c @@ -1,9 +1,9 @@ #include "test.h" -void f(){ - i = 0; - i1 = 0; - i2 = 0; - f1(); - f2(); - f3(); +void f() { + i = 0; + i1 = 0; + i2 = 0; + f1(); + f2(); + f3(); } \ No newline at end of file diff --git a/c/misra/test/rules/RULE-8-7/test1.c b/c/misra/test/rules/RULE-8-7/test1.c index ff2354db16..77377e78df 100644 --- a/c/misra/test/rules/RULE-8-7/test1.c +++ b/c/misra/test/rules/RULE-8-7/test1.c @@ -1,5 +1,5 @@ #include "test.h" -void f(){ - i = 0; - f1(); +void f() { + i = 0; + f1(); } \ No newline at end of file From 5ee232d96e180f512f8b8865f83f856fc76ef662 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Wed, 11 Jan 2023 10:47:09 -0500 Subject: [PATCH 09/11] Declarations6: add RULE-8-10 --- .../InlineFunctionNotDeclaredStaticStorage.ql | 24 +++++++++++++++++++ ...eFunctionNotDeclaredStaticStorage.expected | 3 +++ ...lineFunctionNotDeclaredStaticStorage.qlref | 1 + c/misra/test/rules/RULE-8-10/test.c | 5 ++++ .../cpp/exclusions/c/Declarations6.qll | 17 +++++++++++++ rule_packages/c/Declarations6.json | 19 +++++++++++++++ rules.csv | 2 +- 7 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 c/misra/src/rules/RULE-8-10/InlineFunctionNotDeclaredStaticStorage.ql create mode 100644 c/misra/test/rules/RULE-8-10/InlineFunctionNotDeclaredStaticStorage.expected create mode 100644 c/misra/test/rules/RULE-8-10/InlineFunctionNotDeclaredStaticStorage.qlref create mode 100644 c/misra/test/rules/RULE-8-10/test.c diff --git a/c/misra/src/rules/RULE-8-10/InlineFunctionNotDeclaredStaticStorage.ql b/c/misra/src/rules/RULE-8-10/InlineFunctionNotDeclaredStaticStorage.ql new file mode 100644 index 0000000000..47e80912af --- /dev/null +++ b/c/misra/src/rules/RULE-8-10/InlineFunctionNotDeclaredStaticStorage.ql @@ -0,0 +1,24 @@ +/** + * @id c/misra/inline-function-not-declared-static-storage + * @name RULE-8-10: An inline function shall be declared with the static storage class + * @description Declaring an inline function with external linkage can lead to undefined or + * incorrect program behaviour. + * @kind problem + * @precision very-high + * @problem.severity error + * @tags external/misra/id/rule-8-10 + * correctness + * external/misra/obligation/required + */ + +import cpp +import codingstandards.c.misra +import codingstandards.cpp.Identifiers + +from FunctionDeclarationEntry f +where + not isExcluded(f, Declarations6Package::inlineFunctionNotDeclaredStaticStorageQuery()) and + f.getFunction() instanceof InterestingIdentifiers and + f.getFunction().isInline() and + not f.hasSpecifier("static") +select f, "Inline function not explicitly declared static." diff --git a/c/misra/test/rules/RULE-8-10/InlineFunctionNotDeclaredStaticStorage.expected b/c/misra/test/rules/RULE-8-10/InlineFunctionNotDeclaredStaticStorage.expected new file mode 100644 index 0000000000..fe6b1799d6 --- /dev/null +++ b/c/misra/test/rules/RULE-8-10/InlineFunctionNotDeclaredStaticStorage.expected @@ -0,0 +1,3 @@ +| test.c:2:20:2:21 | declaration of f1 | Inline function not explicitly declared static. | +| test.c:3:13:3:14 | declaration of f2 | Inline function not explicitly declared static. | +| test.c:4:20:4:20 | declaration of f | Inline function not explicitly declared static. | diff --git a/c/misra/test/rules/RULE-8-10/InlineFunctionNotDeclaredStaticStorage.qlref b/c/misra/test/rules/RULE-8-10/InlineFunctionNotDeclaredStaticStorage.qlref new file mode 100644 index 0000000000..fc081c2570 --- /dev/null +++ b/c/misra/test/rules/RULE-8-10/InlineFunctionNotDeclaredStaticStorage.qlref @@ -0,0 +1 @@ +rules/RULE-8-10/InlineFunctionNotDeclaredStaticStorage.ql \ No newline at end of file diff --git a/c/misra/test/rules/RULE-8-10/test.c b/c/misra/test/rules/RULE-8-10/test.c new file mode 100644 index 0000000000..56048b33ec --- /dev/null +++ b/c/misra/test/rules/RULE-8-10/test.c @@ -0,0 +1,5 @@ +static inline void f(); // COMPLIANT +extern inline void f1(); // NON_COMPLIANT +inline void f2(); // NON_COMPLIANT +extern inline void f(); // NON_COMPLIANT -while this will be internal linkage it + // is less clear than explicitly specifying static diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll index 3fbae509f3..a6c70d7ddc 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll @@ -7,6 +7,7 @@ newtype Declarations6Query = TFunctionDeclaredImplicitlyQuery() or TIdentifiersWithExternalLinkageNotUniqueQuery() or TIdentifiersWithInternalLinkageNotUniqueQuery() or + TInlineFunctionNotDeclaredStaticStorageQuery() or TArrayExternalLinkageSizeExplicitlySpecifiedQuery() or TShouldNotBeDefinedWithExternalLinkageQuery() @@ -38,6 +39,15 @@ predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleI ruleId = "RULE-5-9" and category = "advisory" or + query = + // `Query` instance for the `inlineFunctionNotDeclaredStaticStorage` query + Declarations6Package::inlineFunctionNotDeclaredStaticStorageQuery() and + queryId = + // `@id` for the `inlineFunctionNotDeclaredStaticStorage` query + "c/misra/inline-function-not-declared-static-storage" and + ruleId = "RULE-8-10" and + category = "required" + or query = // `Query` instance for the `arrayExternalLinkageSizeExplicitlySpecified` query Declarations6Package::arrayExternalLinkageSizeExplicitlySpecifiedQuery() and @@ -79,6 +89,13 @@ module Declarations6Package { TQueryC(TDeclarations6PackageQuery(TIdentifiersWithInternalLinkageNotUniqueQuery())) } + Query inlineFunctionNotDeclaredStaticStorageQuery() { + //autogenerate `Query` type + result = + // `Query` type for `inlineFunctionNotDeclaredStaticStorage` query + TQueryC(TDeclarations6PackageQuery(TInlineFunctionNotDeclaredStaticStorageQuery())) + } + Query arrayExternalLinkageSizeExplicitlySpecifiedQuery() { //autogenerate `Query` type result = diff --git a/rule_packages/c/Declarations6.json b/rule_packages/c/Declarations6.json index 84410719b0..6928bb5820 100644 --- a/rule_packages/c/Declarations6.json +++ b/rule_packages/c/Declarations6.json @@ -60,6 +60,25 @@ ], "title": "Identifiers that define objects or functions with internal linkage should be unique" }, + "RULE-8-10": { + "properties": { + "obligation": "required" + }, + "queries": [ + { + "description": "Declaring an inline function with external linkage can lead to undefined or incorrect program behaviour.", + "kind": "problem", + "name": "An inline function shall be declared with the static storage class", + "precision": "very-high", + "severity": "error", + "short_name": "InlineFunctionNotDeclaredStaticStorage", + "tags": [ + "correctness" + ] + } + ], + "title": "An inline function shall be declared with the static storage class" + }, "RULE-8-11": { "properties": { "obligation": "advisory" diff --git a/rules.csv b/rules.csv index abb94b1b76..5fb29a1ca4 100644 --- a/rules.csv +++ b/rules.csv @@ -655,7 +655,7 @@ c,MISRA-C-2012,RULE-8-6,Yes,Required,,,An identifier with external linkage shall c,MISRA-C-2012,RULE-8-7,Yes,Advisory,,,Functions and objects should not be defined with external linkage if they are referenced in only one translation unit,,Declarations6,Medium, c,MISRA-C-2012,RULE-8-8,Yes,Required,,,The static storage class specifier shall be used in all declarations of objects and functions that have internal linkage,M3-3-2,Declarations,Medium, c,MISRA-C-2012,RULE-8-9,Yes,Advisory,,,An object should be defined at block scope if its identifier only appears in a single function,M3-4-1,Declarations,Medium, -c,MISRA-C-2012,RULE-8-10,Yes,Required,,,An inline function shall be declared with the static storage class,,Declarations,Medium, +c,MISRA-C-2012,RULE-8-10,Yes,Required,,,An inline function shall be declared with the static storage class,,Declarations6,Medium, c,MISRA-C-2012,RULE-8-11,Yes,Advisory,,,"When an array with external linkage is declared, its size should be explicitly specified",,Declarations6,Medium, c,MISRA-C-2012,RULE-8-12,Yes,Required,,,"Within an enumerator list, the value of an implicitly-specified enumeration constant shall be unique",,Declarations,Medium, c,MISRA-C-2012,RULE-8-13,Yes,Advisory,,,A pointer should point to a const-qualified type whenever possible,,Pointers1,Medium, From bfdb8ed874f816d9211dcd4979f4554953f0b224 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Wed, 11 Jan 2023 14:38:56 -0500 Subject: [PATCH 10/11] Declarations6: add RULE-18-7 --- .../DCL38-C/DeclaringAFlexibleArrayMember.ql | 21 ++---------- c/common/src/codingstandards/c/Variable.qll | 32 +++++++++++++++++++ .../RULE-18-7/FlexibleArrayMembersDeclared.ql | 19 +++++++++++ .../FlexibleArrayMembersDeclared.expected | 1 + .../FlexibleArrayMembersDeclared.qlref | 1 + c/misra/test/rules/RULE-18-7/test.c | 20 ++++++++++++ .../cpp/exclusions/c/Declarations6.qll | 17 ++++++++++ rule_packages/c/Declarations6.json | 19 +++++++++++ rules.csv | 2 +- 9 files changed, 112 insertions(+), 20 deletions(-) create mode 100644 c/misra/src/rules/RULE-18-7/FlexibleArrayMembersDeclared.ql create mode 100644 c/misra/test/rules/RULE-18-7/FlexibleArrayMembersDeclared.expected create mode 100644 c/misra/test/rules/RULE-18-7/FlexibleArrayMembersDeclared.qlref create mode 100644 c/misra/test/rules/RULE-18-7/test.c diff --git a/c/cert/src/rules/DCL38-C/DeclaringAFlexibleArrayMember.ql b/c/cert/src/rules/DCL38-C/DeclaringAFlexibleArrayMember.ql index b5f7087ab0..e9fa3f1017 100644 --- a/c/cert/src/rules/DCL38-C/DeclaringAFlexibleArrayMember.ql +++ b/c/cert/src/rules/DCL38-C/DeclaringAFlexibleArrayMember.ql @@ -15,29 +15,12 @@ import cpp import codingstandards.c.cert - -/** - * A member with the type array that is last in a struct - * includes any sized array (either specified or not) - */ -class FlexibleArrayMember extends MemberVariable { - Struct s; - - FlexibleArrayMember() { - this.getType() instanceof ArrayType and - this.getDeclaringType() = s and - not exists(int i, int j | - s.getAMember(i) = this and - exists(s.getAMember(j)) and - j > i - ) - } -} +import codingstandards.c.Variable from VariableDeclarationEntry m, ArrayType a where not isExcluded(m, Declarations2Package::declaringAFlexibleArrayMemberQuery()) and m.getType() = a and - m.getVariable() instanceof FlexibleArrayMember and + m.getVariable() instanceof FlexibleArrayMemberCandidate and a.getArraySize() = 1 select m, "Incorrect syntax used for declaring this flexible array member." diff --git a/c/common/src/codingstandards/c/Variable.qll b/c/common/src/codingstandards/c/Variable.qll index c6061c99c1..5f4492fdd6 100644 --- a/c/common/src/codingstandards/c/Variable.qll +++ b/c/common/src/codingstandards/c/Variable.qll @@ -6,3 +6,35 @@ class VlaVariable extends Variable { /* Extractor workaround do determine if a VLA array has the specifier volatile.*/ override predicate isVolatile() { this.getType().(ArrayType).getBaseType().isVolatile() } } + +/** + * A flexible array member + * ie member with the type array that is last in a struct + * has no size specified + */ +class FlexibleArrayMember extends FlexibleArrayMemberCandidate { + FlexibleArrayMember() { + exists(ArrayType t | + this.getType() = t and + not exists(t.getSize()) + ) + } +} + +/** + * A member with the type array that is last in a struct + * includes any sized array (either specified or not) + */ +class FlexibleArrayMemberCandidate extends MemberVariable { + Struct s; + + FlexibleArrayMemberCandidate() { + this.getType() instanceof ArrayType and + this.getDeclaringType() = s and + not exists(int i, int j | + s.getAMember(i) = this and + exists(s.getAMember(j)) and + j > i + ) + } +} diff --git a/c/misra/src/rules/RULE-18-7/FlexibleArrayMembersDeclared.ql b/c/misra/src/rules/RULE-18-7/FlexibleArrayMembersDeclared.ql new file mode 100644 index 0000000000..5ae2c9b9c6 --- /dev/null +++ b/c/misra/src/rules/RULE-18-7/FlexibleArrayMembersDeclared.ql @@ -0,0 +1,19 @@ +/** + * @id c/misra/flexible-array-members-declared + * @name RULE-18-7: Flexible array members shall not be declared + * @description The use of flexible array members can lead to unexpected program behaviour. + * @kind problem + * @precision very-high + * @problem.severity error + * @tags external/misra/id/rule-18-7 + * correctness + * external/misra/obligation/required + */ + +import cpp +import codingstandards.c.misra +import codingstandards.c.Variable + +from FlexibleArrayMember f +where not isExcluded(f, Declarations6Package::flexibleArrayMembersDeclaredQuery()) +select f, "Flexible array member declared." diff --git a/c/misra/test/rules/RULE-18-7/FlexibleArrayMembersDeclared.expected b/c/misra/test/rules/RULE-18-7/FlexibleArrayMembersDeclared.expected new file mode 100644 index 0000000000..377a9ca487 --- /dev/null +++ b/c/misra/test/rules/RULE-18-7/FlexibleArrayMembersDeclared.expected @@ -0,0 +1 @@ +| test.c:8:7:8:7 | b | Flexible array member declared. | diff --git a/c/misra/test/rules/RULE-18-7/FlexibleArrayMembersDeclared.qlref b/c/misra/test/rules/RULE-18-7/FlexibleArrayMembersDeclared.qlref new file mode 100644 index 0000000000..b0f0bc00e6 --- /dev/null +++ b/c/misra/test/rules/RULE-18-7/FlexibleArrayMembersDeclared.qlref @@ -0,0 +1 @@ +rules/RULE-18-7/FlexibleArrayMembersDeclared.ql \ No newline at end of file diff --git a/c/misra/test/rules/RULE-18-7/test.c b/c/misra/test/rules/RULE-18-7/test.c new file mode 100644 index 0000000000..385a947090 --- /dev/null +++ b/c/misra/test/rules/RULE-18-7/test.c @@ -0,0 +1,20 @@ +struct s { + int a; + int b[1]; // COMPLIANT +}; + +struct s1 { + int a; + int b[]; // NON_COMPLIANT +}; + +struct s2 { + int a; + int b[2]; // COMPLIANT +}; + +struct s3 { + int a; + int b[1]; // COMPLIANT + int a1; +}; \ No newline at end of file diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll index a6c70d7ddc..b9db9f986f 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll @@ -5,6 +5,7 @@ import codingstandards.cpp.exclusions.RuleMetadata newtype Declarations6Query = TFunctionDeclaredImplicitlyQuery() or + TFlexibleArrayMembersDeclaredQuery() or TIdentifiersWithExternalLinkageNotUniqueQuery() or TIdentifiersWithInternalLinkageNotUniqueQuery() or TInlineFunctionNotDeclaredStaticStorageQuery() or @@ -21,6 +22,15 @@ predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleI ruleId = "RULE-17-3" and category = "mandatory" or + query = + // `Query` instance for the `flexibleArrayMembersDeclared` query + Declarations6Package::flexibleArrayMembersDeclaredQuery() and + queryId = + // `@id` for the `flexibleArrayMembersDeclared` query + "c/misra/flexible-array-members-declared" and + ruleId = "RULE-18-7" and + category = "required" + or query = // `Query` instance for the `identifiersWithExternalLinkageNotUnique` query Declarations6Package::identifiersWithExternalLinkageNotUniqueQuery() and @@ -75,6 +85,13 @@ module Declarations6Package { TQueryC(TDeclarations6PackageQuery(TFunctionDeclaredImplicitlyQuery())) } + Query flexibleArrayMembersDeclaredQuery() { + //autogenerate `Query` type + result = + // `Query` type for `flexibleArrayMembersDeclared` query + TQueryC(TDeclarations6PackageQuery(TFlexibleArrayMembersDeclaredQuery())) + } + Query identifiersWithExternalLinkageNotUniqueQuery() { //autogenerate `Query` type result = diff --git a/rule_packages/c/Declarations6.json b/rule_packages/c/Declarations6.json index 6928bb5820..d25256cc03 100644 --- a/rule_packages/c/Declarations6.json +++ b/rule_packages/c/Declarations6.json @@ -20,6 +20,25 @@ ], "title": "A function shall not be declared implicitly" }, + "RULE-18-7": { + "properties": { + "obligation": "required" + }, + "queries": [ + { + "description": "The use of flexible array members can lead to unexpected program behaviour.", + "kind": "problem", + "name": "Flexible array members shall not be declared", + "precision": "very-high", + "severity": "error", + "short_name": "FlexibleArrayMembersDeclared", + "tags": [ + "correctness" + ] + } + ], + "title": "Flexible array members shall not be declared" + }, "RULE-5-8": { "properties": { "obligation": "required" diff --git a/rules.csv b/rules.csv index 5fb29a1ca4..85295275ca 100644 --- a/rules.csv +++ b/rules.csv @@ -725,7 +725,7 @@ c,MISRA-C-2012,RULE-18-3,Yes,Required,,,"The relational operators >, >=, < and < c,MISRA-C-2012,RULE-18-4,Yes,Advisory,,,"The +, -, += and -= operators should not be applied to an expression of pointer type",M5-0-15,Pointers1,Medium, c,MISRA-C-2012,RULE-18-5,Yes,Advisory,,,Declarations should contain no more than two levels of pointer nesting,A5-0-3,Pointers1,Import, c,MISRA-C-2012,RULE-18-6,Yes,Required,,,The address of an object with automatic storage shall not be copied to another object that persists after the first object has ceased to exist,M7-5-2,Pointers1,Import, -c,MISRA-C-2012,RULE-18-7,Yes,Required,,,Flexible array members shall not be declared,,Declarations,Medium, +c,MISRA-C-2012,RULE-18-7,Yes,Required,,,Flexible array members shall not be declared,,Declarations6,Medium, c,MISRA-C-2012,RULE-18-8,Yes,Required,,,Variable-length array types shall not be used,,Declarations,Medium, c,MISRA-C-2012,RULE-19-1,Yes,Mandatory,,,An object shall not be assigned or copied to an overlapping object,M0-2-1,Contracts,Hard, c,MISRA-C-2012,RULE-19-2,Yes,Advisory,,,The union keyword should not be used,A9-5-1,Banned,Import, From c5741b485f598f0c5ea5beb41fb0ecf49f0b11e5 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Wed, 18 Jan 2023 10:55:55 -0500 Subject: [PATCH 11/11] Declarations6: add implementation note to RULE-5-9 --- rule_packages/c/Declarations6.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rule_packages/c/Declarations6.json b/rule_packages/c/Declarations6.json index d25256cc03..166d0c568b 100644 --- a/rule_packages/c/Declarations6.json +++ b/rule_packages/c/Declarations6.json @@ -74,7 +74,10 @@ "tags": [ "maintainability", "readability" - ] + ], + "implementation_scope": { + "description": "This rule does not explicitly check for the exception of inline functions in header files across multiple translation units as the CodeQL database already represents these as the same entity." + } } ], "title": "Identifiers that define objects or functions with internal linkage should be unique"