While working on WinROTT, I have come across a couple of warnings that may actually be serious bugs. One warning is C4309: 'initializing' : truncation of constant value.
This appears to be innocuous. But maybe it's not.
The following statement generates a C4309 warning:
The value of tx is -128 after this declaration and initalization occurs.
Let's assume this is followed by:
// value of tp is 128
unsigned char tp = 0x80;
bool tc = tx == tp;
What's the value of tc? You might think it is true. But in reality, the comparison is -128 == 128 which is false.
This may be a contrived example. But consider when you are working in a normal c++ program, where the type may be hidden:
// header file (.h)
#define SF_BAT 0x80
// Forward only linked list
typedef struct statestruct
{
byte rotate;
short shapenum; // a shapenum of -1 means get from ob->temp1
short tictime;
void (*think) (void *);
signed char condition; // GH originally signed char
// Caused 0x80 to be assigned as -128
struct statestruct *next; // Link to next struct
} statetype;
// implementation file (.cpp)
// C4309 warning occurs here
statetype s_batblast4 = {FALSE,BATBLAST4,3,T_Projectile,SF_BAT,&s_batblast1};
// Here's where the execution problem occurs
// This comparison will always return false
if (s_batblast4->condition == SF_BAT)
{
// Do something here
}
The moral is, don't ignore warnings. They may not be harmless. In this case, changing condition to unsigned char is the correct fix.