MISRA-C 2004 规则解读(41S-60S)

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

41 S:Ellipsis used in procedure parameter list. 函数入参中使用省略符号,省略符号允许不确定的入参个数,建议避免该编程方式的使用。

42 S:Use of bit field in structure declaration. 避免在结构体声明中使用位字段

struct bitfield1 {INT_32_t  x:1;};  /* not compliant */

43 S:Use of setjmp/longjmp. 避免使用setjmp/longjmp语句,使用该语句使得程序移植性差,结构混乱。

44 S:Use of banned function or variable. 不允许使用关键字命名变量和函数,避免使用与库文件中同名的函数和变量。

45 S:Use of C++ keyword. 不允许使用C++关键字:

void static_45(void){   UINT32_t public    = 0;  /* not compliant */   UINT32_t private   = 0;  /* not compliant */   UINT32_t protected = 0;  /* not compliant */   UINT32_t operator  = 0;  /* not compliant */   UINT32_t new       = 0;  /* not compliant */   UINT32_t template  = 0;  /* not compliant */   UINT32_t virtual   = 0;  /* not compliant */   UINT32_t delete    = 0;  /* not compliant */   UINT32_t friend    = 0;  /* not compliant */   UINT32_t cout      = 0;  /* not compliant */   UINT32_t cin       = 0;  /* not compliant */   UINT32_t endl      = 0;  /* not compliant */   /* ... */}

46 S:extern not in nominated include file.在某些程序中,只有指定的文件可以使用extern修饰符,如果不在指定的文件中使用extern修饰符静态测试会提示警告。

47 S:Array bound exceeded. 使用数组下标获取数组内容时,需要对越界进行防护。

48 S:No default case in switch statement. 避免在使用switch语句时无default分支:

49 S:Logical conjunctions need brackets. 逻辑运算符连接的判断条件,需要使用括号来包围。

void static_49(void){   BOOL flag = TRUE;   INT_32 y = 0, x = 0, z = 1;   if (x < 0 || z + y != 0)  /* not compliant */   {      flag = FALSE;   }   if ((x < 0) || (z + y != 0))  /* compliant */   {      flag = FALSE;   }   }

50 S:Use of shift operator on signed type. 避免对有符号数使用移位运算符:

51 S:Shifting value too far. 避免在移位操作中移动跨度过长。

52 S:Unsigned expression negated.对有符号变量进行运算,有造成变量翻转的风险。

53 S:Use of comma operator.避免使用逗号运算符。

54 S:Sizeof operator with side effects. 不能对表达式使用sizeof操作符。

55 S:Expression with more than one function. 避免在一个表达式中使用两个函数的返回值。

56 S:Equality comparison of floating point. 避免对两个浮点型数据进行比较。

57 S:Statement with no side effect. 避免出现即不改变逻辑又不进行赋值的无效语句。

58 S:Null statement found. 在某个控制语句前出现空行代码,这个空行有可能是开发人员忘记了实现某个功能而留下的坑,如果确实想空一行再写代码,建议补充注释:

void static_58(void){   UINT32_t Timing_Loop = 100U;   /* not compliant - decrement is not part of loop */   while ( Timing_Loop > 0U );         Timing_Loop--;  ; /* compliant as followed by comment */}

59 S:Else alternative missing in if. 在与if/else if配对中缺少else的分支:

void static_59 (void){   UINT32_t x = 1u;   if ( x == 1u )   {      /* ... */ ;   }   else if ( x == 5u)   {      /* ... */ ;   }    /* not compliant */}

60 S:Empty switch statement. 避免出现空switch(无case的情况)语句。

0 0
原创粉丝点击