MISRA C:2012 Dir-4.1 4.6 Code design

2791 Definite: Right hand operand of shift operator is negative or too large.

移位操作过大或者是负数

复制代码
void f1(unsigned long ul, int si)
{
    if (si > 40)
    {
        ul = ul << si;                /* 2791 */
    }
}

void f2(unsigned long ul, int si)
{
    if (si < 0)
    {
        ul = ul << si;                /* 2791 */
    }
}
复制代码

2845 Constant: Maximum number of characters to be written is larger than the target buffer size.

复制代码
#include <string.h>

extern char a[10];
extern char b[11];

void foo (void)
{
  strncpy (a, b, sizeof (b));   /* 2845 */
}
复制代码

字符数组 a 溢出了

 

2871 Infinite loop identified 无限循环

复制代码
void f1(void)
{
    int i;
    int n = 5;

    for (i = 0; i < n;    )         /* 2871 */
    {

    }
}

void f2(int n)
{
    while (n <= 10)                  /* 2872 */
    {

    }
}
复制代码

2877 This loop will never be executed more than once.循环不会被执行超过一次

This loop will only be executed once and so the loop mechanism is redundant. Has there been a mistake ?

复制代码
void foo(void)
{
    int i;
    
    for (i = 0; i < 1; ++i)             /* 2877 */
    {
    }
    

    i = 10;

    while (i <= 10)                     /* 2877 */
    {
        ++i;
    }
    
}
复制代码
7.4  Advisory Dir-4.6   typedefs that indicate size and signedness should be used in place of the basic numerical types
Amplification  

The basic numerical types of char, short, int, long, long long (C99), float, double and long double (C99) should not be used, but specific-length typedefs should be used.

For C99, the types provided by <stdint.h> should be used. For C90, equivalent types should be defined and used.

A type must not be defined with a specific length unless the implemented type is actually of that length.

It is not necessary to use typedefs in the declaration of bit-fields.

For example, on a 32-bit C90 implementation the following definitions might be suitable:

   
   typedef signed   char   int8_t;
   typedef signed   short  int16_t;
   typedef signed   int    int32_t;
   typedef signed   long   int64_t;
   typedef unsigned char   uint8_t;
   typedef unsigned short  uint16_t;
   typedef unsigned int    uint32_t;
   typedef unsigned long   uint64_t;
   typedef          float  float32_t;
   typedef          double float64_t;
   typedef long     double float128_t;

例外

Exception  

 

  1. The basic numerical types may be used in a typedef to define a specific-length type.
  2. For function "main" an int may be used rather than the typedefs as a return type. Therefore int main (void) is permitted.
  3. For function "main" an int may be used rather than the typedefs for the input parameter argc.
  4. For function "main" a char may be used rather than the typedefs for the input parameter argv.

Therefore int main( int argc, char *argv[] ) is permitted (C99 Section 5.1.2.2.1).

Implemented by QAC messages:  
5209 Use of basic type '%s'.

 

3453 使用函数替代类似函数的宏,宏没有类型检查

#define M1(a, b, c)   ((a) + (b) + (c))       /* Message 3453 */
#define M6(A,B)       ((A)[1] + (B)[2])       /* Message 3453 */

This macro appears to be in the form of an expression and could therefore, perhaps, be replaced with a function. It is not always practical or convenient to replace function-like macros with functions; but functions are generally safer than macros because type-checking can be enforced on arguments.

Message 3453 is generated for a function-like macro which ...

  • has at least one parameter
  • does not contain a # or a ## operator
  • does not contain { } ; or any keywords (other than type specifiers or type qualifiers)

 0883 包含文件代码未受到保护以防止重复包含

#ifndef HFILEX_H
#define HFILEX_H
...
...
#endif

 

 

 

 

 

posted @   清风oo  阅读(870)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
历史上的今天:
2018-12-01 12/1 感悟
2018-12-01 《OFFER12》 12_StringPathInMatrix
点击右上角即可分享
微信分享提示