简体   繁体   English

结构数组初始化中的MISRA-C错误

[英]MISRA-C error in struct array initialization

I have the following: 我有以下内容:

typedef struct
{
   uint8_t BlockID;
   uint32_t Copies;
   uint16_t Size;
}NVMM_ConfigType;

const NVMM_ConfigType NvmmCnf_Layout[6] =
{
   {  1, 1,   4},
   {  2, 3,   4},
   {  5, 5,  16},
   { 10, 1,   4},
   { 11, 2,  32},
   { 13, 1, 100},
};

Which seems fine to me, but, MISRA-C is giving the following error: 这对我来说似乎很好,但是,MISRA-C给出了以下错误:

MISRA C:2012 rule 10.3 violation: [R] The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category MISRA C:2012规则10.3违规:[R]表达式的值不得分配给具有较窄基本类型或不同基本类型类别的对象

I've tried to figure out why is this happening but I just can see it. 我试图弄清楚为什么会这样,但我只能看到它。 Also the build results are plagued with this errors on similar situations and I don't know why. 在类似的情况下,构建结果也会受到这种错误的困扰,我不知道为什么。

Does anybody know what's going on? 有人知道发生了什么吗?

EDIT: I have also tried to explicitly cast every value and still getting the same error: 编辑:我也尝试显式转换每个值仍然得到相同的错误:

const NVMM_ConfigType NvmmCnf_Layout[6] =
{
    {  (uint8_t)1, (uint32_t)1,   (uint16_t)4},
    {  (uint8_t)2, (uint32_t)3,   (uint16_t)4},
    {  (uint8_t)5, (uint32_t)5,  (uint16_t)16},
    { (uint8_t)10, (uint32_t)1,   (uint16_t)4},
    { (uint8_t)11, (uint32_t)2,  (uint16_t)32},
    { (uint8_t)13, (uint32_t)1, (uint16_t)100},
};

(Hi, this is a new account so I cannot use the comments section yet to ask for further clarification, so pardon the long reply) (嗨,这是一个新帐户,所以我还不能使用评论部分要求进一步澄清,所以请原谅长篇回复)

To be specific, this Rule 10.3 pertains to MISRA-C:2012 (the latest standard) which is a great improvement over the prior versions in that there is more effort in explaining MISRA's rationale, along with many more compliant and non-compliant examples. 具体而言,本规则10.3适用于MISRA-C:2012(最新标准),这是对先前版本的重大改进,因为在解释MISRA的基本原理方面有更多的努力,以及更多符合要求和不符合要求的示例。

The rationale of the rule is: since C permits assignments between different arithmetic types to be performed automatically, the use of these implicit conversions can lead to unintended results, with the potential for loss of value, sign or precision. 规则的基本原理是:由于C允许自动执行不同算术类型之间的分配,因此使用这些隐式转换可能会导致意外结果,并可能导致价值,符号或精度损失。 MISRA_C:2012 has an essential type model to help warn when this might occur. MISRA_C:2012有一个基本类型模型,可以在发生这种情况时发出警告。

The rule descriptions also include exceptions to the rule. 规则描述还包括规则的例外 For Rule 10.3, one exception is: A non-negative integer constant expression of essentially signed type may be assigned to an object of essentially unsigned type if its value can be represented in that type. 对于规则10.3,一个例外是: 如果其值可以用该类型表示,则可以将基本上为有符号类型的非负整数常量表达式分配给基本无符号类型的对象。

Its not clear what the exact line and column your tool is reporting the violation on (it should). 不清楚您的工具报告违规行为的确切行和列(应该)。 The better of the tools will also provide more detailed information on exactly what part of the rule is being violated (eg if instead of a 1, you had 128 in the first assignment to a 8-bit, the tool should be very explicit about that). 更好的工具还将提供有关违反规则的确切部分的更详细信息(例如,如果不是1,在第一次分配中有128位到8位,该工具应该非常明确)。

In any case, I don't (nor does my tool) see any violation of 10.3 here. 在任何情况下,我都没有(我的工具也没有)在这里看到任何违反10.3的行为。

Since this is a “decidable” rule, I would be concerned about the tool if this is safety-critical code, besides the fact that it is wasting your time. 由于这是一个“可判定”的规则,如果这是一个安全关键代码,我会担心这个工具,除了浪费你的时间这个事实。

Most tools allow you to suppress a warning and document the reason (in this case it is a bug in the tool). 大多数工具允许您禁止警告并记录原因(在这种情况下,它是工具中的错误)。

If your tool vendor needs further information, you can post your question in the discussions forum at http://www.misra-c.com to get the official answer and forward that to the vendor. 如果您的工具供应商需要更多信息,您可以在http://www.misra-c.com的讨论论坛中发布您的问题,以获得正式答案并将其转发给供应商。

Hmm, that rule will make setting 8 bit registers actually impossible, as arithmetic operations are performed as int or larger ( usual arithmetic conversions ). 嗯,该规则将使得设置8位寄存器实际上是不可能的,因为算术运算以int或更大( 通常的算术转换 )执行。 One more reason to reject MISRA as coding standard. 拒绝MISRA作为编码标准的另一个原因。

I assume you have to cast every single value in the initializer to the type of the respective field. 我假设您必须将初始化程序中的每个值转换为相应字段的类型。 But As the rule is cited, that would still be a violation. 但正如引用的规则,这仍然是违规行为。

When I use PC-Lint to check Misra rules, I often find myself needing to add u suffix to constants: 当我使用PC-Lint检查Misra规则时,我经常发现自己需要为常量添加u后缀:

const NVMM_ConfigType NvmmCnf_Layout[6] =
{
   {  1u, 1u,   4u},
   {  2u, 3u,   4u},
   {  5u, 5u,  16u},
   { 10u, 1u,   4u},
   { 11u, 2u,  32u},
   { 13u, 1u, 100u},
};

This eliminates the int to unsigned conversion. 这消除了intunsigned转换。

If this isn't enough, then casts: 如果这还不够,那么演员:

const NVMM_ConfigType NvmmCnf_Layout[6] =
{
   { (uint8_t ) 1u, 1u, (uint16_t )  4u},
   { (uint8_t ) 2u, 3u, (uint16_t )  4u},
   { (uint8_t ) 5u, 5u, (uint16_t ) 16u},
   { (uint8_t )10u, 1u, (uint16_t )  4u},
   { (uint8_t )11u, 2u, (uint16_t ) 32u},
   { (uint8_t )13u, 1u, (uint16_t )100u},
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM