MISRA-C 2004 规则解读(101S-120S)


101 S:Function return type inconsistent. 函数的实际返回值类型与定义的返回值类型不同:

UINT32_t static_101( UINT32_t par_1)
{
    switch (par_1)
    {
       case 0:
          return (-1);     /* not compliant */
          break;
       case 1:
          return (1U);
          break;
       case 2:
          return (1L);    /* not compliant */
          break;
       case 3:
          return (1.0f);  /* not compliant */
          break;
       default:
          break;
    }
}

102 S:Function and prototype return inconsistent (MR). 函数声明时的返回值类型与实际使用时的返回值类型不同:

UINT32_t static_102(UINT32_t p_1, UINT16_t p_2);

/**************************************************************
 * Standard 102 S : Function and prototype return inconsistent.
 **************************************************************/

SINT32_t static_102(UINT32_t p_1, UINT16_t p_2) /* not compliant */
{
   SINT32_t result = 0;
   /* ... */ 
   return result;
}

103 S:Function and prototype param inconsistent (MR). 函数声明时的参数类型与实际使用时的参数类型不同:

BOOL static_103(FLOAT32_t up_1);

/***************************************************************
 * Standard 103 S : Function and prototype param inconsistent.
 ***************************************************************/ 
BOOL static_103(UINT32_t up_1)
{
   BOOL ret = FALSE;
   /* ... */ 
   return ret;
}

104 S:Struct field initialisation incorrect. 结构体初始化赋值的类型与定义的类型不符:

struct s_type_a { SINT32_t xs; FLOAT32_t fs;};

/********************************************************
 * Standard 104 S : Struct field initialisation incorrect.
 ********************************************************/ 

void static_104(void)
{
   struct s_type_a sta = {3.14F, 0.0f}; /* not compliant */
   /* ... */ 
}

105 S:Initialisation brace { } fault. 初始化结构体忘记使用大括号会出错:

struct pixel{ UINT32_t colour; struct {UINT32_t x, y;} coords; };

/***********************************************************
 * Standard 105 S : Struct field initialisation brace fault.
 ***********************************************************/ 

void static_105(void)
{
   UINT32_t xs = 0;
   UINT32_t ys = 0;

   struct pixel spot = {1u, xs, ys }; /* not compliant */

   /* ... */ 
}

106 S:Volatile declaration.避免使用Volatile修饰变量,Volatile可以在应用程序外被改变,容易造成逻辑混淆。

void static_106(void)
{
   volatile UINT32_t v = 1U; /* not compliant */
   UINT32_t x = 0;

   x = v;
   /* ... */ 
}

107 S:Type mismatch in ternary expression. 使用三目运算符时类型不匹配:

static void static_107( BOOL flag )
{
   UINT32_t  x = 0U;
   FLOAT32_t f = 0.0F;

   x = (flag ? f : x);

}

108 S:Assignment types do not match. 变量赋值时类型不匹配:

void static_108(void)
{
   FLOAT32_t fl = 2.0F;
   FLOAT_64 dbl = 3.0;

   fl = dbl; /* not compliant */
}

109 S:Array subscript is not integral. 数组的索引必须为正数。

110 S:Use of single line comment //. 由于使用双斜杠标记注释的做法不是对所用的编译器通用,建议对注释段使用/**/的方式。

111 S:Label is not part of switch statement . 只有在switch语句中使用标签语法是安全的。

112 S:Typedef name redeclared.使用typedef 时某个名称被重复使用是危险的:

typedef SINT32_t mytype;

/*******************************************
 * Standard 112 S : Typedef name redeclared.
 ********************************************/

void static_112(void)
{
   typedef FLOAT32_t mytype; /* not compliant */

   /* ... */
}

113 S:Non standard character in source.源文件中出现非法字符,合法的字符如下:

3***** The lines of permissible characters,(80A1),those NOT changed
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
()[].!~+-*/%<>=& ^|?:;,0123456789_{}'"#\$@<space><tab> 

114 S:Expression is not Boolean. 表达式类型不为BOOl类型,或者表达式的运算对象都为BOOL类型:

void static_114(BOOL bl, UINT32_t a)
{
   UINT32_t x;
   BOOL flag;

   flag = bl + bl; /* not compliant */

   if (a) /* not compliant */
   {
      ; /* ... */
   }
   if (a == 1) /*compliant */
   {
      ; /* ... */
   }

   x = ( a && bl ? 1U : 0U ); /* not compliant */

}

115 S:String incorrectly terminated. string类型换行时需要使用反斜杠,否则在某些编译器中造成string类型结尾异常的错误:

/*******************************************************
 * Standard 115 S : String incorrectly terminated.
 *******************************************************/ 
void static_115(void)
{
   CHAR* str1 = "string\
                 literal";   /* compliant */

   CHAR* str2 = "string
                 literal";  /* not compliant */
 }

116 S:Boolean comparison with 0 preferred. 与一个已知值的变量进行比较时,使用不等号来判断FALSE比使用等号判断TRUE安全:

void static_116 (void)
{
   UINT32_t x = 2u;

   if ( x == 1u ) /* not compliant */
   {
      /* ... */ ;
   }
   if ( x != 1u )
   {
      /* ... */ ;
   }
}

117 S:Logical negation of constant value. 对const变量取反是危险的:

void static_117(void)
{
   BOOL flag = FALSE;

   if (flag == (BOOL)!1) /* not compliant */
   {
      /* ... */ 
   }
}

118 S:main must be int (void) or int (int,char* []).C语言Main函数的格式必须是int (void) 或者 int (int,char* [])。

119 S:Nested comment found.避免在注释中出现子注释:

void static_119(void)
{

/*  This is the Outer Comment
 /* This is the Inner Comment - not compliant
  */
}

120 S:Use of bit operator on signed type. 避免对有符号类型进行位运算:

void static_120(void)
{
   SINT32_t b = 1;

   b = b | 1; /* not compliant */

   /* ... */ 
}

猜你喜欢

转载自blog.csdn.net/u013992766/article/details/51172500
今日推荐