Mail Archives: djgpp/2000/01/11/18:56:35
Robert S Whitlock <roberts DOT j DOT whitlock AT juno DOT com> wrote:
> For me, this is truly a wtf. Any ideas?
There could be other bugs, but I found this one simply by grepping for
strcat. This is really all the code you needed to post:
/* ... */
char *sdk_log(char *msg)
{
if (logfile != (FILE *) NULL)
{
fprintf(logfile,strcat(msg," typed a line\n"));
//fprintf(logfile,msg);
fflush(logfile);
}
return msg;
}
/* ... */
sdk_log("Failure to initialize Allegro");
This won't work.
You are passing a *constant* string literal to sdk_log, then you
modify that string. The compiler has made code to assume the string
would not be modified, but it did not catch the error because the
constness is lost when msg is declared as char*.
Preferred solution: declare msg as const char* and change sdk_log() so
it will work without modifying msg.
Or be sure to call sdk_log() only with strings that have enough spare
space for whatever you want to append.
Casting 'const char*' to 'char*' is legal (for historical reasons) but
is a bad thing because it encourages bugs (like the one you have).
Compiling with -Wwrite-strings might help to point out places where
this is happening.
Hope that helps.
- Raw text -