Rule 5-0-12

Moderators: david ward, misra cpp

Post Reply
dg1980
Posts: 109
Joined: Wed Apr 27, 2016 2:33 pm
Company: Elektrobit Automotive GmbH

Rule 5-0-12

Post by dg1980 » Thu Sep 15, 2016 1:53 pm

From the official text:

Code: Select all

uint8_t b = '\r';//Non-compliant, explicitly unsigned
Would a static_cast turn this into a compliant statement?

Code: Select all

uint8_t b = static_cast<uint8_t>('\r');//Compliant
Static analysis tool used says yes, hence the question.

dg1980
Posts: 109
Joined: Wed Apr 27, 2016 2:33 pm
Company: Elektrobit Automotive GmbH

Re: Rule 5-0-12

Post by dg1980 » Mon Sep 26, 2016 12:18 pm

Ok, I just tested 2 different MISRA tools and both consider the following code to be compliant with 5-0-12:

Code: Select all

uint8_t str[] = {
 static_cast<uint8_t>('m'),
 static_cast<uint8_t>('i'),
 static_cast<uint8_t>('s'),
 static_cast<uint8_t>('r'),
 static_cast<uint8_t>('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++:
Don't rely on the signedness of plain char. Period.

Code: Select all

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?

misra cpp
Posts: 148
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: Rule 5-0-12

Post by misra cpp » Tue Oct 11, 2016 10:51 am

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

Post Reply

Return to “6.5 Expressions (C++)”