MISRA-C 2004 规则解读(61S-80S)

来源:互联网 发布:总会有一个人,知你冷暖 编辑:程序博客网 时间:2024/05/20 11:37

61 S:Switch contains default only. 避免switch语句只有default分支而没有case分支。

62 S:Switch case not terminated with break. 建议每个case分支都有break语句。

63 S:Empty parameter list to procedure/function. 函数无入参时,建议增加void修饰:

void static_63(void) /* compliant */{   /* ... */ }

64 S:Void procedure used in expression. 函数返回值为void类型,却对返回值进行操作,这在vs2008中是编译失败的。

65 S:Void variable passed as parameter. 避免使用void类型修饰函数入参:

void void_para_func(void P1){  /* ... */}/****************************************************************** * Standard 65 S : Void variable passed as parameter. * Does not Compile with VC++ ******************************************************************/void static_65( void ){  void v_ptr;  UINT32_t a;  void_para_func(v_ptr);   /* not compliant */  void_para_func((void)a); /* not compliant */}

66 S:Function with empty return expression.定义有返回值的函数,却什么也没有返回,这在vs2008中是编译失败的。

UINT32_t static_66 ( void ){   /* ... */   return;  /* not compliant */}

67 S:#define used in a block. define语句应当出现在文件开头处,不应当在函数体内使用。

68 S:#undef used. 避免使用#undef语句。

69 S:#pragma used. 避免使用#pragma语句。

70 S:Logical comparison of pointers. 对指针的比较只允许使用(== and !=)

char * x;char * y;....if (x>y)        { /*  Incorrect, Unsafe*/ }if (x==y)       { /*  Correct usage*/ }

71 S:Pointer assignment to wider scope. 该规则用于检查对象的指针是否被分配给超过其作用域范围的值。 由于局部变量在运算完成后,指针指向的变量空间可能被收回,那么被赋值的外部变量可能指向未知的值。

UINT32_t *ptr;UINT32_t* static_71( void ){   static UINT32_t w = 10u;   UINT32_t local = 3U;   UINT32_t *local_ptr = &local;   ptr = &local;     /* Not Compliant */   ptr = local_ptr;  /* Not Compliant */   return &w;       /* Not Compliant if modifier is set to 0 */}

72 S:Signed bit field less than 2 bits wide.有符号型变量有一位表示符号,至少还需要一位表示值。

73 S:Bit field not signed or unsigned int.位字段被定义成其他类型是危险的:

struct static_73 { UCHAR  x:1;  /* Not Compliant */};

74 S:Union declared. 避免使用联合定义数据结构。

75 S:Executable code before an included file. #include语句必须出现在可执行语句之前。

76 S:More than one of # or ## in a macro. 不允许宏定义中出现多个 # 或者 ##:

/* not compliant */#define SetVariable( Variable, Value ) ( dummy(NewString##Variable, #Value ))

77 S:Macro replacement list needs parentheses. 使用宏定义时,注意使用括号来限定宏运算的边界,避免在宏被替换后出现意想不到的运算组合:

#define increment_NOK(a)    ( a ) + 1#define increment_OK(b)   ( ( b ) + 1 )/******************************************************** * Standard 77 S : Macro replacement list needs parentheses. ********************************************************/void static_77(void){   INT32_t result = 1;   result = increment_NOK(result) * 2; /* result = 3 */   result = 1;   result = increment_OK(result) * 2;  /* result = 4 */}

78 S:Macro parameter not in brackets. 同77 S:

#define abs(x) (((x) >= 0) ? (x) : -(x)) /* Compliant */ #define abs(x) x >= 0 ? x : -x           /* Not compliant */ 

79 S:Macro contains unacceptable items. 宏定义中避免出现关键字:

/* not compliant */#define t void

80 S:Pointer indirection exceeds 2 levels. 避免使用二级指针。

0 0
原创粉丝点击