I just refactored some old C code of mine. Surprisingly the updated code failed in a very unexpected manner that I still don't understand. I narrowed the problem down and here is what the relevant code looks like:
static uint8_t _x, _y; // ... _x and _y get both initialized and updated elsewhere
void setOutput(uint8_t val) { ...}void someFunction() {
static uint8_t tmp;
...
swich(..) {
case foo: {
// tmp = _x & _y;
setOutput( _x & _y);
}
...
}
}
The code fails because the _x & _y expression suddenly passes total garbage
to the setOutput() function. Originally I had used the commented tmp assignment
and then used tmp as an argument for that call - and that functions correctly.
However what I find most puzzling is this: simply uncommenting the "tmp" assignment - without even using tmp later) makes the problem disappear...WTF?!
I am still using an older Emscripten version 1.38 and the effect is there regardless if I compile to WASM or asm.js and whether or not I am using different optimizer options or the closure compiler.
Any ideas what is going on here? Is this some kind of known bug or some C feature that I am not aware of..