We have a legacy c++ .dll compiled for windows under visual studio. We have been running into issues where we get different results when we compile the program using different compiler options.
I have done a pretty simple port so that I can compile it under linux using g++. I just wanted to see what kind of warnings gcc would throw at me and possibly try to run it using valgrind to look for possible erros.
So that is the background. Here is the question. We have a bunch of fprintf function calls that print to a log file. When compiled under g++, I get warnings like this.
../f11.cpp:754:128: warning: format ‘%i’ expects type ‘int’, but argument 8 has type ‘long unsigned int’
Obviously this is a bad thing we need to fix, but I am just curious about the potential consequences of ignoring this warning? Are the consequences only limited to the output in the log file, or could this cause things like buffer overruns or any other type of situation where we are overwriting memory without knowing it?
You are getting such warnings because of code like
On a 64 bits x86-64 Linux machine, what is probably happenning is that
printfimplementation would callva_arg(arglist, int)for thexargument. Sinceintis 32 bits andlongis 64 bits, the 64 bits value is probably truncated to its 32 lower bits, which in that particular case probably don’t harm much.If it is a
scanf ("%i", &x);things become much much uglier. Probably, only 32 bits out of 64 of thelong xget changed, and that will break the code later.But as everyone responded, this is undefined behavior. you’ll feel sorry if you don’t fix it or at the very least, add a big fat
/* FIXME */comment for the person working on the code in a few weeks or months.