用零初始化数组或结构会导致 Misra 违反 Rule11.9
我正在使用 Klocworks 检查 C99 上的 MISRA 违规情况。 当初始化 u8(unsigned char) 类型的数组或第一个元素为相同类型的结构时,它违反了 MISRA 规则 11.9。
示例:
typedef unsigned char u8;
typedef {
u8 a;
u8 b;
u8 c;
} tmyStruct;
const tmyStruct myStruct = { // MISRA.LITERAL.NULL.PTR.CONST.2012(Rule 11.9)
.a = (u8)0,
.b = (u8)0,
.c = (u8)1
};
或
typedef {
u16 a;
u8 b;
u8 c;
} tmyStruct;
tmySructArr[3] =
{
{
.a = (u16)0,
.b = (u8)0,
.c = (u8)1
},
{
.a = (u16)0,
.b = (u8)0,
.c = (u8)1
},
{
.a = (u16)0,
.b = (u8)0,
.c = (u8)1
},
};
I am using Klocworks to check MISRA violations on C99.
When initializing an array of type u8(unsigned char) or a structure whose first element is of the same type, it shows a violation to MISRA Rule 11.9.
Example:
typedef unsigned char u8;
typedef {
u8 a;
u8 b;
u8 c;
} tmyStruct;
const tmyStruct myStruct = { // MISRA.LITERAL.NULL.PTR.CONST.2012(Rule 11.9)
.a = (u8)0,
.b = (u8)0,
.c = (u8)1
};
OR
typedef {
u16 a;
u8 b;
u8 c;
} tmyStruct;
tmySructArr[3] =
{
{
.a = (u16)0,
.b = (u8)0,
.c = (u8)1
},
{
.a = (u16)0,
.b = (u8)0,
.c = (u8)1
},
{
.a = (u16)0,
.b = (u8)0,
.c = (u8)1
},
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MISRA C:2012 规则 11.9 规定
虽然我看到几个零值常量,但我没有看到任何指针,因此我礼貌地建议这是误报,您需要将其提交给 Klokwork /强制支持。
PS:请放弃自定义整数类型,并使用
请注意我的从属关系简介!
Rule 11.9 of MISRA C:2012 states
While I see several zero value constants, I don't see any pointers, so I politely suggest that this is a false positive, and that you need to refer this to Klokwork/Perforce support.
PS: Please ditch the custom integer types, and use
<stdint.h>
Please note profile for my affiliations!