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. 避免使用二级指针。
- MISRA-C 2004 规则解读(61S-80S)
- MISRA-C 2004 规则解读(1S-20S)
- MISRA-C 2004 规则解读(21S-40S)
- MISRA-C 2004 规则解读(41S-60S)
- MISRA-C 2004 规则解读(81S-100S)
- MISRA-C 2004 规则解读(101S-120S)
- MISRA-C 2004 规则解读(121S-140S)
- MISRA-C 2004 规则解读(141S-160S)
- MISRA-C 2004 规则解读(161S-180S)
- MISRA-C 2004 规则解读(181S-200S)
- MISRA-C 2004 规则解读(201S-220S)
- MISRA-C 2004 规则解读(221S-240S)
- MISRA-C 2004 规则解读(241S-260S)
- MISRA C指导指南解读系列3(MISRA C规则11-19)
- MISRA C指导指南解读系列4(MISRA C规则20-32)
- MISRA C指导指南解读系列5(MISRA C规则33-45)
- MISRA C指导指南解读系列6(MISRA C规则46-67)
- MISRA C指导指南解读系列7(MISRA C规则68-86)
- 【浅谈Activiti之"开发环境搭建"】
- MISRA-C 2004 规则解读(41S-60S)
- 提升vi的性能
- hdu-1080 Human Gene Functions
- #1014 : Trie树
- MISRA-C 2004 规则解读(61S-80S)
- Stanford ex4:Neural Networks Learning
- 高仿糗事百科学习(二)HotFragment
- Android学习之Android studio TraceView和lint工具的使用详解
- IDEA15引用mvn-install到repository的其他项目出现的奇怪问题
- iOS 探讨之 "Developer Tools Access 需要控制另一个进..."
- 说说条件编译#ifndef,#define,#endif
- C++编程基础笔记章节四
- MISRA-C 2004 规则解读(81S-100S)