diff --git a/c/misra/src/rules/RULE-6-1/BitFieldsShallOnlyBeDeclaredWithAnAppropriateType.ql b/c/misra/src/rules/RULE-6-1/BitFieldsShallOnlyBeDeclaredWithAnAppropriateType.ql new file mode 100644 index 0000000000..81c7fb69a3 --- /dev/null +++ b/c/misra/src/rules/RULE-6-1/BitFieldsShallOnlyBeDeclaredWithAnAppropriateType.ql @@ -0,0 +1,33 @@ +/** + * @id c/misra/bit-fields-shall-only-be-declared-with-an-appropriate-type + * @name RULE-6-1: Bit-fields shall only be declared with an appropriate type + * @description Declaring bit-fields on types other than appropriate ones causes + * implementation-specific or undefined behavior. + * @kind problem + * @precision very-high + * @problem.severity error + * @tags external/misra/id/rule-6-1 + * external/misra/obligation/required + */ + +import cpp +import codingstandards.c.misra + +predicate isAppropriatePrimitive(Type type) { + /* An appropriate primitive types to which a bit-field can be declared. */ + type instanceof IntType and + ( + type.(IntegralType).isExplicitlySigned() or + type.(IntegralType).isExplicitlyUnsigned() + ) + or + type instanceof BoolType +} + +from BitField bitField +where + not isExcluded(bitField, + BitfieldTypesPackage::bitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery()) and + /* A violation would neither be an appropriate primitive type nor an appropriate typedef. */ + not isAppropriatePrimitive(bitField.getType().resolveTypedefs()) +select bitField, "Bit-field " + bitField + " is declared on type " + bitField.getType() + "." diff --git a/c/misra/src/rules/RULE-6-2/SingleBitNamedBitFieldsOfASignedType.ql b/c/misra/src/rules/RULE-6-2/SingleBitNamedBitFieldsOfASignedType.ql new file mode 100644 index 0000000000..d4be3d6dd2 --- /dev/null +++ b/c/misra/src/rules/RULE-6-2/SingleBitNamedBitFieldsOfASignedType.ql @@ -0,0 +1,34 @@ +/** + * @id c/misra/single-bit-named-bit-fields-of-a-signed-type + * @name RULE-6-2: Single-bit named bit fields shall not be of a signed type + * @description Single-bit named bit fields carry no useful information and therefore should not be + * declared or used. + * @kind problem + * @precision very-high + * @problem.severity error + * @tags external/misra/id/rule-6-2 + * external/misra/obligation/required + */ + +import cpp +import codingstandards.c.misra + +/* + * Check if the DECLARED bit-fields is a single bit, because Rule 6.2 also intends to catch confusion on the programmers' part. Consider: + * + * struct S { + * int32_t x: 1; + * } + * + * In this case, field x is essentially of 32 bits, but is declared as 1 bit and its type int32_t is signed. Therefore, it indicates confusion by the programmer, which is exactly what this rule intends to find. + */ + +from BitField bitField +where + not isExcluded(bitField, BitfieldTypesPackage::singleBitNamedBitFieldsOfASignedTypeQuery()) and + bitField.getDeclaredNumBits() = 1 and // Single-bit, + not bitField.isAnonymous() and // named, + bitField.getType().(IntegralType).isSigned() // but its type is signed. +select bitField, + "Single-bit bit-field named " + bitField.toString() + " has a signed type " + bitField.getType() + + "." diff --git a/c/misra/test/rules/RULE-6-1/BitFieldsShallOnlyBeDeclaredWithAnAppropriateType.expected b/c/misra/test/rules/RULE-6-1/BitFieldsShallOnlyBeDeclaredWithAnAppropriateType.expected new file mode 100644 index 0000000000..aaba0ee30c --- /dev/null +++ b/c/misra/test/rules/RULE-6-1/BitFieldsShallOnlyBeDeclaredWithAnAppropriateType.expected @@ -0,0 +1,4 @@ +| test.c:6:7:6:8 | x1 | Bit-field x1 is declared on type int. | +| test.c:10:15:10:16 | x5 | Bit-field x5 is declared on type signed long. | +| test.c:12:15:12:16 | x6 | Bit-field x6 is declared on type signed char. | +| test.c:14:14:14:15 | x7 | Bit-field x7 is declared on type Color. | diff --git a/c/misra/test/rules/RULE-6-1/BitFieldsShallOnlyBeDeclaredWithAnAppropriateType.qlref b/c/misra/test/rules/RULE-6-1/BitFieldsShallOnlyBeDeclaredWithAnAppropriateType.qlref new file mode 100644 index 0000000000..7000f50ab1 --- /dev/null +++ b/c/misra/test/rules/RULE-6-1/BitFieldsShallOnlyBeDeclaredWithAnAppropriateType.qlref @@ -0,0 +1 @@ +rules/RULE-6-1/BitFieldsShallOnlyBeDeclaredWithAnAppropriateType.ql \ No newline at end of file diff --git a/c/misra/test/rules/RULE-6-1/test.c b/c/misra/test/rules/RULE-6-1/test.c new file mode 100644 index 0000000000..0271ed1e32 --- /dev/null +++ b/c/misra/test/rules/RULE-6-1/test.c @@ -0,0 +1,15 @@ +typedef unsigned int UINT16; + +enum Color { R, G, B }; + +struct SampleStruct { + int x1 : 2; // NON_COMPLIANT - not explicitly signed or unsigned + unsigned int x2 : 2; // COMPILANT - explicitly unsigned + signed int x3 : 2; // COMPILANT - explicitly signed + UINT16 x4 : 2; // COMPLIANT - type alias resolves to a compliant type + signed long x5 : 2; // NON_COMPLIANT - cannot declare bit field for long, even + // if it's signed + signed char x6 : 2; // NON_COMPILANT - cannot declare bit field for char, even + // if it's signed + enum Color x7 : 3; // NON_COMPILANT - cannot declare bit field for enum +} sample_struct; diff --git a/c/misra/test/rules/RULE-6-2/SingleBitNamedBitFieldsOfASignedType.expected b/c/misra/test/rules/RULE-6-2/SingleBitNamedBitFieldsOfASignedType.expected new file mode 100644 index 0000000000..df7677961a --- /dev/null +++ b/c/misra/test/rules/RULE-6-2/SingleBitNamedBitFieldsOfASignedType.expected @@ -0,0 +1,4 @@ +| test.c:4:7:4:8 | x1 | Single-bit bit-field named x1 has a signed type int. | +| test.c:7:14:7:15 | x2 | Single-bit bit-field named x2 has a signed type signed int. | +| test.c:9:7:9:8 | x3 | Single-bit bit-field named x3 has a signed type signed char. | +| test.c:11:7:11:8 | x4 | Single-bit bit-field named x4 has a signed type signed short. | diff --git a/c/misra/test/rules/RULE-6-2/SingleBitNamedBitFieldsOfASignedType.qlref b/c/misra/test/rules/RULE-6-2/SingleBitNamedBitFieldsOfASignedType.qlref new file mode 100644 index 0000000000..50c34f70a7 --- /dev/null +++ b/c/misra/test/rules/RULE-6-2/SingleBitNamedBitFieldsOfASignedType.qlref @@ -0,0 +1 @@ +rules/RULE-6-2/SingleBitNamedBitFieldsOfASignedType.ql \ No newline at end of file diff --git a/c/misra/test/rules/RULE-6-2/test.c b/c/misra/test/rules/RULE-6-2/test.c new file mode 100644 index 0000000000..b3eaa0dd0e --- /dev/null +++ b/c/misra/test/rules/RULE-6-2/test.c @@ -0,0 +1,17 @@ +#include + +struct SampleStruct { + int x1 : 1; // NON_COMPILANT: very likely be signed, but if it's not, the + // query will automatically handle it since we use signed(), not + // isExplicitlySigned(). + signed int x2 : 1; // NON_COMPILANT: single-bit named field with a signed type + signed char + x3 : 1; // NON_COMPILANT: single-bit named field with a signed type + signed short + x4 : 1; // NON_COMPILANT: single-bit named field with a signed type + unsigned int + x5 : 1; // COMPILANT: single-bit named field but with an unsigned type + signed int x6 : 2; // COMPILANT: named field with a signed type but declared + // to carry more than 1 bit + signed char : 1; // COMPILANT: single-bit bit-field but unnamed +} sample_struct; diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/BitfieldTypes.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/BitfieldTypes.qll new file mode 100644 index 0000000000..ef9e94b27a --- /dev/null +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/BitfieldTypes.qll @@ -0,0 +1,44 @@ +//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ +import cpp +import RuleMetadata +import codingstandards.cpp.exclusions.RuleMetadata + +newtype BitfieldTypesQuery = + TBitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery() or + TSingleBitNamedBitFieldsOfASignedTypeQuery() + +predicate isBitfieldTypesQueryMetadata(Query query, string queryId, string ruleId, string category) { + query = + // `Query` instance for the `bitFieldsShallOnlyBeDeclaredWithAnAppropriateType` query + BitfieldTypesPackage::bitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery() and + queryId = + // `@id` for the `bitFieldsShallOnlyBeDeclaredWithAnAppropriateType` query + "c/misra/bit-fields-shall-only-be-declared-with-an-appropriate-type" and + ruleId = "RULE-6-1" and + category = "required" + or + query = + // `Query` instance for the `singleBitNamedBitFieldsOfASignedType` query + BitfieldTypesPackage::singleBitNamedBitFieldsOfASignedTypeQuery() and + queryId = + // `@id` for the `singleBitNamedBitFieldsOfASignedType` query + "c/misra/single-bit-named-bit-fields-of-a-signed-type" and + ruleId = "RULE-6-2" and + category = "required" +} + +module BitfieldTypesPackage { + Query bitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery() { + //autogenerate `Query` type + result = + // `Query` type for `bitFieldsShallOnlyBeDeclaredWithAnAppropriateType` query + TQueryC(TBitfieldTypesPackageQuery(TBitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery())) + } + + Query singleBitNamedBitFieldsOfASignedTypeQuery() { + //autogenerate `Query` type + result = + // `Query` type for `singleBitNamedBitFieldsOfASignedType` query + TQueryC(TBitfieldTypesPackageQuery(TSingleBitNamedBitFieldsOfASignedTypeQuery())) + } +} diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll index ed96e862ee..3fa8156798 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll @@ -3,6 +3,7 @@ import cpp import codingstandards.cpp.exclusions.RuleMetadata //** Import packages for this language **/ import Banned +import BitfieldTypes import Concurrency1 import Concurrency2 import Concurrency3 @@ -47,6 +48,7 @@ import Syntax /** The TQuery type representing this language * */ newtype TCQuery = TBannedPackageQuery(BannedQuery q) or + TBitfieldTypesPackageQuery(BitfieldTypesQuery q) or TConcurrency1PackageQuery(Concurrency1Query q) or TConcurrency2PackageQuery(Concurrency2Query q) or TConcurrency3PackageQuery(Concurrency3Query q) or @@ -91,6 +93,7 @@ newtype TCQuery = /** The metadata predicate * */ predicate isQueryMetadata(Query query, string queryId, string ruleId, string category) { isBannedQueryMetadata(query, queryId, ruleId, category) or + isBitfieldTypesQueryMetadata(query, queryId, ruleId, category) or isConcurrency1QueryMetadata(query, queryId, ruleId, category) or isConcurrency2QueryMetadata(query, queryId, ruleId, category) or isConcurrency3QueryMetadata(query, queryId, ruleId, category) or diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/Types.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/Types.qll new file mode 100644 index 0000000000..79cf3550b1 --- /dev/null +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/Types.qll @@ -0,0 +1,520 @@ +//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ +import cpp +import RuleMetadata +import codingstandards.cpp.exclusions.RuleMetadata + +newtype TypesQuery = + TPreventOrDetectDomainAndRangeErrorsInMathFunctionsQuery() or + TFloatingPointConversionsNotWithinRangeOfNewTypeQuery() or + TFloatingPointOfIntegralValuesLosePrecisionQuery() or + TObjectReprUsedForComparingFloatingPointValuesQuery() or + TEnsureThatUnsignedIntegerOperationsDoNotWrapQuery() or + TIntConversionCausesLostOrMisinterpretedDataQuery() or + TOperationsOnSignedIntegersResultsInOverflowQuery() or + TDivAndModOperationResultsInDivByZeroQuery() or + TExprShiftedByNegativeBitsOrGreaterThanOperandQuery() or + TUseCorrectIntegerPrecisionsQuery() or + TConvertingAPointerToIntegerOrIntegerToPointerQuery() or + TNumericTypedefsNotUsedInPlaceOfBasicNumericalTypesQuery() or + TOperandsOfAnInappropriateEssentialTypeQuery() or + TCharTypeExprsUsedInAddOrSubQuery() or + TAssignmentToIncompatibleEssentialTypeQuery() or + TArithConversionOperandHasDifferentEssTypeCategoryQuery() or + TValueCastToInappropriateEssentialTypeQuery() or + TCompositeExprValueAssignedToObjWithWiderEssTypeQuery() or + TConvertedCompExprOperandHasWiderEssTypeThanOtherQuery() or + TCompExprValCastToIncompatEssTypeQuery() or + TConstExprEvalCausesUnsignedIntWraparoundQuery() or + TArrayTypeParamAtSizeofOperandQuery() or + TLoopCounterHaveEssentiallyFloatingTypeQuery() or + TCtypeFuncNeitherReprAsUnsignedCharNorEOFQuery() or + TMemcmpUsedToCompareNullTerminatedStringsQuery() or + TMemcpyMemmoveMemcmpArgNotPointerToCompatTypesQuery() or + TMemcmpArgNotPtsToSignedUnsignedBooleanEnumEssTypeQuery() or + TBitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery() or + TSingleBitNamedBitFieldsOfASignedTypeQuery() or + TStringLiteralAssignedToObjPtrToConstQualifiedCharQuery() + +predicate isTypesQueryMetadata(Query query, string queryId, string ruleId, string category) { + query = + // `Query` instance for the `preventOrDetectDomainAndRangeErrorsInMathFunctions` query + TypesPackage::preventOrDetectDomainAndRangeErrorsInMathFunctionsQuery() and + queryId = + // `@id` for the `preventOrDetectDomainAndRangeErrorsInMathFunctions` query + "c/cert/prevent-or-detect-domain-and-range-errors-in-math-functions" and + ruleId = "FLP32-C" and + category = "rule" + or + query = + // `Query` instance for the `floatingPointConversionsNotWithinRangeOfNewType` query + TypesPackage::floatingPointConversionsNotWithinRangeOfNewTypeQuery() and + queryId = + // `@id` for the `floatingPointConversionsNotWithinRangeOfNewType` query + "c/cert/floating-point-conversions-not-within-range-of-new-type" and + ruleId = "FLP34-C" and + category = "rule" + or + query = + // `Query` instance for the `floatingPointOfIntegralValuesLosePrecision` query + TypesPackage::floatingPointOfIntegralValuesLosePrecisionQuery() and + queryId = + // `@id` for the `floatingPointOfIntegralValuesLosePrecision` query + "c/cert/floating-point-of-integral-values-lose-precision" and + ruleId = "FLP36-C" and + category = "rule" + or + query = + // `Query` instance for the `objectReprUsedForComparingFloatingPointValues` query + TypesPackage::objectReprUsedForComparingFloatingPointValuesQuery() and + queryId = + // `@id` for the `objectReprUsedForComparingFloatingPointValues` query + "c/cert/object-repr-used-for-comparing-floating-point-values" and + ruleId = "FLP37-C" and + category = "rule" + or + query = + // `Query` instance for the `ensureThatUnsignedIntegerOperationsDoNotWrap` query + TypesPackage::ensureThatUnsignedIntegerOperationsDoNotWrapQuery() and + queryId = + // `@id` for the `ensureThatUnsignedIntegerOperationsDoNotWrap` query + "c/cert/ensure-that-unsigned-integer-operations-do-not-wrap" and + ruleId = "INT30-C" and + category = "rule" + or + query = + // `Query` instance for the `intConversionCausesLostOrMisinterpretedData` query + TypesPackage::intConversionCausesLostOrMisinterpretedDataQuery() and + queryId = + // `@id` for the `intConversionCausesLostOrMisinterpretedData` query + "c/cert/int-conversion-causes-lost-or-misinterpreted-data" and + ruleId = "INT31-C" and + category = "rule" + or + query = + // `Query` instance for the `operationsOnSignedIntegersResultsInOverflow` query + TypesPackage::operationsOnSignedIntegersResultsInOverflowQuery() and + queryId = + // `@id` for the `operationsOnSignedIntegersResultsInOverflow` query + "c/cert/operations-on-signed-integers-results-in-overflow" and + ruleId = "INT32-C" and + category = "rule" + or + query = + // `Query` instance for the `divAndModOperationResultsInDivByZero` query + TypesPackage::divAndModOperationResultsInDivByZeroQuery() and + queryId = + // `@id` for the `divAndModOperationResultsInDivByZero` query + "c/cert/div-and-mod-operation-results-in-div-by-zero" and + ruleId = "INT33-C" and + category = "rule" + or + query = + // `Query` instance for the `exprShiftedByNegativeBitsOrGreaterThanOperand` query + TypesPackage::exprShiftedByNegativeBitsOrGreaterThanOperandQuery() and + queryId = + // `@id` for the `exprShiftedByNegativeBitsOrGreaterThanOperand` query + "c/cert/expr-shifted-by-negative-bits-or-greater-than-operand" and + ruleId = "INT34-C" and + category = "rule" + or + query = + // `Query` instance for the `useCorrectIntegerPrecisions` query + TypesPackage::useCorrectIntegerPrecisionsQuery() and + queryId = + // `@id` for the `useCorrectIntegerPrecisions` query + "c/cert/use-correct-integer-precisions" and + ruleId = "INT35-C" and + category = "rule" + or + query = + // `Query` instance for the `convertingAPointerToIntegerOrIntegerToPointer` query + TypesPackage::convertingAPointerToIntegerOrIntegerToPointerQuery() and + queryId = + // `@id` for the `convertingAPointerToIntegerOrIntegerToPointer` query + "c/cert/converting-a-pointer-to-integer-or-integer-to-pointer" and + ruleId = "INT36-C" and + category = "rule" + or + query = + // `Query` instance for the `numericTypedefsNotUsedInPlaceOfBasicNumericalTypes` query + TypesPackage::numericTypedefsNotUsedInPlaceOfBasicNumericalTypesQuery() and + queryId = + // `@id` for the `numericTypedefsNotUsedInPlaceOfBasicNumericalTypes` query + "c/misra/numeric-typedefs-not-used-in-place-of-basic-numerical-types" and + ruleId = "DIR-4-6" and + category = "advisory" + or + query = + // `Query` instance for the `operandsOfAnInappropriateEssentialType` query + TypesPackage::operandsOfAnInappropriateEssentialTypeQuery() and + queryId = + // `@id` for the `operandsOfAnInappropriateEssentialType` query + "c/misra/operands-of-an-inappropriate-essential-type" and + ruleId = "RULE-10-1" and + category = "required" + or + query = + // `Query` instance for the `charTypeExprsUsedInAddOrSub` query + TypesPackage::charTypeExprsUsedInAddOrSubQuery() and + queryId = + // `@id` for the `charTypeExprsUsedInAddOrSub` query + "c/misra/char-type-exprs-used-in-add-or-sub" and + ruleId = "RULE-10-2" and + category = "required" + or + query = + // `Query` instance for the `assignmentToIncompatibleEssentialType` query + TypesPackage::assignmentToIncompatibleEssentialTypeQuery() and + queryId = + // `@id` for the `assignmentToIncompatibleEssentialType` query + "c/misra/assignment-to-incompatible-essential-type" and + ruleId = "RULE-10-3" and + category = "required" + or + query = + // `Query` instance for the `arithConversionOperandHasDifferentEssTypeCategory` query + TypesPackage::arithConversionOperandHasDifferentEssTypeCategoryQuery() and + queryId = + // `@id` for the `arithConversionOperandHasDifferentEssTypeCategory` query + "c/misra/arith-conversion-operand-has-different-ess-type-category" and + ruleId = "RULE-10-4" and + category = "required" + or + query = + // `Query` instance for the `valueCastToInappropriateEssentialType` query + TypesPackage::valueCastToInappropriateEssentialTypeQuery() and + queryId = + // `@id` for the `valueCastToInappropriateEssentialType` query + "c/misra/value-cast-to-inappropriate-essential-type" and + ruleId = "RULE-10-5" and + category = "advisory" + or + query = + // `Query` instance for the `compositeExprValueAssignedToObjWithWiderEssType` query + TypesPackage::compositeExprValueAssignedToObjWithWiderEssTypeQuery() and + queryId = + // `@id` for the `compositeExprValueAssignedToObjWithWiderEssType` query + "c/misra/composite-expr-value-assigned-to-obj-with-wider-ess-type" and + ruleId = "RULE-10-6" and + category = "required" + or + query = + // `Query` instance for the `convertedCompExprOperandHasWiderEssTypeThanOther` query + TypesPackage::convertedCompExprOperandHasWiderEssTypeThanOtherQuery() and + queryId = + // `@id` for the `convertedCompExprOperandHasWiderEssTypeThanOther` query + "c/misra/converted-comp-expr-operand-has-wider-ess-type-than-other" and + ruleId = "RULE-10-7" and + category = "required" + or + query = + // `Query` instance for the `compExprValCastToIncompatEssType` query + TypesPackage::compExprValCastToIncompatEssTypeQuery() and + queryId = + // `@id` for the `compExprValCastToIncompatEssType` query + "c/misra/comp-expr-val-cast-to-incompat-ess-type" and + ruleId = "RULE-10-8" and + category = "required" + or + query = + // `Query` instance for the `constExprEvalCausesUnsignedIntWraparound` query + TypesPackage::constExprEvalCausesUnsignedIntWraparoundQuery() and + queryId = + // `@id` for the `constExprEvalCausesUnsignedIntWraparound` query + "c/misra/const-expr-eval-causes-unsigned-int-wraparound" and + ruleId = "RULE-12-4" and + category = "advisory" + or + query = + // `Query` instance for the `arrayTypeParamAtSizeofOperand` query + TypesPackage::arrayTypeParamAtSizeofOperandQuery() and + queryId = + // `@id` for the `arrayTypeParamAtSizeofOperand` query + "c/misra/array-type-param-at-sizeof-operand" and + ruleId = "RULE-12-5" and + category = "mandatory" + or + query = + // `Query` instance for the `loopCounterHaveEssentiallyFloatingType` query + TypesPackage::loopCounterHaveEssentiallyFloatingTypeQuery() and + queryId = + // `@id` for the `loopCounterHaveEssentiallyFloatingType` query + "c/misra/loop-counter-have-essentially-floating-type" and + ruleId = "RULE-14-1" and + category = "required" + or + query = + // `Query` instance for the `ctypeFuncNeitherReprAsUnsignedCharNorEOF` query + TypesPackage::ctypeFuncNeitherReprAsUnsignedCharNorEOFQuery() and + queryId = + // `@id` for the `ctypeFuncNeitherReprAsUnsignedCharNorEOF` query + "c/misra/ctype-func-neither-repr-as-unsigned-char-nor-eof" and + ruleId = "RULE-21-13" and + category = "mandatory" + or + query = + // `Query` instance for the `memcmpUsedToCompareNullTerminatedStrings` query + TypesPackage::memcmpUsedToCompareNullTerminatedStringsQuery() and + queryId = + // `@id` for the `memcmpUsedToCompareNullTerminatedStrings` query + "c/misra/memcmp-used-to-compare-null-terminated-strings" and + ruleId = "RULE-21-14" and + category = "required" + or + query = + // `Query` instance for the `memcpyMemmoveMemcmpArgNotPointerToCompatTypes` query + TypesPackage::memcpyMemmoveMemcmpArgNotPointerToCompatTypesQuery() and + queryId = + // `@id` for the `memcpyMemmoveMemcmpArgNotPointerToCompatTypes` query + "c/misra/memcpy-memmove-memcmp-arg-not-pointer-to-compat-types" and + ruleId = "RULE-21-15" and + category = "required" + or + query = + // `Query` instance for the `memcmpArgNotPtsToSignedUnsignedBooleanEnumEssType` query + TypesPackage::memcmpArgNotPtsToSignedUnsignedBooleanEnumEssTypeQuery() and + queryId = + // `@id` for the `memcmpArgNotPtsToSignedUnsignedBooleanEnumEssType` query + "c/misra/memcmp-arg-not-pts-to-signed-unsigned-boolean-enum-ess-type" and + ruleId = "RULE-21-16" and + category = "required" + or + query = + // `Query` instance for the `bitFieldsShallOnlyBeDeclaredWithAnAppropriateType` query + TypesPackage::bitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery() and + queryId = + // `@id` for the `bitFieldsShallOnlyBeDeclaredWithAnAppropriateType` query + "c/misra/bit-fields-shall-only-be-declared-with-an-appropriate-type" and + ruleId = "RULE-6-1" and + category = "required" + or + query = + // `Query` instance for the `singleBitNamedBitFieldsOfASignedType` query + TypesPackage::singleBitNamedBitFieldsOfASignedTypeQuery() and + queryId = + // `@id` for the `singleBitNamedBitFieldsOfASignedType` query + "c/misra/single-bit-named-bit-fields-of-a-signed-type" and + ruleId = "RULE-6-2" and + category = "required" + or + query = + // `Query` instance for the `stringLiteralAssignedToObjPtrToConstQualifiedChar` query + TypesPackage::stringLiteralAssignedToObjPtrToConstQualifiedCharQuery() and + queryId = + // `@id` for the `stringLiteralAssignedToObjPtrToConstQualifiedChar` query + "c/misra/string-literal-assigned-to-obj-ptr-to-const-qualified-char" and + ruleId = "RULE-7-4" and + category = "required" +} + +module TypesPackage { + Query preventOrDetectDomainAndRangeErrorsInMathFunctionsQuery() { + //autogenerate `Query` type + result = + // `Query` type for `preventOrDetectDomainAndRangeErrorsInMathFunctions` query + TQueryC(TTypesPackageQuery(TPreventOrDetectDomainAndRangeErrorsInMathFunctionsQuery())) + } + + Query floatingPointConversionsNotWithinRangeOfNewTypeQuery() { + //autogenerate `Query` type + result = + // `Query` type for `floatingPointConversionsNotWithinRangeOfNewType` query + TQueryC(TTypesPackageQuery(TFloatingPointConversionsNotWithinRangeOfNewTypeQuery())) + } + + Query floatingPointOfIntegralValuesLosePrecisionQuery() { + //autogenerate `Query` type + result = + // `Query` type for `floatingPointOfIntegralValuesLosePrecision` query + TQueryC(TTypesPackageQuery(TFloatingPointOfIntegralValuesLosePrecisionQuery())) + } + + Query objectReprUsedForComparingFloatingPointValuesQuery() { + //autogenerate `Query` type + result = + // `Query` type for `objectReprUsedForComparingFloatingPointValues` query + TQueryC(TTypesPackageQuery(TObjectReprUsedForComparingFloatingPointValuesQuery())) + } + + Query ensureThatUnsignedIntegerOperationsDoNotWrapQuery() { + //autogenerate `Query` type + result = + // `Query` type for `ensureThatUnsignedIntegerOperationsDoNotWrap` query + TQueryC(TTypesPackageQuery(TEnsureThatUnsignedIntegerOperationsDoNotWrapQuery())) + } + + Query intConversionCausesLostOrMisinterpretedDataQuery() { + //autogenerate `Query` type + result = + // `Query` type for `intConversionCausesLostOrMisinterpretedData` query + TQueryC(TTypesPackageQuery(TIntConversionCausesLostOrMisinterpretedDataQuery())) + } + + Query operationsOnSignedIntegersResultsInOverflowQuery() { + //autogenerate `Query` type + result = + // `Query` type for `operationsOnSignedIntegersResultsInOverflow` query + TQueryC(TTypesPackageQuery(TOperationsOnSignedIntegersResultsInOverflowQuery())) + } + + Query divAndModOperationResultsInDivByZeroQuery() { + //autogenerate `Query` type + result = + // `Query` type for `divAndModOperationResultsInDivByZero` query + TQueryC(TTypesPackageQuery(TDivAndModOperationResultsInDivByZeroQuery())) + } + + Query exprShiftedByNegativeBitsOrGreaterThanOperandQuery() { + //autogenerate `Query` type + result = + // `Query` type for `exprShiftedByNegativeBitsOrGreaterThanOperand` query + TQueryC(TTypesPackageQuery(TExprShiftedByNegativeBitsOrGreaterThanOperandQuery())) + } + + Query useCorrectIntegerPrecisionsQuery() { + //autogenerate `Query` type + result = + // `Query` type for `useCorrectIntegerPrecisions` query + TQueryC(TTypesPackageQuery(TUseCorrectIntegerPrecisionsQuery())) + } + + Query convertingAPointerToIntegerOrIntegerToPointerQuery() { + //autogenerate `Query` type + result = + // `Query` type for `convertingAPointerToIntegerOrIntegerToPointer` query + TQueryC(TTypesPackageQuery(TConvertingAPointerToIntegerOrIntegerToPointerQuery())) + } + + Query numericTypedefsNotUsedInPlaceOfBasicNumericalTypesQuery() { + //autogenerate `Query` type + result = + // `Query` type for `numericTypedefsNotUsedInPlaceOfBasicNumericalTypes` query + TQueryC(TTypesPackageQuery(TNumericTypedefsNotUsedInPlaceOfBasicNumericalTypesQuery())) + } + + Query operandsOfAnInappropriateEssentialTypeQuery() { + //autogenerate `Query` type + result = + // `Query` type for `operandsOfAnInappropriateEssentialType` query + TQueryC(TTypesPackageQuery(TOperandsOfAnInappropriateEssentialTypeQuery())) + } + + Query charTypeExprsUsedInAddOrSubQuery() { + //autogenerate `Query` type + result = + // `Query` type for `charTypeExprsUsedInAddOrSub` query + TQueryC(TTypesPackageQuery(TCharTypeExprsUsedInAddOrSubQuery())) + } + + Query assignmentToIncompatibleEssentialTypeQuery() { + //autogenerate `Query` type + result = + // `Query` type for `assignmentToIncompatibleEssentialType` query + TQueryC(TTypesPackageQuery(TAssignmentToIncompatibleEssentialTypeQuery())) + } + + Query arithConversionOperandHasDifferentEssTypeCategoryQuery() { + //autogenerate `Query` type + result = + // `Query` type for `arithConversionOperandHasDifferentEssTypeCategory` query + TQueryC(TTypesPackageQuery(TArithConversionOperandHasDifferentEssTypeCategoryQuery())) + } + + Query valueCastToInappropriateEssentialTypeQuery() { + //autogenerate `Query` type + result = + // `Query` type for `valueCastToInappropriateEssentialType` query + TQueryC(TTypesPackageQuery(TValueCastToInappropriateEssentialTypeQuery())) + } + + Query compositeExprValueAssignedToObjWithWiderEssTypeQuery() { + //autogenerate `Query` type + result = + // `Query` type for `compositeExprValueAssignedToObjWithWiderEssType` query + TQueryC(TTypesPackageQuery(TCompositeExprValueAssignedToObjWithWiderEssTypeQuery())) + } + + Query convertedCompExprOperandHasWiderEssTypeThanOtherQuery() { + //autogenerate `Query` type + result = + // `Query` type for `convertedCompExprOperandHasWiderEssTypeThanOther` query + TQueryC(TTypesPackageQuery(TConvertedCompExprOperandHasWiderEssTypeThanOtherQuery())) + } + + Query compExprValCastToIncompatEssTypeQuery() { + //autogenerate `Query` type + result = + // `Query` type for `compExprValCastToIncompatEssType` query + TQueryC(TTypesPackageQuery(TCompExprValCastToIncompatEssTypeQuery())) + } + + Query constExprEvalCausesUnsignedIntWraparoundQuery() { + //autogenerate `Query` type + result = + // `Query` type for `constExprEvalCausesUnsignedIntWraparound` query + TQueryC(TTypesPackageQuery(TConstExprEvalCausesUnsignedIntWraparoundQuery())) + } + + Query arrayTypeParamAtSizeofOperandQuery() { + //autogenerate `Query` type + result = + // `Query` type for `arrayTypeParamAtSizeofOperand` query + TQueryC(TTypesPackageQuery(TArrayTypeParamAtSizeofOperandQuery())) + } + + Query loopCounterHaveEssentiallyFloatingTypeQuery() { + //autogenerate `Query` type + result = + // `Query` type for `loopCounterHaveEssentiallyFloatingType` query + TQueryC(TTypesPackageQuery(TLoopCounterHaveEssentiallyFloatingTypeQuery())) + } + + Query ctypeFuncNeitherReprAsUnsignedCharNorEOFQuery() { + //autogenerate `Query` type + result = + // `Query` type for `ctypeFuncNeitherReprAsUnsignedCharNorEOF` query + TQueryC(TTypesPackageQuery(TCtypeFuncNeitherReprAsUnsignedCharNorEOFQuery())) + } + + Query memcmpUsedToCompareNullTerminatedStringsQuery() { + //autogenerate `Query` type + result = + // `Query` type for `memcmpUsedToCompareNullTerminatedStrings` query + TQueryC(TTypesPackageQuery(TMemcmpUsedToCompareNullTerminatedStringsQuery())) + } + + Query memcpyMemmoveMemcmpArgNotPointerToCompatTypesQuery() { + //autogenerate `Query` type + result = + // `Query` type for `memcpyMemmoveMemcmpArgNotPointerToCompatTypes` query + TQueryC(TTypesPackageQuery(TMemcpyMemmoveMemcmpArgNotPointerToCompatTypesQuery())) + } + + Query memcmpArgNotPtsToSignedUnsignedBooleanEnumEssTypeQuery() { + //autogenerate `Query` type + result = + // `Query` type for `memcmpArgNotPtsToSignedUnsignedBooleanEnumEssType` query + TQueryC(TTypesPackageQuery(TMemcmpArgNotPtsToSignedUnsignedBooleanEnumEssTypeQuery())) + } + + Query bitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery() { + //autogenerate `Query` type + result = + // `Query` type for `bitFieldsShallOnlyBeDeclaredWithAnAppropriateType` query + TQueryC(TTypesPackageQuery(TBitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery())) + } + + Query singleBitNamedBitFieldsOfASignedTypeQuery() { + //autogenerate `Query` type + result = + // `Query` type for `singleBitNamedBitFieldsOfASignedType` query + TQueryC(TTypesPackageQuery(TSingleBitNamedBitFieldsOfASignedTypeQuery())) + } + + Query stringLiteralAssignedToObjPtrToConstQualifiedCharQuery() { + //autogenerate `Query` type + result = + // `Query` type for `stringLiteralAssignedToObjPtrToConstQualifiedChar` query + TQueryC(TTypesPackageQuery(TStringLiteralAssignedToObjPtrToConstQualifiedCharQuery())) + } +} diff --git a/rule_packages/c/BitfieldTypes.json b/rule_packages/c/BitfieldTypes.json new file mode 100644 index 0000000000..4e93f3371a --- /dev/null +++ b/rule_packages/c/BitfieldTypes.json @@ -0,0 +1,38 @@ +{ + "MISRA-C-2012": { + "RULE-6-1": { + "properties": { + "obligation": "required" + }, + "queries": [ + { + "description": "Declaring bit-fields on types other than appropriate ones causes implementation-specific or undefined behavior.", + "kind": "problem", + "name": "Bit-fields shall only be declared with an appropriate type", + "precision": "very-high", + "severity": "error", + "short_name": "BitFieldsShallOnlyBeDeclaredWithAnAppropriateType", + "tags": [] + } + ], + "title": "Bit-fields shall only be declared with an appropriate type" + }, + "RULE-6-2": { + "properties": { + "obligation": "required" + }, + "queries": [ + { + "description": "Single-bit named bit fields carry no useful information and therefore should not be declared or used.", + "kind": "problem", + "name": "Single-bit named bit fields shall not be of a signed type", + "precision": "very-high", + "severity": "error", + "short_name": "SingleBitNamedBitFieldsOfASignedType", + "tags": [] + } + ], + "title": "Single-bit named bit fields shall not be of a signed type" + } + } +} \ No newline at end of file diff --git a/rules.csv b/rules.csv index 15625c5fba..c455e2ca15 100644 --- a/rules.csv +++ b/rules.csv @@ -640,8 +640,8 @@ c,MISRA-C-2012,RULE-5-6,Yes,Required,,,A typedef name shall be a unique identifi 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,,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-6-1,Yes,Required,,,Bit-fields shall only be declared with an appropriate type,M9-6-4,BitfieldTypes,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,BitfieldTypes,Import, c,MISRA-C-2012,RULE-7-1,Yes,Required,,,Octal constants shall not be used,M2-13-2,Banned,Import, c,MISRA-C-2012,RULE-7-2,Yes,Required,,,A �u� or �U� suffix shall be applied to all integer constants that are represented in an unsigned type,M2-13-3,Syntax,Easy, c,MISRA-C-2012,RULE-7-3,Yes,Required,,,The lowercase character �l� shall not be used in a literal suffix,M2-13-4,Syntax,Easy,