MISRA Discussion Forums

Full Version: Rule 5-0-12
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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?
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