Page 1 of 1

12.4, reading volatile side effect

Posted: Wed Oct 13, 2010 8:29 am
by jdp
Hi,

It's the first Misra compliant project I'm involved in and I've some troubles to understand some rules. On in particular is rule 12.4. I've some expression like

Code: Select all

if (xxx && REGISTER)
where REGISTER is a deference to a volatile pointer (represents the content of a microcontroller register) declared as following:

Code: Select all

#define REGISTER            (*((uword volatile *) 0xEA2CU))
The right and of && doesn't comply to rule 12.4 because of the volatile access. I don't understand why reading a volatile content is not Misra compliant. The problem is that registers access are defined as volatile and I don't know how to overcome this to make my statement compliant.

Thanks for your help.

Re: 12.4, reading volatile side effect

Posted: Wed Oct 13, 2010 12:36 pm
by Lundin
The MISRA compliant way would be:

Code: Select all

uword reg;
reg = REGISTER;
if(xxx && (reg > 0)) /* no side effect here */
Most of the time, reading volatiles is harmless. You have to evaluate each volatile access from case to case and determine if reading it would be harmful.

Note that all operands of a conditional statement must be of boolean type, according to other MISRA rules. You cannot write if(REGISTER) etc to check against zero, it has to be if(REGISTER > 0).

Re: 12.4, reading volatile side effect

Posted: Thu Oct 14, 2010 7:55 am
by Lundin
Lundin wrote:Note that all operands of a conditional statement must be of boolean type
Oops this isn't correct. Scratch "all operands..." and change that to "the expression...".

Re: 12.4, reading volatile side effect

Posted: Wed Nov 03, 2010 9:38 am
by misra-c
Rule 12.4 does not permit side-effects in the right hand operand of a logical operator or in the 2nd and 3rd operands of a conditional operator. The reason for this restriction is that the side-effect may or may not occur.

The method suggested by the previous poster is compliant with Rule 12.4 because the side-effect always occurs.

Re: 12.4, reading volatile side effect

Posted: Thu Aug 06, 2015 5:32 pm
by m4l490n
misra-c wrote:The reason for this restriction is that the side-effect may or may not occur.

The method suggested by the previous poster is compliant with Rule 12.4 because the side-effect always occurs.

Could you please explain why does the side-effect may or may not occur? and Why the expression if(xxx && (reg > 0)) forces the side-effect to always occur?

Regards.

Re: 12.4, reading volatile side effect

Posted: Fri Oct 09, 2015 12:41 pm
by misra-c
The && operator does not evaluate its right-hand operand if the left-hand operand evaluates to False. Therefore any side-effect in the right-hand expression will not occur if the left-hand expression evaluates to False.

Re-writing the code ensures that the volatile is always read.

Code: Select all

#define REGISTER            (*((uword volatile *) 0xEA2CU))
uword reg;
reg = REGISTER;             /* volatile read will always happen */
if(xxx && (reg > 0)) 
whereas this is not the case in the original code

Code: Select all

#define REGISTER            (*((uword volatile *) 0xEA2CU))
if (xxx && REGISTER)        /* volatile read will only occur if xxx evaluates to True */