Rule 12.6 and functions taking/returning Boolean values.

6.12 Expressions

Moderators: misra-c, david ward

Post Reply
zaffanella
Posts: 5
Joined: Tue Jun 10, 2008 7:12 pm
Company: University of Parma (Italy)

Rule 12.6 and functions taking/returning Boolean values.

Post by zaffanella » Fri Feb 10, 2012 9:22 am

The second part of Rule 12.6 says that:
"Expressions that are effectively Boolean should not be used as operands to operators other than ..."
The list of the allowed operators has been corrected in TC1 to include &&, ||, !, =, ==, != and ?:

What about the function call operator () ?

If a Boolean value (by enforcement or by construction) is used as an argument to a function having a parameter whose type is Boolean (by enforcement), are we violating Rule 12.6? For instance:

Code: Select all

typedef /* Boolean-by-enforcement type */ bool_t;
void foo(bool_t b);

void bar(int32_t a, int32_t b) {
  bool_t b = (a == b);
  foo(b); /* Non-compliant? */
}
A similar conversion problem exists when returning a Boolean value. In this case Rule 12.6 never applies (even though a Boolean value is being "abused") merely because, strictly speaking, the return statement is not an operator. For instance:

Code: Select all

int32_t bar(int32_t a, int32_t b) {
  bool_t b = (a == b);
  return b; /* Compliant? */
}
Are the two examples above matching what was really meant by Rule 12.6 and TC1?

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

Re: Rule 12.6 and functions taking/returning Boolean values.

Post by misra-c » Mon Feb 20, 2012 1:46 pm

The function call operator should have been included in this list with the caveat that expressions with effectively Boolean type may be used in the argument list where the corresponding parameter also has effectively Boolean type.

An expression with effectively Boolean type may also be used in a return statement and also to initialise an object but Rule 12.6 does not apply to these cases because there are no operators present.
---
Posted by and on behalf of
the MISRA C Working Group

zaffanella
Posts: 5
Joined: Tue Jun 10, 2008 7:12 pm
Company: University of Parma (Italy)

Re: Rule 12.6 and functions taking/returning Boolean values.

Post by zaffanella » Tue Feb 21, 2012 2:07 pm

misra-c wrote:The function call operator should have been included in this list with the caveat that expressions with effectively Boolean type may be used in the argument list where the corresponding parameter also has effectively Boolean type.
OK, so the first example is compliant with respect to the proper meaning of Rule 12.6.
misra-c wrote:An expression with effectively Boolean type may also be used in a return statement and also to initialise an object but Rule 12.6 does not apply to these cases because there are no operators present.
If Rule 12.6 does not apply to return statements (and object inizializations), then which rule is violated by the code in the second example above?
As far as we can tell, the overall guideline is that effectively Boolean types/expressions should not be confused with the proper arithmetic types/expressions, i.e., they need to be clearly separated as is the case for "plain char" types/expressions. However, we seem to be missing a specific rule corresponding to Rule 6.1.

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

Re: Rule 12.6 and functions taking/returning Boolean values.

Post by misra-c » Tue Feb 21, 2012 3:15 pm

zaffanella wrote:If Rule 12.6 does not apply to return statements (and object inizializations), then which rule is violated by the code in the second example above?
As far as we can tell, the overall guideline is that effectively Boolean types/expressions should not be confused with the proper arithmetic types/expressions, i.e., they need to be clearly separated as is the case for "plain char" types/expressions. However, we seem to be missing a specific rule corresponding to Rule 6.1.
There is no rule for effectively Boolean types corresponding to Rule 6.1 although it would clearly be sensible to write code as if such a rule did exist.

The subject of types and expressions will be given much more thorough treatment in the next version of the MISRA C Guidelines. This version, commonly referred to as MISRA C3 is currently undergoing public review in readiness for release later this year.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “6.12 Expressions”