Recently I found a mistake in my code. it's about an union type local variable has not been initialized, so that there will be unexpected data in the memory.
I'm curious if there is a way to use static analysis tool such as Cppcheck to find similar problems elsewhere in my existing projects. After learning how to use Cppcheck, I found that there's only some MISRA-C style warning such as "The union keyword should not be used" in the code section where I used union local variable. So my question is, is it possible to use Cppcheck to check if the union has been initialized?
Thanks for your time.
Here's my demo code:
typedef unsigned char UBYTE;
typedef unsigned short UWORD;
typedef union
{
UWORD word;
struct
{
UBYTE Lbyte;
UBYTE Hbyte;
}byte;
struct
{
UWORD LBLbit4:4;
UWORD LBHbit4:4;
UWORD HBLbit4:4;
UWORD HBHbit4:4;
}bit4;
struct
{
UWORD bit0:1;
UWORD bit1:1;
UWORD bit2:1;
UWORD bit3:1;
UWORD bit4:1;
UWORD bit5:1;
UWORD bit6:1;
UWORD bit7:1;
UWORD bit8:1;
UWORD bit9:1;
UWORD bit10:1;
UWORD bit11:1;
UWORD bit12:1;
UWORD bit13:1;
UWORD bit14:1;
UWORD bit15:1;
}bit;
}uuWORD;
static UWORD UpdateBit12(UWORD flag)
{
uuWORD data;
if(flag == (UWORD)1)
{
data.bit.bit12 = 1;
}
else
{
data.bit.bit12 = 0;
}
return data.word;
}