9 MISRA C RULES

Supported and unsupported MISRA C rules

A number of MISRA C rules leave room for interpretation. Other rules can only be checked in a limited way. In such cases the implementation decisions and possible restrictions for these rules are listed.

1. The code shall conform to standard C, without language extensions

* 2. Other languages should only be used with an interface standard

3. Inline assembly is only allowed in dedicated C functions

* 4. Provision should be made for appropriate run-time checking

5. Only use characters and escape sequences defined by ISO C

* 6. Character values shall be restricted to a subset of ISO 106460-1

7. Trigraphs shall not be used

8. Multibyte characters and wide string literals shall not be used

9. Comments shall not be nested

10. Sections of code should not be "commented out"

11. Identifiers shall not rely on significance of more than 31 characters

12. The same identifier shall not be used in multiple name spaces

13. Specific-length typedefs should be used instead of the basic types

14. Use 'unsigned char' or 'signed char' instead of plain 'char'

* 15. Floating point implementations should comply with a standard

16. The bit representation of floating point numbers shall not be used

17. "typedef" names shall not be reused

18. Numeric constants should be suffixed to indicate type

19. Octal constants (other than zero) shall not be used

20. All object and function identifiers shall be declared before use

21. Identifiers shall not hide identifiers in an outer scope

22. Declarations should be at function scope where possible

* 23. All declarations at file scope should be static where possible

24. Identifiers shall not have both internal and external linkage

* 25. Identifiers with external linkage shall have exactly one definition

26. Multiple declarations for objects or functions shall be compatible

* 27. External objects should not be declared in more than one file

28. The "register" storage class specifier should not be used

29. The use of a tag shall agree with its declaration

30. All automatics shall be initialized before being used

31. Braces shall be used in the initialization of arrays and structures

32. Only the first, or all enumeration constants may be initialized

33. The right hand operand of && or || shall not contain side effects

34. The operands of a logical && or || shall be primary expressions

35. Assignment operators shall not be used in Boolean expressions

36. Logical operators should not be confused with bitwise operators

37. Bitwise operations shall not be performed on signed integers

38. A shift count shall be between 0 and the operand width minus 1

39. The unary minus shall not be applied to an unsigned expression

40. "sizeof" should not be used on expressions with side effects

* 41. The implementation of integer division should be documented

42. The comma operator shall only be used in a "for" condition

43. Don't use implicit conversions which may result in information loss

44. Redundant explicit casts should not be used

45. Type casting from any type to or from pointers shall not be used

46. The value of an expression shall be evaluation order independent

47. No dependence should be placed on operator precedence rules

48. Mixed arithmetic should use explicit casting

49. Tests of a (non-Boolean) value against 0 should be made explicit

50. F.P. variables shall not be tested for exact equality or inequality

51. Constant unsigned integer expressions should not wrap-around

52. There shall be no unreachable code

53. All non-null statements shall have a side-effect

54. A null statement shall only occur on a line by itself

55. Labels should not be used

56. The "goto" statement shall not be used

57. The "continue" statement shall not be used

58. The "break" statement shall not be used (except in a "switch")

59. An "if" or loop body shall always be enclosed in braces

60. All "if", "else if" constructs should contain a final "else"

61. Every non-empty "case" clause shall be terminated with a "break"

62. All "switch" statements should contain a final "default" case

63. A "switch" expression should not represent a Boolean case

64. Every "switch" shall have at least one "case"

65. Floating point variables shall not be used as loop counters

66. A "for" should only contain expressions concerning loop control

67. Iterator variables should not be modified in a "for" loop

68. Functions shall always be declared at file scope

69. Functions with variable number of arguments shall not be used

70. Functions shall not call themselves, either directly or indirectly

71. Function prototypes shall be visible at the definition and call

72. The function prototype of the declaration shall match the definition

73. Identifiers shall be given for all prototype parameters or for none

74. Parameter identifiers shall be identical for declaration/definition

75. Every function shall have an explicit return type

76. Functions with no parameters shall have a "void" parameter list

77. An actual parameter type shall be compatible with the prototype

78. The number of actual parameters shall match the prototype

79. The values returned by "void" functions shall not be used

80. Void expressions shall not be passed as function parameters

81. "const" should be used for reference parameters not modified

82. A function should have a single point of exit

83. Every exit point shall have a "return" of the declared return type

84. For "void" functions, "return" shall not have an expression

85. Function calls with no parameters should have empty parentheses

86. If a function returns error information, it should be tested

87. #include shall only be preceded by another directives or comments

88. Non-standard characters shall not occur in #include directives

89. #include shall be followed by either <filename> or "filename"

90. Plain macros shall only be used for constants/qualifiers/specifiers

91. Macros shall not be #define'd and #undef'd within a block

92. #undef should not be used

93. A function should be used in preference to a function-like macro

94. A function-like macro shall not be used without all arguments

95. Macro arguments shall not contain pre-preprocessing directives

96. Macro definitions/parameters should be enclosed in parentheses

97. Don't use undefined identifiers in pre-processing directives

98. A macro definition shall contain at most one # or ## operator

99. All uses of the #pragma directive shall be documented

100. "defined" shall only be used in one of the two standard forms

101. Pointer arithmetic should not be used

102. No more than 2 levels of pointer indirection should be used

103. No relational operators between pointers to different objects

104. Non-constant pointers to functions shall not be used

105. Functions assigned to the same pointer shall be of identical type

106. Automatic address may not be assigned to a longer lived object

107. The null pointer shall not be de-referenced

108. All struct/union members shall be fully specified

109. Overlapping variable storage shall not be used

110. Unions shall not be used to access the sub-parts of larger types

111. Bit fields shall have type "unsigned int" or "signed int"

112. Bit fields of type "signed int" shall be at least 2 bits long

113. All struct/union members shall be named

114. Reserved and standard library names shall not be redefined

115. Standard library function names shall not be reused

* 116. Production libraries shall comply with the MISRA C restrictions

* 117. The validity of library function parameters shall be checked

118. Dynamic heap memory allocation shall not be used

119. The error indicator "errno" shall not be used

120. The macro "offsetof" shall not be used

121. <locale.h> and the "setlocale" function shall not be used

122. The "setjmp" and "longjmp" functions shall not be used

123. The signal handling facilities of <signal.h> shall not be used

124. The <stdio.h> library shall not be used in production code

125. The functions atof/atoi/atol shall not be used

126. The functions abort/exit/getenv/system shall not be used

127. The time handling functions of library <time.h> shall not be used

* = Not supported by the TASKING TriCore C compiler

See also section 5.8 , C Code Checking: MISRA C, in Chapter Using the Compiler of the User's Guide.


Copyright © 2003 Altium BV