用零初始化数组或结构会导致 Misra 违反 Rule11.9

发布于 2025-01-10 23:34:29 字数 592 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜扑 2025-01-17 23:34:29

MISRA C:2012 规则 11.9 规定

NULL应是唯一允许的整数形式null
指针常量

虽然我看到几个零值常量,但我没有看到任何指针,因此我礼貌地建议这是误报,您需要将其提交给 Klokwork /强制支持。

PS:请放弃自定义整数类型,并使用

请注意我的从属关系简介!

Rule 11.9 of MISRA C:2012 states

The macro NULL shall be the only permitted form of integer null
pointer constant.

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文