We have got a function with 3 static variables. It is weird but we see sometimes one of the 3 static variables is getting reinitialized:
static uchar * Foo( uchar c_par1, uchar c_par2, uchar c_par3 )
{
static char s_var1[10];
static uchar c_var2 = 0;
static uchar c_var3 = 0;
.....
return s_var1;
}
In the above case c_var2 is getting reinitailized sometimes.
We are sure it is reinitailized because we are also printing the memory location of each of these variables and it is never changed. We suspected s_var1 might overwrite the c_var2 but the memory locations of these 2 variables are far apart.
This sounds like a global buffer overflow somewhere.
Run
nm -n a.out, and find out what variables are nearc_var2. Then look for overflows caused by these variables.Or use Address Sanitizer, which should be able to give you exact location of the bug quite easily.
Another way to debug this: run the program under GDB, and set a watchpoint on the address of
c_var2. The watchpoint should fire every timec_var2is modified.If your program is multithreaded, then note that static variables and threads don’t work together well.
If it is not multithreaded, I don’t see how a delay could have any effect (unless you are also processing signals).