Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 5-0-12
#1
From the official text:
Code:
uint8_t b = '\r';//Non-compliant, explicitly unsigned
Would a static_cast turn this into a compliant statement?
Code:
uint8_t b = static_cast('\r');//Compliant
Static analysis tool used says yes, hence the question.
<t></t>
Reply
#2
Ok, I just tested 2 different MISRA tools and both consider the following code to be compliant with 5-0-12:

Code:
uint8_t str[] = {
static_cast('m'),
static_cast('i'),
static_cast('s'),
static_cast('r'),
static_cast('a')
};

As there seems to be a lot of interpretation going on among tool suppliers, i would suggest stripping down this rule to it´s bare essentials in the next version of MISRA C++:
Quote:Don't rely on the signedness of plain char. Period.
Code:
extern char x; if (x == -1)//non-compliant as it is implementation defined whether char is signed or unsigned
Everything else in this rule is not a real problem IMHO.
In fact, in some communication protocols you have to mix numeric information (e.g. frame length) with character data (e.g. special strings).
How would you do that without deviating from 5-0-12 in it´s current form?
<t></t>
Reply
#3
You (and the tools) are correct, the use of static_cast makes the assignments compliant.

The situation you cite, of the need to mix numeric and character data in communication protocols is a case where a deviation is justified
Posted by and on behalf of
the MISRA C++ Working Group
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)