Doubt on rule 13.2 - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4) +--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17) +---- Forum: 6.13 Control Statement Expressions (https://forum.misra.org.uk/forumdisplay.php?fid=40) +---- Thread: Doubt on rule 13.2 (/showthread.php?tid=628) |
Doubt on rule 13.2 - roberto - 16-02-2009 One of the examples given for rule 13.2 is: Code: if ( y ) /* Not compliant, unless y is effectively Boolean data Would it still be not compliant if y is of pointer type? In case it is compliant, should 13.2 be interpreted as to be applicable only to expressions of integral type? Re: Doubt on rule 13.2 - Lundin - 19-02-2009 The rule says "tests of a value against zero". As I understand it, this applies to everything that isn't Boolean: integers, pointers, float numbers, strings... Pointers are especially troublesome, since they aren't always containing address 0, but could also contain the mysterious address NULL, which could be defined as 0 or (void*)0. Using an implicity comparison can therefore cause your compiler to whine about implicit typecasting. It is generally considered good programming practice to explicitly compare pointers against NULL, no matter what MISRA advises regarding implicit tests against zero. Re: Doubt on rule 13.2 - roberto - 20-02-2009 Hi Lundin, in this moment I am not concerned about good programming practice, but on the genuine interpretation of MISRA rules. In particular, when y is a pointer, Code: if ( y ) ... This reasoning made me think that, perhaps, there is an oversight in the example given on page 65 of MISRA-C:2004: perhaps it should read Code: if ( y ) /* Not compliant, unless y is effectively Boolean data Thanks, Roberto Re: Doubt on rule 13.2 - Lundin - 23-02-2009 But you are testing against zero, rather than NULL. The difference is subtle, and only a matter of coding style. This cannot cause any problems, but it can perhaps cause implicit typecast warnings. I interpret the rule as "everything that isn't Boolean should be explicitly tested". Consider this: if(!y) If y is a pointer, this is surely poor coding style, since ! is a pure Boolean operator. Re: Doubt on rule 13.2 - misra-c - 02-03-2009 The interpretation, as expounded by lundin, is correct. The rule is intended to apply to all tests against 0 and this inclues testing pointers against NULL. The rationale for this rule is to make a clear distinction between types that are being interpreted as Booleans (flags) and another other types. |