结构数组初始化中的MISRA-C错误 [英] MISRA-C error in struct array initialization

查看:165
本文介绍了结构数组初始化中的MISRA-C错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

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},
};

这对我来说似乎很好,但是,MISRA-C出现以下错误:

Which seems fine to me, but, MISRA-C is giving the following error:

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?

编辑:我还试图显式地转换每个值,并且仍然收到相同的错误:

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)

具体来说,本规则10.3适用于MISRA-C:2012(最新标准),这是对先前版本的一项重大改进,因为它需要更多的精力来解释MISRA的基本原理,以及更多合规且不合规的内容.兼容的示例.

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.

该规则的原理是:由于C允许自动执行不同算术类型之间的赋值,因此使用这些隐式转换可能会导致意想不到的结果,并有可能丢失值,符号或精度. MISRA_C:2012有一个基本的类型模型来帮助警告何时可能发生.

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. MISRA_C:2012 has an essential type model to help warn when this might occur.

规则说明还包括规则的例外.对于规则10.3,一个例外是:如果本质值类型的非负整数常量表达式的值可以表示为该类型,则可以将其赋值给该本质类型的对象.

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.

尚不清楚您的工具在哪个确切的行和列上报告了违规(应该).更好的工具还将提供有关违反规则的哪一部分的更详细的信息(例如,如果不是8,则第一次分配给8位的分配是128,而不是1) ).

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 (e.g. if instead of a 1, you had 128 in the first assignment to a 8-bit, the tool should be very explicit about that).

无论如何,我在这里(我的工具也没有)看不到任何违反10.3的行为.

In any case, I don’t (nor does my tool) see any violation of 10.3 here.

由于这是一个可决定的"规则,因此,如果这是安全性至关重要的代码,那么我会担心该工具,除了这会浪费您的时间.

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).

如果您的工具供应商需要更多信息,则可以将您的问题发布在论坛上,网址为 http://www. misra-c.com 获取正式答案并将其转发给供应商.

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.

这篇关于结构数组初始化中的MISRA-C错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆