diff --git a/cpp/common/src/codingstandards/cpp/exclusions/cpp/Memory2.qll b/cpp/common/src/codingstandards/cpp/exclusions/cpp/Memory2.qll new file mode 100644 index 000000000..d7bfbba99 --- /dev/null +++ b/cpp/common/src/codingstandards/cpp/exclusions/cpp/Memory2.qll @@ -0,0 +1,26 @@ +//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ +import cpp +import RuleMetadata +import codingstandards.cpp.exclusions.RuleMetadata + +newtype Memory2Query = TPointerDifferenceTakenBetweenDifferentArraysQuery() + +predicate isMemory2QueryMetadata(Query query, string queryId, string ruleId, string category) { + query = + // `Query` instance for the `pointerDifferenceTakenBetweenDifferentArrays` query + Memory2Package::pointerDifferenceTakenBetweenDifferentArraysQuery() and + queryId = + // `@id` for the `pointerDifferenceTakenBetweenDifferentArrays` query + "cpp/misra/pointer-difference-taken-between-different-arrays" and + ruleId = "RULE-8-7-2" and + category = "required" +} + +module Memory2Package { + Query pointerDifferenceTakenBetweenDifferentArraysQuery() { + //autogenerate `Query` type + result = + // `Query` type for `pointerDifferenceTakenBetweenDifferentArrays` query + TQueryCPP(TMemory2PackageQuery(TPointerDifferenceTakenBetweenDifferentArraysQuery())) + } +} diff --git a/cpp/common/src/codingstandards/cpp/exclusions/cpp/Memory3.qll b/cpp/common/src/codingstandards/cpp/exclusions/cpp/Memory3.qll new file mode 100644 index 000000000..6dce95e89 --- /dev/null +++ b/cpp/common/src/codingstandards/cpp/exclusions/cpp/Memory3.qll @@ -0,0 +1,26 @@ +//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ +import cpp +import RuleMetadata +import codingstandards.cpp.exclusions.RuleMetadata + +newtype Memory3Query = TPointerComparedBetweenDifferentArraysQuery() + +predicate isMemory3QueryMetadata(Query query, string queryId, string ruleId, string category) { + query = + // `Query` instance for the `pointerComparedBetweenDifferentArrays` query + Memory3Package::pointerComparedBetweenDifferentArraysQuery() and + queryId = + // `@id` for the `pointerComparedBetweenDifferentArrays` query + "cpp/misra/pointer-compared-between-different-arrays" and + ruleId = "RULE-8-9-1" and + category = "required" +} + +module Memory3Package { + Query pointerComparedBetweenDifferentArraysQuery() { + //autogenerate `Query` type + result = + // `Query` type for `pointerComparedBetweenDifferentArrays` query + TQueryCPP(TMemory3PackageQuery(TPointerComparedBetweenDifferentArraysQuery())) + } +} diff --git a/cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll b/cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll index b51bc8465..19f143db6 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll @@ -40,6 +40,8 @@ import Linkage2 import Literals import Loops import Macros +import Memory2 +import Memory3 import MoveForward import Naming import Null @@ -105,6 +107,8 @@ newtype TCPPQuery = TLiteralsPackageQuery(LiteralsQuery q) or TLoopsPackageQuery(LoopsQuery q) or TMacrosPackageQuery(MacrosQuery q) or + TMemory2PackageQuery(Memory2Query q) or + TMemory3PackageQuery(Memory3Query q) or TMoveForwardPackageQuery(MoveForwardQuery q) or TNamingPackageQuery(NamingQuery q) or TNullPackageQuery(NullQuery q) or @@ -170,6 +174,8 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat isLiteralsQueryMetadata(query, queryId, ruleId, category) or isLoopsQueryMetadata(query, queryId, ruleId, category) or isMacrosQueryMetadata(query, queryId, ruleId, category) or + isMemory2QueryMetadata(query, queryId, ruleId, category) or + isMemory3QueryMetadata(query, queryId, ruleId, category) or isMoveForwardQueryMetadata(query, queryId, ruleId, category) or isNamingQueryMetadata(query, queryId, ruleId, category) or isNullQueryMetadata(query, queryId, ruleId, category) or diff --git a/cpp/misra/src/rules/RULE-8-7-2/PointerDifferenceTakenBetweenDifferentArrays.ql b/cpp/misra/src/rules/RULE-8-7-2/PointerDifferenceTakenBetweenDifferentArrays.ql new file mode 100644 index 000000000..286814510 --- /dev/null +++ b/cpp/misra/src/rules/RULE-8-7-2/PointerDifferenceTakenBetweenDifferentArrays.ql @@ -0,0 +1,24 @@ +/** + * @id cpp/misra/pointer-difference-taken-between-different-arrays + * @name RULE-8-7-2: Subtraction between pointers shall only be applied to ones that address elements of the same array + * @description Pointer difference should be taken from pointers that belong to a same array. + * @kind path-problem + * @precision high + * @problem.severity error + * @tags external/misra/id/rule-8-7-2 + * scope/system + * correctness + * external/misra/enforcement/undecidable + * external/misra/obligation/required + */ + +import cpp +import codingstandards.cpp.misra +import codingstandards.cpp.rules.donotsubtractpointersaddressingdifferentarrays.DoNotSubtractPointersAddressingDifferentArrays + +class PointerDifferenceTakenBetweenDifferentArraysQuery extends DoNotSubtractPointersAddressingDifferentArraysSharedQuery +{ + PointerDifferenceTakenBetweenDifferentArraysQuery() { + this = Memory2Package::pointerDifferenceTakenBetweenDifferentArraysQuery() + } +} diff --git a/cpp/misra/src/rules/RULE-8-9-1/PointerComparedBetweenDifferentArrays.ql b/cpp/misra/src/rules/RULE-8-9-1/PointerComparedBetweenDifferentArrays.ql new file mode 100644 index 000000000..19f15433f --- /dev/null +++ b/cpp/misra/src/rules/RULE-8-9-1/PointerComparedBetweenDifferentArrays.ql @@ -0,0 +1,24 @@ +/** + * @id cpp/misra/pointer-compared-between-different-arrays + * @name RULE-8-9-1: The built-in relational operators >, >=, < and <= shall not be applied to objects of pointer type + * @description Pointer comparison should be done between ones that belong to a same array. + * @kind path-problem + * @precision high + * @problem.severity error + * @tags external/misra/id/rule-8-9-1 + * scope/system + * correctness + * external/misra/enforcement/undecidable + * external/misra/obligation/required + */ + +import cpp +import codingstandards.cpp.misra +import codingstandards.cpp.rules.donotuserelationaloperatorswithdifferingarrays.DoNotUseRelationalOperatorsWithDifferingArrays + +class PointerComparedBetweenDifferentArraysQuery extends DoNotUseRelationalOperatorsWithDifferingArraysSharedQuery +{ + PointerComparedBetweenDifferentArraysQuery() { + this = Memory3Package::pointerComparedBetweenDifferentArraysQuery() + } +} diff --git a/cpp/misra/test/rules/RULE-8-7-2/PointerDifferenceTakenBetweenDifferentArrays.testref b/cpp/misra/test/rules/RULE-8-7-2/PointerDifferenceTakenBetweenDifferentArrays.testref new file mode 100644 index 000000000..6506d7bed --- /dev/null +++ b/cpp/misra/test/rules/RULE-8-7-2/PointerDifferenceTakenBetweenDifferentArrays.testref @@ -0,0 +1 @@ +cpp/common/test/rules/donotsubtractpointersaddressingdifferentarrays/DoNotSubtractPointersAddressingDifferentArrays.ql \ No newline at end of file diff --git a/cpp/misra/test/rules/RULE-8-9-1/PointerComparedBetweenDifferentArrays.testref b/cpp/misra/test/rules/RULE-8-9-1/PointerComparedBetweenDifferentArrays.testref new file mode 100644 index 000000000..5ca71e38c --- /dev/null +++ b/cpp/misra/test/rules/RULE-8-9-1/PointerComparedBetweenDifferentArrays.testref @@ -0,0 +1 @@ +cpp/common/test/rules/donotuserelationaloperatorswithdifferingarrays/DoNotUseRelationalOperatorsWithDifferingArrays.ql \ No newline at end of file diff --git a/rule_packages/cpp/Memory2.json b/rule_packages/cpp/Memory2.json new file mode 100644 index 000000000..f4631f67a --- /dev/null +++ b/rule_packages/cpp/Memory2.json @@ -0,0 +1,26 @@ +{ + "MISRA-C++-2023": { + "RULE-8-7-2": { + "properties": { + "enforcement": "undecidable", + "obligation": "required" + }, + "queries": [ + { + "description": "Pointer difference should be taken from pointers that belong to a same array.", + "kind": "path-problem", + "name": "Subtraction between pointers shall only be applied to ones that address elements of the same array", + "precision": "high", + "severity": "error", + "short_name": "PointerDifferenceTakenBetweenDifferentArrays", + "shared_implementation_short_name": "DoNotSubtractPointersAddressingDifferentArrays", + "tags": [ + "scope/system", + "correctness" + ] + } + ], + "title": "Pointer difference should be taken from pointers that belong to a same array." + } + } +} diff --git a/rule_packages/cpp/Memory3.json b/rule_packages/cpp/Memory3.json new file mode 100644 index 000000000..c3f09b350 --- /dev/null +++ b/rule_packages/cpp/Memory3.json @@ -0,0 +1,26 @@ +{ + "MISRA-C++-2023": { + "RULE-8-9-1": { + "properties": { + "enforcement": "undecidable", + "obligation": "required" + }, + "queries": [ + { + "description": "Pointer comparison should be done between ones that belong to a same array.", + "kind": "path-problem", + "name": "The built-in relational operators >, >=, < and <= shall not be applied to objects of pointer type", + "precision": "high", + "severity": "error", + "short_name": "PointerComparedBetweenDifferentArrays", + "shared_implementation_short_name": "DoNotUseRelationalOperatorsWithDifferingArrays", + "tags": [ + "scope/system", + "correctness" + ] + } + ], + "title": "Pointer comparison should be done between ones that belong to a same array." + } + } +} diff --git a/rules.csv b/rules.csv index cca308583..3011924c8 100644 --- a/rules.csv +++ b/rules.csv @@ -895,9 +895,9 @@ cpp,MISRA-C++-2023,RULE-8-2-10,Yes,Required,Undecidable,System,"Functions shall cpp,MISRA-C++-2023,RULE-8-2-11,Yes,Required,Decidable,Single Translation Unit,An argument passed via ellipsis shall have an appropriate type,,Preconditions,Easy, cpp,MISRA-C++-2023,RULE-8-3-1,Yes,Advisory,Decidable,Single Translation Unit,The built-in unary - operator should not be applied to an expression of unsigned type,M5-3-2,ImportMisra23,Import, cpp,MISRA-C++-2023,RULE-8-3-2,Yes,Advisory,Decidable,Single Translation Unit,The built-in unary + operator should not be used,,Banned,Easy, -cpp,MISRA-C++-2023,RULE-8-7-1,Yes,Required,Undecidable,System,Pointer arithmetic shall not form an invalid pointer,ARR30-C,Memory,Easy, -cpp,MISRA-C++-2023,RULE-8-7-2,Yes,Required,Undecidable,System,Subtraction between pointers shall only be applied to pointers that address elements of the same array,ARR36-C,Memory,Easy, -cpp,MISRA-C++-2023,RULE-8-9-1,Yes,Required,Undecidable,System,"The built-in relational operators >, >=, < and <= shall not be applied to objects of pointer type, except where they point to elements of the same array",ARR36-C,Memory,Easy, +cpp,MISRA-C++-2023,RULE-8-7-1,Yes,Required,Undecidable,System,Pointer arithmetic shall not form an invalid pointer,ARR30-C,Memory1,Easy, +cpp,MISRA-C++-2023,RULE-8-7-2,Yes,Required,Undecidable,System,Subtraction between pointers shall only be applied to pointers that address elements of the same array,ARR36-C,Memory2,Easy, +cpp,MISRA-C++-2023,RULE-8-9-1,Yes,Required,Undecidable,System,"The built-in relational operators >, >=, < and <= shall not be applied to objects of pointer type, except where they point to elements of the same array",ARR36-C,Memory3,Easy, cpp,MISRA-C++-2023,RULE-8-14-1,Yes,Advisory,Undecidable,System,The right-hand operand of a logical && or operator should not contain persistent side effects,"M5-14-1, RULE-13-5",SideEffects3,Medium, cpp,MISRA-C++-2023,RULE-8-18-1,Yes,Mandatory,Undecidable,System,An object or subobject must not be copied to an overlapping object,"M0-2-1, RULE-19-1",Memory,Hard, cpp,MISRA-C++-2023,RULE-8-18-2,Yes,Advisory,Decidable,Single Translation Unit,The result of an assignment operator should not be used,RULE-13-4,ImportMisra23,Import,