Static analyzer invokes incorrect destructors for lifetime-extended temporaries
| Bugzilla Link | 17001 |
| Resolution | FIXED |
| Resolved on | Nov 30, 2016 13:09 |
| Version | trunk |
| OS | Linux |
| Attachments | Patch draft |
| CC | @belkadan,@haoNoQ |
Extended Description
extern void clang_analyzer_eval(bool);
bool x;
struct A { int *a; A() { } ~A() { } }; struct B: public A { B() { } ~B() { x = true; } };
void f() { { const A &a = B(); } clang_analyzer_eval(x); // expected-warning{{TRUE}} }
When a reference bound to a temporary goes out of scope, the destructor of the temporary should fire. However, the analyzer incorrectly determines which destructor to call: it uses the type of the reference variable instead of the type of the actual CXXBindTemporaryExpr. Changing the variable type to 'const B &' produces correct behavior.
I actually consider this to be a bug in the CFG - the correct destructor to call is deducible statically and this information should be available in the CFG. I have made a very proof-of-concept patch (attached), which amends this situation by using CFGTemporaryDtor instead of CFGAutomaticObjDtor nodes to represent these objects. However, given the current state of temporary object support in the analyzer, this causes the it to regress a bit, because it now cannot process any calls to the lifetime-extended temporaries. Therefore, I attach this here mostly just to start the discussion about how to address this issue.
Should be fixed in r288263.
This still manifests on ToT.
I don't think it's that we don't get the right region so much as that we don't inline constructors for temporaries, even lifetime-extended ones, so we can't tell that tho constructor didn't fully initialize the object. But I buy this general line of reasoning, and would be willing to accept this regression in the name of correctness.
This is abstractly the right thing to do, but you're right that it's technically a regression until we get proper support for destroying temporary regions. Now the question is whether we are willing to regress here for the sake of correctness in some other situations or not. And I'm inclining towards the answer "yes". My reasons for that are the following:
-
the current implementation doesn't invoke the destructor correctly anyway. E.g., for code like: struct A { int *x; A() { }; ~A() { *x = 47; } }; void f() { A a; } void g() { const A &a = A(); } I only get the "dereference of undefined value" warning in f(), but not in g(), which would suggest that the destructor is not called with the correct region. This means that any analysis that runs on the destructor will be probably incorrect anyway.
-
My patch, in it's current form breaks, only 3 analyzer tests. Of those, 2 simply verify that the cfg output is what they expect, which can be easily fixed. The third test verifies that a global variable set in the destructor retains it's value after the destructor returns. This will obviously no longer work, but I think this is not a very common use case for destructors (and, due to the first point, this is actually the only use case that really works in the current implementation).
(Also, I'd rather push more 'const' into the CFG, rather than removing 'const' from CFGBuilder. There's no reason why statements accessed via the CFG should ever be non-const.) That sounds like a good idea, I'll keep that in mind when I do the patch for real.
This is abstractly the right thing to do, but you're right that it's technically a regression until we get proper support for destroying temporary regions.
Compounding this is the fact that the destructor may be virtual in B but not in A (the base class), but that's easy to detect once we have something like this, at least.
(Also, I'd rather push more 'const' into the CFG, rather than removing 'const' from CFGBuilder. There's no reason why statements accessed via the CFG should ever be non-const.)
assigned to @devincoughlin