17

Misra c rules

Embed Size (px)

Citation preview

Page 1: Misra c rules
Page 2: Misra c rules

1. ISO 9899 STANDARD 2. COMBINATON OF C AND ASSEMBLY

SHOULD SUPPORT BY COMPILER 4. RUN TIME CHECKING

5. Use IOS C standard escape sequences\n, break, goto

6. ISO 10646-1 rules for char types 7. don’t use tri graphs

??-=~, ??)=] 8. no multi byte char & wide string

literalsJapanese, Chinese characters

Page 3: Misra c rules

9. no nested comments 10. not to comment code part

11. Identifiers should not be >31

14. char type should be signed or unsigned

16. no bit representation of floating point

Use only decimal format 17. typedef names shall not be reused

declare only once in header file

Page 4: Misra c rules

18. use as bit_44 (suffix) 19. octal constants shall not be used

c=052 052=42 in decimal0 is used to represent for octal representation

20. declarations of functions before using

21. namespace shall not have same spellings

24. no two identifiers should have internal & external linkages

static i=0; extern i=0;

Page 5: Misra c rules

25. An identifier with an external linkage shall have exactly one external definition

26. If objects are declared more than once they shall have compatible declarations

delay function in multiple locations

30. automatic variables have garbage values

31. Braces shall be used to indicate and match the structure in the non-zero initialisation of arrays and structures

Page 6: Misra c rules
Page 7: Misra c rules

Bit wise operations should not be Confused Performed on signed int

38. shift operations 16 bit integer should shift only in 0-15 range

39. Unary minus t=100 and –t represents -100

40. size of operator If(sizeof(y)==4) ok If(sizeof(f(z))==4) not ok

41. Integer division based on compiler A=3/2 A=3/-2 A=3%2 A/=2 A%=-2

42. comma operator should be used only in ctrl expressions

Page 8: Misra c rules

43. implicit conversions results in loss of information Int32 I; int16 j,a;a=i*j;

47. t=a*b+c/q; not ok

t=(a*b)+(c/q); ok 48. int i=1,j=3; float k;

K=i/j; to get precise value 50. Float f=0.1234, g=0.1234;

If(f==g) not to use

Page 9: Misra c rules

52. every function should execute at least once 54. if(i==k); while(1); Not to use

Labels in switch statements Goto(avoid) Continue Break (use only in switch)

If, if else, while must enclose in braces If, ifelse must have final else Every case of switch ends with break One default and one case is needed in switch Switch(x=y) don’t use Don’t use floating values for loop counters 67.

for(i=0;i<10;i++) { i=5; }

Page 10: Misra c rules

Declare at file scope Variable no of arguments shall not be

used Shall not call themselves Prototype declaration is needed Return type should match with the

function type Must have return type With no parameters declare as void

Page 11: Misra c rules

#include---preceded by pre-processor directive

C macros used #define pi 4

Macros are not defined as #define’d Not to use #undef function-like macro shall not be 'called'

without all its arguments Arguments to a function-like macro shall

not contain tokens that look like pre-processing directives

each instance of a parameter, shall be enclosed in parenthesis

#define TIMES2 ( x,y ) ( x* ( y ) )

Page 12: Misra c rules

Identifiers in pre-processor directives should be defined before use

At least one preprocessor directive #pragma( libraries we can add to

compilers) need to be documented properly The defined pre-processor operator shall

only be used in one of the two standard forms

#if defined d ok #if defined ( d ) ok #if defined "d" not ok

Page 13: Misra c rules

Pointer arithmetic shall not be usedk = *pi; k = * ( ++pi ) ; /* RULE 101 */

No more than 2 levels of pointer indirection should be used

k = ( *pi1 ) ;k = ( * ( *pi2 ) ) ;k = ( * ( * ( *pi3 ) ) ) ; /* RULE 102 */

Relational operators shall not be applied to pointer types except where both operands are of the same type

int m[10] = { 0,1,2,3,4,5,6,7,8,9 }; int n[10] = { 0,1,2,3,4,5,6,7,8,9 }; int *pm, *pmm; pm = &m[1]; pmm = &m[3]; if ( pm < pmm ) { } pmm = &n[3]; if( pm < pmm ) /* RULE 103 */ { }

Page 14: Misra c rules

Non-constant pointers to functions shall not be used

fp = ( int ( * ) ( ) ) func104b; /* RULE 104 */

The null pointer shall not be dereferenced

structure or union shall be fully specified struct s {

int mem;

foo; //108 rule

}; Overlapping variable storage shall not be

used

Page 15: Misra c rules

Bit fields shall only be defined to be one of type unsigned int or signed int

static struct s{ int p_a : 4; /* RULE 111 */signed int p_b : 4; long p_e : 4; /* RULE 111 */} st;

st.p_a = 0x1;

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

signed int p_a : 4; unsigned int p_b : 1; signed int p_c : 1; /* RULE 112 */

All members shall be named and shall only be access with their name

unsigned int p_b : 1; signed int : 8; /* RULE 113 */ unsigned int p_c : 4;

Page 16: Misra c rules

Don’t change reserved key words Standard library names shall not be reused Should follow standard rules Dynamic heap memory allocation shall not be used

pd = malloc ( sizeof ( double ) ) ; The error indicator errno shall not be used Not to use

Offsetof <local.h> and setlocale Setjmp macro, longjmp function Signal handling in <signal.h> <stdio.h> Atof, atoi, atol from<stslib.h> Abort, exit, getenv and system from <stdlib.h> Time handling of <time.h>

Page 17: Misra c rules