AYUSH KUMAR
AYUSH KUMAR

Reputation: 45

MISRA C-2012 Rule 10.3 Violation?

When I try to add these macros:

#define BOOT_VD_TRUE  ((uint8_t)(0x00U))
#define BOOT_VD_FALSE ((uint8_t)(0x01U))

It violates MISRA C-2012 Rule 10.3: Implicit conversion of (uint8_t)1U from essential type "boolean" to different or narrower essential type "unsigned 8-bit int".

I tried removing type cast but still it considers 0 and 1 as booleans and not integers.

Function Definition is as follows:

uint8_t bootInitFlag(uint8_t Val) 
{ 
   uint8_t bootFlag = Val;
   // Doing something
}

bootInitFlag(BOOT_VD_TRUE);

Upvotes: 3

Views: 964

Answers (2)

Andrew
Andrew

Reputation: 2322

As Lundin states in his answer, this appears to be a tool configuration issue... talk to your vendor.

--

But... noting your code:

#define BOOT_VD_TRUE  ((uint8_t)(0x00U))   /* ADB notes: TRUE  = 0 */
#define BOOT_VD_FALSE ((uint8_t)(0x01U))   /* ADB notes: FALSE = 1 */

I assume your specifying of TRUE = 0 and FALSE = 1 is intentional? But guaranteed to confuse the heck out of most people... so I strongly advocate using the traditional false = 0, true = 1

--

However...

You don't specify which version of C or which compiler you are using, but unless you are still using a fully-conformant C90-only compiler, the recommendation has to be to use <stdbool.h> (and even in the era of C90, most compilers had adopted the proposed <stdbool.h> as a extension):

And even if you insist on using the intermediate names, you can use:

#include <stdbool.h>

#define BOOT_VD_TRUE  false    // ADB notes: reverse logic retained
#define BOOT_VD_FALSE true     // ADB notes: reverse logic retained

void bootInitFlag( bool Val )  // ADB notes: void, as you're not returning anything
{ 
   bool bootFlag = Val;
   // Doing something
}

...

bootInitFlag( BOOT_VD_TRUE );

Upvotes: 1

Lundin
Lundin

Reputation: 214780

In order for any MISRA checker to work as intended, it needs to know what your boolean types are. Normally they are bool, true, false unless you are stuck with ancient C90. In that case you need to actively tell the tool what your boolean types are or it can't know that.

A tool which assumes that some of your types are essentially boolean based on the naming is just broken. Bug report it.

Upvotes: 1

Related Questions