We use Parasoft C++test to statically analyze our code. It's having trouble with code like the following:
void foo(int* x) {
try {
bar();
} catch(...) {
delete x;
throw;
}
*x;
}
It warns on the *x;
line that:
Freed memory shouldn't be subsequently accessed under any circumstances
Somehow it's concluded that control flow can pass into the catch(...)
block, delete x
, go past the throw;
, and make it to *x;
. I tried throw std::exception("");
and a couple others and got the same thing. Parasoft certainly knows about exceptions and incorporates them into its control flow, because there are many other tests that involve exception checking. Is it just confused in this case, or is there actually some way for the execution of this program to hit both delete x;
and *x;
?
Perhaps this is a daft suggestion, but what does Parasoft say if you leave the catch for the end? I.e.
I realize that may not work for all combinations of statements and exceptions, for example if you have multiple exception types to catch with different handling at different stages of foo, but at least it may give you the start of a workaround if you really want to get rid of the warning.