栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

misra c编码规范个人整理总结/misra c 2012中文版-个人总结-【方便查询】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

misra c编码规范个人整理总结/misra c 2012中文版-个人总结-【方便查询】

整理MISAR-2012错误解决方法-带编号,本文根据文档整理了部分常见的MISAR-2012错误及解决方法,顺序是错误码顺序,参考文档《LDRA standards for C/C++》,侵权即删。

代码注释compliant:代表合格的、正确的代码注释not compliant:代表不合格的、不正确的

S类

9 S :ssignment operator in expression12 S :No brackets to then/else35 S :Static procedure is not explicitly called in code analysed.47 S :Array bound exceeded.59 S :Else alternative missing in if.87 S :Use of pointer arithmetic.90 S :Basic type declaration used94 S :Casting operation on a pointer.95 S :Casting operation to a pointer.96 S :ssignment operator in expression101 S :Function return type inconsistent.104 S :Struct field initialisation incorrect.114 S :expression is not Boolean139 S :Construct leads to infeasible code.203 S :Cast on a constant value.252 S :Lower case suffix to literal number.270 S :For loop initialisation is not simple.302 S :Comment possibly contains code.331 S :Literal value requires a U suffix.332 S :Widening cast on complex integer expression.361 S :expression needs brackets.382 S :(void) missing for discarded return value.397 S:Array initialisation has insufficient items.410 S :Switch empty default has no comment.433 S :Type conversion without cast434 S :Signed/unsigned conversion without cast.436 S :Declaration does not specify an array.443 S :Unsigned integral type cast to signed.458 S :Implicit conversion: actual to formal param.628 S :Macro not used in translation unit. D类

1 D :Unused Procedure Parameter18 D :Identifier name reused27 D :Variable should be declared static.28 D :Potentially Infinite loop found.61 D :Procedure should be declared static.63 D :No definition in system for prototyped procedure65 D :void function has no side effects.69 D :UR anomaly, variable used before assignment.76 D :Procedure is not called or referenced in code analysed.91 D : Function return value potentially unused.105 D :DU anomaly dead code, var value is unused on all paths.120 D :Pointer param should be declared pointer to const.128 D :Global pointer not checked within this procedure135 D :Pointer assigned to NULL may be dereferenced.S :D :

代码注释compliant:代表合格的、正确的 代码注释not compliant:代表不合格的、不正确的 S类 9 S :ssignment operator in expression

中文含义:表达式中有赋值运算符
错误代码示例:

BOOL static_9(BOOL test)
{
   BOOL result,flag;
 
   result = ( flag = test ); 
 
   return result;
}
12 S :No brackets to then/else

中文含义:then/else缺少括号
代码示例:

SINT_32 static_12(SINT_32 p_1, SINT_32 p_2)
{
   SINT_32 i = 1;
   SINT_32 j = 0;
 
   if (p_1 > 0)
   {
     i = i - 1;
   }
   else
      i = i + 1;          
 }
35 S :Static procedure is not explicitly called in code analysed.

中文含义:static函数没有显示调用
错误代码示例:

static BOOL static_35(UINT_32 p_1)  
{
   BOOL ret = ( p_1 == 1U );
   return ret;
}
47 S :Array bound exceeded.

中文含义:数组越界
代码示例:

void static_047(void)
{
  SINT_32 array[5] = {0,0,0,0,0};
  SINT_32 *ptr;
 
  array[5] = 1; 
 
  ptr = &array[5]; 
  ptr = &array[6]; 
}
59 S :Else alternative missing in if.

中文含义:if后缺少else,规定if之后必须接else
代码示例:

void static_59 (void)
{
   UINT_32 x = 2u;
 
   if ( x == 2u )
   {
       ;
   }
   else if ( x == 3u)
   {
       ;
   } 
   
}
87 S :Use of pointer arithmetic.

中文含义:使用了指针运算,这是不允许的
代码示例:

void static_87(void)
{
   UINT_32 w;
   UINT_32 array[5];
   UINT_32 * p1_ptr;
 
   p1_ptr = array;
   w = *(p1_ptr + 8);  
}
90 S :Basic type declaration used

中文含义:使用了int、char、float、double等基础类型,这是不允许的
代码示例:

unsigned int static_90 (void) 
{
  char           ch;        
  unsigned char  uc;           
  unsigned int   ui_32;     
  unsigned short ui_16;     
  int            i_32;      
  float          f_32;      
  double         f_64;      
  signed char    sc;        
 
  wchar_t        wc;        
 
   
  return ui_32;
}
94 S :Casting operation on a pointer.

中文含义:对指针执行强制转换操作
错误代码示例:

static void static_94(UINT_32 * p1_ptr)
{
   UINT_32 *ptr2;
   CHAR * ptr_ch;
 
   ptr2 = (UINT_32 *) p1_ptr;  
 
   (void) get_ptr();           
 
   ptr_ch = (CHAR *) p1_ptr;   
}
95 S :Casting operation to a pointer.

中文含义:将操作强制转换为指针
错误代码示例:

 
struct Astruct { UINT_32 a; };
 
void static_95 (UINT_32 *intptr)
{
  struct Astruct *Astructptr;
  Astructptr = (struct Astruct *) intptr; 
}

96 S :ssignment operator in expression

中文含义:不同类型混合计算
错误代码示例:

static void static_96(void)
{
  INT_32 i32 = 10;
  FLOAT_64 f64 = 20.5;
  FLOAT_32 f32 = 2.0F;
 
  f64 = i32 + f64;  
  f64 = f64 * f32;  
}
}
101 S :Function return type inconsistent.

中文含义:返回值和函数类型对应不上
代码示例:

UINT_32 static_101( UINT_32 par_1)
{
    switch (par_1)
    {
       case 0:
          return (-1);     
          break;
       case 1:
          return (1U);
          break;
       case 2:
          return (1L);    
          break;
       case 3:
          return (1.0f);  
          break;
       default:
          break;
    }
}
104 S :Struct field initialisation incorrect.

中文含义:结构字段初始化不正确。
代码示例:

struct s_type_a { SINT_32 xs; FLOAT_32 fs;};

void static_104(void)
{
   struct s_type_a sta = {3.14F, 0.0f}; 
   
}
114 S :expression is not Boolean

中文含义:表达式不能有boolean类型
错误代码示例:

void static_114(BOOL bl, UINT_32 a)
{
   UINT_32 x;
   BOOL flag;
   
   flag = bl + bl; 
 
   if (a) 
   {
      ; 
   }
   x = ( a && bl ? 1U : 0U ); 
}
139 S :Construct leads to infeasible code.

中文含义:if的条件可能不成立,导致if里面的语句不能抵达
代码示例:

#define defval 0
 
typedef enum { LANE_0 = 0, LANE_1 = 1, LANE_LAST = 3 } lane_t;
extern lane_t get_lane ( void );
void static_139( void )
{
   lane_t lane = get_lane();
   if ( (lane > LANE_0) && ( lane <= LANE_LAST))
      
      {  }
 
   if (defval)
      
      {  }
}
203 S :Cast on a constant value.

中文含义:同种类型之间使用强制转换
错误代码示例:

const INT_16 con = 19;
const INT_16 * pcon;
 
static void static_203(void)
{
   INT_16 x;
   INT_16 *p;
   x = (INT_16)con;      
   p = (INT_16 *)pcon;   
}
252 S :Lower case suffix to literal number.

中文含义:数字后面不能写小写后缀,得要是U或L,uint8这种无符号型数据后缀必须是U,比如uint8 i = 0U;
错误代码示例:

const SINT_64 fr1 = 64l;  
const SINT_64 fr2 = 64L;  
 
void static_252(void)
{
  SINT_64 x1 = fr2;
}
270 S :For loop initialisation is not simple.

中文含义:for循环的初始化条件过于复杂
代码示例:

void static_270(void)
{
  UINT_32 loop;
  UINT_32 myVar = 0U;
  const UINT_32 max = 10U;
 
  for ( ++myVar, loop = 0U; loop < max; loop++ ) 
  {
    
  }
}
302 S :Comment possibly contains code.

中文含义:屏蔽的部分可能包含代码,可以用#if 0和#endif,不会报错
错误代码示例:

void static_302 (UINT_32 myParam)
{
  if (myParam > limit)
  {
    myParam = limit;
     
  }
}
331 S :Literal value requires a U suffix.

中文含义:文字值需要U后缀
错误代码示例:

void static_331(void)
{
  UINT_32 x1 = 5;   
  UINT_32 y1 = 6U;  
  UINT_64 z1 = 0;  
 
  y1 = y1 * 7;     
   
}
332 S :Widening cast on complex integer expression.

中文含义:加宽对复杂整数表达式的强制转换。
错误代码示例:

typedef unsigned short Uint_16;
typedef unsigned int Uint_32;
Uint_16 u16a = 40000U;
Uint_16 u16b = 30000U;
 
void static_332( void )
{
  Uint_32 u32 = (Uint_32) (u16a + u16b); 
  
}
361 S :expression needs brackets.

中文含义:表达式需要括号
错误代码示例:

SINT_32 static_361(SINT_32 x1,
                   SINT_32 x2,
                   SINT_32 x3)
{
  SINT_32 z1;
  z1 = z1 * x2  >> 3U;     
  z1 = x1 * x2 + x3;     
  z1 = x1 * x2++;        
  z1 = x1 + x2 - x3;     
  z1 = x1 + x2 + x3;     
  return z1;
}
382 S :(void) missing for discarded return value.

中文含义:意思就是函数前要加(void)
错误代码示例:

UINT_32 a_fn(UINT_32 us1)
{
  return us1;
}
 
void static_382(void)
{
  a_fn(my_const);         
  (void)a_fn(my_const);   
}
397 S:Array initialisation has insufficient items.

中文含义:数组初始化没有足够的项
代码示例:

void static_397 (void)
{
  INT_32 my_array[3] = { 1, 2 };        
 
  INT_32 array2[2][2] = { {0}, {1,2} }; 
 
  CHAR char_10[10] = "Hello";           
}
410 S :Switch empty default has no comment.

中文含义:switch语句应包含一个默认条款,如果之前的case条款未得到满足,则该默认条款将采取适当的措施,或者至少包含一条注释,表明程序员已经考虑了这种可能性。注释必须放在默认值之后和中断之前。
代码示例:

void static_410( void )
{
  switch (season)
  {
    case spring:
      x1 = 1U;
      break;
    case summer:
      x1 = 4U;
      break;
    case autumn:
      x1 = 7U;
      break;
    case winter:
      x1 = 10U;
      break;
    
    default:
    
      break;
  }
}
433 S :Type conversion without cast

中文含义:无强制转换的类型转换
错误代码示例:

void static_433(long s64)
{
  char ch = s64; 
}
434 S :Signed/unsigned conversion without cast.

中文含义:没使用强制转换,就把A类型变量赋值给B类型变量
错误代码示例:

void static_434(UINT_32 us1)
{
  SINT_32 ss1 = us1;  
       
}
436 S :Declaration does not specify an array.

中文含义:声明未指定数组
错误代码示例:

void static_436 (INT_8 * ptr, INT_8 arr[10])
{
  INT_8  * p1 = ptr;
  INT_8  * p2 = arr;
 
  ptr[5] = 0;   
  p1[5] = 0;    
 
  p2[5] = 0;    
 
}
443 S :Unsigned integral type cast to signed.

中文含义:无符号整型转换为有符号整型。
代码示例:

void static_443( void )
{
  INT_32  s32;
  UINT_32 u32a,
          u32b;
 
  s32 = (INT_32)(u32a + u32b);  
 
  s32 = (INT_32)(u32a);         
 
}
458 S :Implicit conversion: actual to formal param.

中文含义:隐式转换:实际参数到形式参数,调用的函数参数类型是A,结果传入的是B类型
错误代码示例:

static void narrow_int(Uint_32 u32b)
{
  ;   
}
 
static void static_458(void)
{
  Uint_64 u64a;
 
  narrow_int(u64a); 
}
628 S :Macro not used in translation unit.

中文含义:#define定义的数据没有被使用过
错误代码示例:

#define SIZE_USED 6  	 
#define DATA 3           
INT_32 static_628(void)
{
#define SIZE_NOT_USED 6   
return SIZE_USED;
}
D类 1 D :Unused Procedure Parameter

中文含义:存在未使用的程序参数
代码示例:

UINT_32 SDA_001( UINT_32 p_1, UINT_32 p_2 )
{
   UINT_32 v_1;
   v_1 = p_1;
   v_1++;
   return v_1;
}  
 
18 D :Identifier name reused

中文含义:局部变量名称与全局变量一致
代码示例:

UINT_32 Re_Used;
UINT_32 SDA_018( void )
{
   UINT_32 Re_Used; 
   Re_Used = 1;
   return Re_Used;
}
27 D :Variable should be declared static.

中文含义:意思是只在本文件使用的变量,前面要加static,在其他文件要使用的可不加
错误代码示例:

第一个文件:Sda_027_1.c

#include "c_standards.h"

INT_32 global_1 = 1;     
 
INT_32 global_2 = 2;     
 
static INT_32 SDA_027( void )
{
  return global_2 - global_1;
}
 
INT_32 main( void )
{
  return  SDA_027() + SDA_027_2();
}
 
第二个文件:Sda_027_2.c

#include "c_standards.h"
 
INT_32 global_2;
 
INT_32 SDA_027_2 ( void )
{
	return global_2;
}
28 D :Potentially Infinite loop found.

中文含义:发现潜在的无限循环
错误代码示例:

void SDA_028( void )
{
   INT_32 i = 1;
   BOOL flag = TRUE;
 
   while (flag) 
   {
      if (i==0)
      {
          flag = FALSE;
      }
   }
}
61 D :Procedure should be declared static.

中文含义:只在当前文件使用的函数应该被声明为static,在其他文件使用的就不声明static
错误代码示例:

Sda_061_1.c

#include "c_standards.h"

static void helper_proc1( void ) { ; } 
 
void helper_proc2( void) { ; }    
 
void sda_061( void )    
{
  helper_proc1();
  helper_proc2();
}

第二个文件:Sda_061_2.c

#include "c_standards.h"
 
int main(void)
{
  sda_061();
 
  return 0;
}
63 D :No definition in system for prototyped procedure

中文含义:函数声明了,但没定义内容
代码示例:

void sda_063_1( void );
void sda_063_2( void );  
 
void sda_063_1( void )
{
  
}
 
int main(void)
{
  sda_063_1();
  sda_063_2();
  return 0;
}
65 D :void function has no side effects.

中文含义:具有无效返回类型的功能应具有外部副作用。未能为生成任何输出做出贡献可能不是开发人员的意图或期望。
个人理解:可能是说函数没有返回值,参数又没有输入指针或结构体去改变什么值,没有任何产出,说这种函数没啥实际意义。
错误代码示例:

static void sda_065_1( void ) 
{
  UINT_32 local_int = 1U;
 
  local_int++;
}
69 D :UR anomaly, variable used before assignment.

中文含义:在赋值前使用的变量
错误代码示例:

void sda_069 ( void )
{
  UINT_32 var_1;  
 
  var_1++;
}
76 D :Procedure is not called or referenced in code analysed.

中文含义:意思是这个函数未被调用过
代码示例:

static void SDA_076 ( void) 
{
  ;
}
SINT_32 main(void)
{
}
91 D : Function return value potentially unused.

中文含义:函数返回值可能未被使用
代码示例:

static UINT_32 return_unsigned ( void )
{
  return 4U;
}
 
static void SDA_091 ( UINT_32 x )
{
   UINT_32 partused;
 
   partused = return_unsigned ( ); 
   if ( x == 3 )		
   {
      glob_res = partused;
   }
   
}
105 D :DU anomaly dead code, var value is unused on all paths.

中文含义:意思就是变量的值,在此函数区域内没有使用过
代码示例:

static void sda_105 ( const UINT_32 p1 )
{
  UINT_32 var_1 = 0U;
  UINT_32 var_2 = p1;  
  var_1++;  			
  if ( p1 > 42U )
  {
     printf("%un", var_2); 
  }
}
120 D :Pointer param should be declared pointer to const.

中文含义:指针参数应该被定义为const
代码示例:

void sda_120( UINT_32 * pptr1,
              const UINT_32 * pptr2,  
              UINT_32 * pptr3,        
              UINT_32 arr1[ ],        
              const UINT_32 arr2[ ]	  
            )  
{
   *pptr1 = *pptr2 + *pptr3;          
  
   *pptr1 = arr1[0] + arr2[0];      
}
128 D :Global pointer not checked within this procedure

中文含义:在使用全局指针之前,没有检查它是否为NULL
代码示例:

UINT_32 *glob1;
UINT_32 *glob2;
 
void SDA_128(void)
{
  UINT_32 loc = *glob1; 
  UINT_32 loc2;
 
  if (glob2 != NULL)
  {
    loc2 = *glob2; 
  }
}
135 D :Pointer assigned to NULL may be dereferenced.

中文含义:分配给NULL的指针可能会被取消引用,还是要检查空指针的意思,防止有的指针通过判断条件才给其定义指向,但有时候判断不成立,就没有定义指向,指针依旧是NULL,而后面使用前,如果不做NULL判断,就会出问题。
代码示例:

SINT_32 glob = 1;
 
void sda135(SINT_32 flag)
{
  SINT_32 *ptr1 = NULL;
  SINT_32 *ptr2 = NULL;
  SINT_32 val;
 
  if (flag == 1)
  {
    ptr1 = &glob;
    ptr2 = &glob;
  }
 
  val = *ptr1; 
 
  if (ptr2 != NULL)
  {
    val = *ptr2; 
  }
}
S :

中文含义:
代码示例:

 
D : 

中文含义:
代码示例:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/784135.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号