This bug only pops up occasionally. It was very hard for me to track down and verify that it was happening at all, and then reproduce it.
This is complicated by the fact that this is a CGI (web-based) program, so I can only test it through a web browser.
I have a function that does some work on variables passed by address. Somehow, between the time the function returns, and the time those variables are used, the data is changing.
This is basically what's happening:
Code:
void my_function( char *data, int *length ) {
// various stuff is done to data
// length may be changed and is set
printf( "before return: %ld characters: %s\n", *length, data );
}
int other_function() {
char *data;
int length;
// stuff, the data pointer gets malloc'd, etc
my_function( data, &length );
printf( "after return: %ld characters: %s\n", length, data );
}
At execution, one would expect the "before" and "after" printed lines to be the same ... and usually they are.
However, rarely, they aren't. After some painful work, I discovered that the data pointer is getting changed to point at a different address.
How on earth might that be happening, and what sort of bug should I be looking for?
My first thought was that I have a buffer overrun someplace, but I don't know where to begin to track that down. My understanding is that when a function returns, nothing gets done except the stack variables associated with it get released ... but I can't for the life of me imagine why that would corrupt the value of a pointer declared elsewhere.
The program is not multithreaded.
Help ...