警告639:二进制操作中类型'uint64'的强类型不匹配

时间:2014-01-23 17:31:48

标签: c++ lint misra

我在不同的文件中收到这些警告。我试图解决它,但仍然对所有人都有相同的警告(所有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);

你能告诉我原因吗?

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);