I remember when I first used this piece of low-budget software 12 years
ago that it had some interesting bugs, most of which surfaced when you
used 'clever' features of C++, such as pure virtuals, destructors, etc.
I've been using it to do a small TCP/IP implementation for a PCjr and
have been sticking to vanilla C. After a few weeks of coding I've hit a
bug that has just bewildered me.
Take the following code:
#include <stdio.h>
#include "IP.h"
IpAddr_t MyIpAddr;
char *Ip::formatAddress( const IpAddr_t a, char *buffer ) {
char bogus[] = "";
sprintf( buffer, "%d.%d.%d.%d", a[0], a[1], a[2], a[3] );
return buffer;
}
If I *dont* have the 'char bogus[]' line in there, the call to sprintf
always returns zero. If I have that extra line in there, the call to
sprintf works as expected.
Another work-around is to not use the 'char bogus[]' trick, and just
repeat the call to sprintf. The second one will work.
Yet another trick is to move the function to the end of the file ...
After a few hours of wrestling with this I am fairly sure it is a bug in
the linker, and it has to do with the string constant on the sprintf.
If it is the first string constant in the file, it doesn't work. If I
do anything that adds another string constant before that one, it works,
even if the other string constant is never referenced or used.
Has anybody run into something similar to this? I'm using tdump
(supplied with the compiler), the linker's map file, and debug to try to
figure out the code gen and the suspected fixup error, but I can't see
anything obviously wrong. Hints on tracking this would be appreciated.
Mike