Doubt on rule 13.2

6.13 Control Statement Expressions

Moderators: misra-c, david ward

Post Reply
roberto
Posts: 11
Joined: Wed Jun 04, 2008 10:33 pm
Company: University of Parma, Italy

Doubt on rule 13.2

Post by roberto » Mon Feb 16, 2009 1:01 pm

One of the examples given for rule 13.2 is:

Code: Select all

if ( y ) /* Not compliant, unless y is effectively Boolean data
                              (e.g. a flag) */
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?

Lundin
Posts: 70
Joined: Mon Dec 10, 2007 1:57 pm

Re: Doubt on rule 13.2

Post by Lundin » Thu Feb 19, 2009 2:01 pm

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.

roberto
Posts: 11
Joined: Wed Jun 04, 2008 10:33 pm
Company: University of Parma, Italy

Re: Doubt on rule 13.2

Post by roberto » Fri Feb 20, 2009 4:11 pm

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: Select all

  if ( y ) ...
is not testing a value against zero: it is testing the validity of a pointer and, as you write, this may have nothing to do with zero.
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: Select all

  if ( y ) /* Not compliant, unless y is effectively Boolean data
               (e.g. a flag) or a pointer.  */
Can the MISRA committee shed some light on this?
Thanks,

Roberto

Lundin
Posts: 70
Joined: Mon Dec 10, 2007 1:57 pm

Re: Doubt on rule 13.2

Post by Lundin » Mon Feb 23, 2009 11:14 am

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.

misra-c
Posts: 561
Joined: Thu Jan 05, 2006 1:11 pm

Re: Doubt on rule 13.2

Post by misra-c » Mon Mar 02, 2009 11:25 am

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.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “6.13 Control Statement Expressions”