I came across this bug in some code running on a Blackfin 533 processor.
The first time Func()
runs, fooStruct
will contain garbage, but in the next iteration, the old value that was returned by getFoo()
will by chance still be in fooStruct.foo
.
FooStruct
{
double foo;
double bar;
};
void Func()
{
FooStruct fooStruct;
double bar = 123.4 / fooStruct.foo;
fooStruct.foo = getFoo();
fooStruct.bar = bar;
}
That means that the first time this runs, we are reading from an uninitialized variable, which is undefined behavior. What about the following iterations? Is that still undefined behavior? What sort of behavior can we expect to see when reading uninitialized variables on embedded processors?
One undefined behaviour has been encountered, the behaviour of that and all subsequent statements is undefined too.
Paradoxically, the behaviour of any statements prior to the undefined one are undefined too.
As for the sort of behaviour, asking to categorise undefined behaviour is not logical.