RuleDescriptionKPI
arithOperationsOnVoidPointervarname’ is of type ‘vartype’. When using void pointers in calculations the behaviour is undefined. Arithmetic operations on ‘void *’ is a GNU C extension which defines the ‘sizeof(void)’ to be 1.Robustness
AssignmentAddressToIntegerAssigning a pointer to an integer (int/long/etc) is not portable across different platforms and compilers. For example in 32-bit Windows and linux they are same width but in 64-bit Windows and linux they are of different width. In worst case you end up assigning 64-bit address to 32-bit integer. The safe way is to store addresses only in pointer types (or typedefs like uintptr_t).Portability
AssignmentIntegerToAddressAssigning an integer (int/long/etc) to a pointer is not portable across different platforms and compilers. For example in 32-bit Windows and linux they are same width but in 64-bit Windows and linux they are of different width. In worst case you end up assigning 64-bit integer to 32-bit pointer. The safe way is to store addresses only in pointer types (or typedefs like uintptr_t).Portability
CastAddressToIntegerAtReturnReturning an address value in a function with integer (int/long/etc) return type is not portable across different platforms and compilers. For example in 32-bit Windows and Linux they are same width but in 64-bit Windows and Linux they are of different width. In worst case you end up casting 64-bit address down to 32-bit integer. The safe way is to always return an integer.Portability
CastIntegerToAddressAtReturnReturning an integer (int/long/etc) in a function with pointer return type is not portable across different platforms and compilers. For example in 32-bit Windows and Linux they are same width but in 64-bit Windows and Linux they are of different width. In worst case you end up casting 64-bit integer down to 32-bit pointer. The safe way is to always return a pointer.Portability
fflushOnInputStreamfflush() called on input stream ‘stdin’ may result in undefined behaviour on non-linux systems.Portability
functionStaticThe member function ‘class::function’ can be made a static function. Making a function static can bring a performance benefit since no ‘this’ instance is passed to the function. This change should not cause compiler errors but it does not necessarily make sense conceptually. Think about your design and the task of the function first – is it a function that must not access members of class instances?Efficiency
invalidPointerCastCasting between float* and double* which have an incompatible binary data representation.Portability
memsetClassFloatUsing memset() on class which contains a floating point number. This is not portable because memset() sets each byte of a block of memory to a specific value and the actual representation of a floating-point value is implementation defined. Note: In case of an IEEE754-1985 compatible implementation setting all bits to zero results in the value 0.0.Portability
memsetFloatThe 2nd memset() argument ‘varname’ is a float its representation is implementation defined. memset() is used to set each byte of a block of memory to a specific value and the actual representation of a floating-point value is implementation defined.Portability
passedByValueParameter ‘parametername’ is passed by value. It could be passed as a (const) reference which is usually faster and recommended in C++.Efficiency
pointerOutOfBoundsUndefined behaviour pointer arithmetic ” is out of bounds. From chapter 6.5.6 in the C specification:12When an expression that has integer type is added to or subtracted from a pointer ..” and then “If both the pointer operand and the result point to elements of the same array object or one past the last element of the array object the evaluation shall not produce an overflowPortability
postfixOperatorPrefix ++/– operators should be preferred for non-primitive types. Pre-increment/decrement can be more efficient than post-increment/decrement. Post-increment/decrement usually involves keeping a copy of the previous value around and adds a little extra code.Efficiency
redundantCopyBuffer ‘var’ is being written before its old content has been used.Maintainability
redundantCopyLocalConstThe const variable ‘varname’ is assigned a copy of the data. You can avoid the unnecessary data copying by converting ‘varname’ to const reference.Resource Utilization
shiftNegativeLHSShifting a negative value is technically undefined behaviourRobustness
sizeofDereferencedVoidPointer*varname’ is of type ‘void’ the behaviour of ‘sizeof(void)’ is not covered by the ISO C standard. A value for ‘sizeof(void)’ is defined only as part of a GNU C extension which defines ‘sizeof(void)’ to be 1.Robustness
sizeofVoidBehaviour of ‘sizeof(void)’ is not covered by the ISO C standard. A value for ‘sizeof(void)’ is defined only as part of a GNU C extension which defines ‘sizeof(void)’ to be 1.Robustness
stlcstrParamThe conversion from const char* as returned by c_str() to std::string creates an unnecessary string copy. Solve that by directly passing the string.Efficiency
stlcstrReturnThe conversion from const char* as returned by c_str() to std::string creates an unnecessary string copy. Solve that by directly returning the string.Efficiency
stlIfStrFindEither inefficient or wrong usage of string::find(). string::compare() will be faster if string::find’s result is compared with 0 because it will not scan the whole string. If your intention is to check that there are no findings in the string you should compare with std::string::npos.Efficiency
stlSizeChecking for ‘list’ emptiness might be inefficient. Using list.empty() instead of list.size() can be faster. list.size() can take linear time but list.empty() is guaranteed to take constant time.Efficiency
unknownSignCharArrayIndexchar’ type used as array index. Values greater that 127 will be treated depending on whether ‘char’ is signed or unsigned on target platform.Robustness
useInitializationListWhen an object of a class is created the constructors of all member variables are called consecutively in the order the variables are declared even if you don’t explicitly write them to the initialization list. You could avoid assigning ‘variable’ a value by passing the value to the constructor in the initialization list.Efficiency
uselessCallsSubstrIneffective call of function ‘substr’ because it returns a copy of the object. Use operator= instead.Accuracy
uselessCallsSwapThe ‘swap()’ function has no logical effect when given itself as parameter (str.swap(str)). As it is currently the code is inefficient. Is the object or the parameter wrong here?Efficiency
varFuncNullUBPassing NULL after the last typed argument to a variadic function leads to undefined behaviour.12The C99 standard in section 7.15.1.1 states that if the type used by va_arg() is not compatible with the type of the actual next argument (as promoted according to the default argument promotions) the behavior is undefined.12The value of the NULL macro is an implementation-defined null pointer constant (7.17) which can be any integer constant expression with the value 0 or such an expression casted to (void*) (6.3.2.3). This includes values like 0 0L or even 0LL.12In practice on common architectures this will cause real crashes if sizeof(int) != sizeof(void*) and NULL is defined to 0 or any other null pointer constant that promotes to int.12To reproduce you might be able to use this little code example on 64bit platforms. If the output includes ERROR” the sentinel had only 4 out of 8 bytes initialized to zero and was not detected as the final argument to stop argument processing via va_arg(). Changing the 0 to (void*)0 or 0L will make the “ERROR” output go away.12#include 12#include 1212void f(char *s …) {12 va_list apRobustness
misra-c2012-1.3There shall be no occurrence of undefined or critical unspecified behaviorRobustness
misra-c2012-2.1A project shall not contain unreachable codeRobustness
misra-c2012-2.2There shall be no dead codeRobustness
misra-c2012-2.4A project should not contain unused tag declarationsRobustness
misra-c2012-2.6A function should not contain unused label declarationsRobustness
misra-c2012-3.1The character sequences /* an // shall not be used within a commentRobustness
misra-c2012-4.1Octal and hexadecimal escape sequences shall be terminatedRobustness
misra-c2012-5.1External identifiers shall be distinctRobustness
misra-c2012-5.3An identifier declared in an inner scope shall not hide an identifier declared in an outer scopeRobustness
misra-c2012-5.4Macro identifiers shall be distinctRobustness
misra-c2012-5.5Identifiers shall be distinct from macro namesRobustness
misra-c2012-7.1Octal constants shall not be usedRobustness
misra-c2012-7.3The lowercase character ‘l’ shall not be used in a literal suffixRobustness
misra-c2012-8.3All declarations of an object or function shall use the same names and type qualifiersRobustness
misra-c2012-8.11When an array with external linkage is declared, its size should be explicitly specifiedRobustness
misra-c2012-8.12Within an enumerator list, the value of an implicitly-specified enumeration constant shall be uniqueRobustness
misra-c2012-8.14The restrict type qualifier shall not be usedRobustness
misra-c2012-9.1The value of an object with automatic storage duration shall not be read before it has been setRobustness
misra-c2012-9.5Where designated initializers are used to initialize an array object the size of the array shall be specified explicitlyRobustness
misra-c2012-10.4Both operands of an operator in which the usual arithmetic conversions are performed shall have the same essential type categoryRobustness
misra-c2012-10.6The value of a composite expression shall not be assigned to an object with wider essential typeRobustness
misra-c2012-10.8The value of a composite expression shall not be cast to a different essential type category or a wider essential typeRobustness
misra-c2012-11.3A cast shall not be performed between a pointer to object type and a pointer to a different object typeRobustness
misra-c2012-11.4A conversion should not be performed between a pointer to object and an integer typeRobustness
misra-c2012-11.5A conversion should not be performed from pointer to void into pointer to objectRobustness
misra-c2012-11.6A cast shall not be performed between pointer to void and an arithmetic typeRobustness
misra-c2012-11.7A cast shall not be performed between pointer to object and a noninteger arithmetic typeRobustness
misra-c2012-11.8A cast shall not remove any const or volatile qualification from the type pointed to by a pointerRobustness
misra-c2012-11.9The macro NULL shall be the only per mitted form of integer null pointer constantRobustness
misra-c2012-12.1The precedence of operators within expressions should be made explicitRobustness
misra-c2012-12.2The right-hand operand of a shift operator shall lie in the range zero to one less than the width in bits of the essential type of the left-hand operandRobustness
misra-c2012-12.3The comma operator should not be usedRobustness
misra-c2012-12.4Evaluation of constant expressions should not lead to unsigned integer wrap-aroundRobustness
misra-c2012-13.1Initializer lists shall not contain persistent side effectsRobustness
misra-c2012-13.2The value of an expression and its persistent side effects shall be the same under all permitted evaluation ordersRobustness
misra-c2012-13.3A full expression containing an increment (++) or decrement (–) operator should have no other potential side effects other than that caused by the increment or decrement operatorRobustness
misra-c2012-13.4The result of an assignment operator should not be usedRobustness
misra-c2012-13.5The right-hand operand of a logical && or || operator shall not contain persistent side effectsRobustness
misra-c2012-13.6The operand of the sizeof operator shall not contain any expression which has potential side effectsRobustness
misra-c2012-14.1A loop counter shall not have essentially floating typeRobustness
misra-c2012-14.2A for loop shall be well-formedRobustness
misra-c2012-14.4The controlling expression of an if statement and the controlling expression of an iteration- statement shall have essentially Boolean typeRobustness
misra-c2012-15.1The goto statement should not be usedRobustness
misra-c2012-15.2The goto statement shall jump to a label declared later in the same functionRobustness
misra-c2012-15.3Any label referenced by a goto statement shall be declared in the same block, or in any block enclosing the goto statementRobustness
misra-c2012-15.5A function should have a single point of exit at the endRobustness
misra-c2012-15.6The body of an iteration-statement or a selection-statement shall be a compound-statementRobustness
misra-c2012-15.7All if … else if constructs shall be terminated with an else statementRobustness
misra-c2012-16.2A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statementRobustness
misra-c2012-16.3An unconditional break statement shall terminate every switch-clauseRobustness
misra-c2012-16.4Every switch statement shall have a default labelRobustness
misra-c2012-16.5A default label shall appear as either the first or the last switch label of a switch statementRobustness
misra-c2012-16.6Every switch statement shall have at least two switch-clausesRobustness
misra-c2012-16.7A switch-expression shall not have essentially Boolean typeRobustness
misra-c2012-17.1The features of shall not be usedRobustness
misra-c2012-17.5The function argument corresponding to a parameter declared to have an array type shall have an appropriate number of elementsRobustness
misra-c2012-17.6The declaration of an array parameter shall not contain the static keyword between the [ ]Robustness
misra-c2012-17.8A function parameter should not be modifiedRobustness
misra-c2012-18.1A pointer resulting from arithmetic on a pointer operand shall address an element of the same array as that pointer operandRobustness
misra-c2012-18.5Declarations should contain no more than two levels of pointer nestingRobustness
misra-c2012-18.6The address of an object with automatic storage shall not be copied to another object that persists after the first object has ceased to existRobustness
misra-c2012-18.8Variable-length array types shall not be usedRobustness
misra-c2012-19.2The union keyword should not be usedRobustness
misra-c2012-20.1#include directives should only be preceded by preprocessor directives or commentsRobustness
misra-c2012-20.2The ‘, ” or characters and the /* or // character sequences shall not occur in a header fi le nameRobustness
misra-c2012-20.3The #include directive shall be followed by either a or “filename” sequenceRobustness
misra-c2012-20.4A macro shall not be defined with the same name as a keywordRobustness
misra-c2012-20.5#undef should not be usedRobustness
misra-c2012-21.3The memory allocation and deallocation functions of shall not be usedRobustness
misra-c2012-21.4The standard header file shall not be usedRobustness
misra-c2012-21.5The standard header file shall not be usedRobustness
misra-c2012-21.7The atof, atoi, atol and atoll functions of shall not be usedRobustness
misra-c2012-21.8The library functions abort, exit, getenv and system of shall not be usedRobustness
misra-c2012-21.9The library functions bsearch and qsort of shall not be usedRobustness
misra-c2012-21.11The standard header file shall not be usedRobustness
misra-c2012-22.1All resources obtained dynamically by means of Standard Library functions shall be explicitly releasedRobustness
misra-c2012-22.2A block of memory shall only be freed if it was allocated by means of a Standard Library functionRobustness
misra-c2012-22.4There shall be no attempt to write to a stream which has been opened as read-onlyRobustness
misra-c2012-22.6The value of a pointer to a FILE shall not be used after the associated stream has been closedRobustness