MISRA C++-2008 Rule 5-0-15 - Array indexing shall be the only form of pointer arithmetic

心不动则不痛 提交于 2019-12-04 21:03:28

There is no problem with your code. It uses array indexing as required. Your static analyser is broken.

I feel the "for" should have condition with "i+3".

for (uint32_t i = 0; i+3 < bufferSize; i+=4)

This could solve the problem. Let me know if it solves.

Ok, I found a way this to work:

byte* buf = new(std::nothrow) byte[bufferSize];
.....
for (uint32_t i = 0; i < bufferSize; i+=4)
{
  ..............           
  uint32_t k = i;
  buf[k] = b;
  k++;
  buf[k] = g;
  k++;
  buf[k] = r;
  k++;
  buf[k] = a;
}

It seems that MISRA does not likes the index arithmetic to be inside the [] brackets. I am not sure if this is not a bug in the tool, maybe it is fixed in the newer Coverity tools.

The following DO NOT work ( MISRA again complains with Rule 5-0-15 violation):

byte* buf = new(std::nothrow) byte[bufferSize];
.....
for (uint32_t i = 0; i < bufferSize; i+=4)
{
  ..............           
  uint32_t k = i;
  buf[k++] = b;
  buf[k++] = g;
  buf[k++] = r;
  buf[k++] = a;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!