[英]How to resolve MISRA C:2012 Rule 13.2 and 13.3 for C code?
I have C source code and I am making it MISRA Compliant. 我有C源代码,我正在使其符合MISRA标准。 I got an following errors related to MISRA 2012 Rule 13.3 and 13.2:
我收到了与MISRA 2012规则13.3和13.2相关的以下错误:
increment/decrement operation combined with other operation with side-effects [MISRA 2012 Rule 13.3, advisory]buf[count++] = U1RXREG; 增量/减量操作与其他具有副作用的操作相结合[MISRA 2012规则13.3,建议] buf [count ++] = U1RXREG;
both sides have side effects [MISRA 2012 Rule 1.3, required], [MISRA 2012 Rule 13.2, required] buf[count] = U1RXREG; 双方都有副作用[MISRA 2012规则1.3,要求],[MISRA 2012规则13.2,要求] buf [count] = U1RXREG;
Source code for problem 1: 问题1的源代码:
void UART_call(void)
{
if(count < BUF_SIZE)
{
buf[count++] = U1RXREG;
Flag = 1;
}
else
{
count = 0;
Flag = 0;
}
}
After resolving 13.3 error from problem 1 code I am getting MISRA 1.3 and 13.2 errors. 解决问题1代码中的13.3错误后,我收到MISRA 1.3和13.2错误。 Source code for problem 2:
问题2的源代码:
void UART_call(void)
{
if(count < BUF_SIZE)
{
buf[count] = U1RXREG;
count = count + 1U;
Flag = 1;
}
else
{
count = 0;
Flag = 0;
}
}
- increment/decrement operation combined with other operation with side-effects [MISRA 2012 Rule 13.3, advisory]buf[count++] = U1RXREG;
增量/减量操作与其他具有副作用的操作相结合[MISRA 2012规则13.3,建议] buf [count ++] = U1RXREG;
This is as you seem to have noted, solved by moving the incrementation out of the assignment expression: 正如您似乎已经注意到的那样,通过将增量移出赋值表达式来解决:
buf[count] = U1RXREG;
count++;
The rationale behind this is to prevent writing bugs such as buf[count++] = count;
这背后的基本原理是防止编写bug如
buf[count++] = count;
- both sides have side effects [MISRA 2012 Rule 1.3, required], [MISRA 2012 Rule 13.2, required] buf[count] = U1RXREG;
双方都有副作用[MISRA 2012规则1.3,要求],[MISRA 2012规则13.2,要求] buf [count] = U1RXREG;
I'd say this is a false positive. 我会说这是误报。 The line
buf[count] = U1RXREG;
行
buf[count] = U1RXREG;
is harmless. 是无害的。
The reason for the warning is that U1RXREG
is obviously a volatile-qualified rx register of the UART hardware, and MISRA-C doesn't like mixing volatile access with other things in the same expression, particularly not with another "side-effect", in this case the ++
of count together with the assignment to buf
. 警告的原因是
U1RXREG
显然是UART硬件的易失性rx寄存器,而MISRA-C不喜欢将易失性访问与同一表达式中的其他东西混合,特别是不能与另一个“副作用”混合,在这种情况下,计数的++
与buf
的赋值一起。 It's a common source for false positives from static analysers, though sometimes they do find real bugs related to this, as in the &&
case you asked about yesterday . 这是来自静态分析器的误报的常见来源,尽管有时它们确实发现了与此相关的真正错误,就像你昨天提到的
&&
情况一样 。
Assuming 32 bit registers, then the pedantic way to fix it is to use a temporary variable: 假设32位寄存器,那么解决它的迂腐方法是使用临时变量:
uint32_t rxreg = U1RXREG
buf[count] = rxreg;
As far as machine code and program behavior are concerned, this is equivalent to the original code. 就机器代码和程序行为而言,这相当于原始代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.