简体   繁体   中英

cert C and misra fixes

I need to solve misra related issues and cert related issues

uint8_t      globalRegion;
uint8_t      tcc;
uint16_t     paramId;
globalRegion = 9U;

If I remove U i get misra etype assign error stating that usgined int is compared with signed value.

globalRegion = 9;

But if i add UI am getting error STRONG.TYPE.ASSIGN.CONST A value '9U' is assigned to the strongly typed variable 'globalRegion' of type 'uint8_t'

globalRegion = 9U;

If TYPECASTED will get another error porting.cast.size Cast of an expression to a type of potentially incompatible size

globalRegion = ((unit8_t)9U);

how to resolve this error without getting any new error

globalRegion = 9U; is MISRA compliant. An uint8_t belong to the type group "essentially unsigned" so it is correct and required by MISRA to use U suffix when assigning an integer constant to that variable.

It would appear that some Klocwork-specific thing STRONG.TYPE.ASSIGN.CONST is meant to warn about using so-called "magic numbers". It doesn't seem to have anything to do with the 'U' as such. It is widely recognised that using magic numbers in code is considered bad practice, though I can't recall that MISRA or CERT has an explicit rule against them.

You could try to do globalRegion = meaningful_name; instead, where meaningful_name should be replaced some sensibly named constant. You could try to either do it in the form #define meaningful_name 9U or const uint8_t meaningful_name = 9U; .

On the other hand, the Klocwork manual is babbling about "ANSI" as if it's still the year 1989, so that manual isn't exactly building up confidence in the correctness and usefulness of that tool...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM