0

我在不同的文件中收到这些警告。我试图解决它,但对所有人(所有 6 种情况)仍然有相同的警告。

#define CMD_READ_ARRAY_FAST  0x0bU
uint64 page_size;
uint32 offset;
uint64 page_addr;
uint64 sector_size;
uint8_t cmd[5];
size_t len;
size_t actual;

1) page_addr = (uint64) (offset / page_size);

2)  cmd[0] = CMD_READ_ARRAY_FAST;
    cmd[1] = (uint8_t) (page_addr >> 8U);
    cmd[2] = (uint8_t) page_addr;
    cmd[3] = (uint8_t) (offset % page_size);
    cmd[4] = 0x00U;

3)page_addr = (uint64) (offset / page_size);
4)byte_addr = (uint64) (offset % page_size);
5)if ((offset % sector_size) || (len % sector_size)) 
6)cmd[1] = (uint8_t) ((offset / sector_size) + actual);

你能告诉我这样做的原因吗?

4

2 回答 2

0

您的警告源于在同一表达式中混合不同的整数类型,然后依赖于隐式转换。隐式转换总是不好的,MISRA 与否。

以下修复有望清除所有警告:

1)

page_addr = (uint64_t)offset / page_size;

2) 除了第 3 行,一切看起来都不错。

cmd[3] = (uint8_t) ((uint64_t)offset % page_size);

3) 在操作之前再次将 uint32_t 转换为 uint64_t。

4) 在操作之前再次将 uint32_t 转换为 uint64_t。

5) 这里有几个 MISRA 违规行为。最好用几行重写它,而不是冗长的、不可读的。例如:

bool cond1 = ((uint64_t)offset % sector_size) != 0;
bool cond2 = ((uint64_t)len % sector_size) != 0;

if (cond1 || cond2) 

6)这可能还必须是几行才能保持可读性:

uint64_t tmp = (uint64_t)offset / sector_size;
cmd[1] = (uint8_t) (tmp + (uint64_t)actual);
于 2014-01-28T17:24:19.477 回答
0

我通过这种方式解决了上述问题:

#define CMD_READ_ARRAY_FAST  0x0bU
uint64 page_size;
uint32 offset;
uint64 page_addr;
uint64 sector_size;
uint8_t cmd[5];
size_t len;
size_t actual;

1) page_addr = (uint64) (offset / page_size);
Ans: (uint64) ((uint64)offset / page_size);

2)  cmd[0] = CMD_READ_ARRAY_FAST;
    cmd[1] = (uint8_t) (page_addr >> 8U);
    cmd[2] = (uint8_t) page_addr;
    cmd[3] = (uint8_t) (offset % page_size);
    cmd[4] = 0x00U;

Ans:cmd[0] = CMD_READ_ARRAY_FAST;
    cmd[1] = (uint8_t) (page_addr >> 8U);
    cmd[2] = (uint8_t) page_addr;
    cmd[3] = (uint8_t) ((uint8_t)offset % page_size);
    cmd[4] = 0x00U;


3)page_addr = (uint64) (offset / page_size);
Ans: page_addr = (uint64) ((uint64)offset / page_size);

4)byte_addr = (uint64) (offset % page_size);
Ans: byte_addr = (uint64) ((uint64)offset % page_size);

5)if ((offset % sector_size) || (len % sector_size)) 
Ans: if (((uint64)offset % sector_size) || ((uint64)len % sector_size)) {

6)cmd[1] = (uint8_t) ((offset / sector_size) + actual);
Ans: cmd[1] = (uint8_t) (((uint8_t)offset / (uint8_t)sector_size) + (uint8_t)actual);
于 2014-01-24T09:29:47.637 回答