[cfe-dev] GCC's "Temporaries May Vanish Before You Expect"

Jordan Rose jordan_rose at apple.com
Wed Sep 12 16:55:56 PDT 2012


On Sep 12, 2012, at 16:50 , Richard Smith <richard at metafoo.co.uk> wrote:

> On Wed, Sep 12, 2012 at 4:32 PM, Jordan Rose <jordan_rose at apple.com> wrote:
>  
> >> The second way is if -fcatch-undefined-behavior could be augmented to handle this case. I have no idea how to do this, though -- perhaps it requires something ASan-ish to poison the temporary memory once its lifetime ends.
> >>
> >> Any chance of either of these happening on trunk?
>  
> We'd want to wait until the end of the storage duration rather than the end of the lifetime of the object, but yes, we could do that (for instance, we could emit llvm lifetime intrinsics for all local variables, and teach ASan to poison memory based on them).

Well, I'm not entirely sure what you mean by this, but AFAICT the issue is that GCC thinks the storage duration ends sooner than we think it does. In this test case (with a recent Xcode clang), we're definitely calling the destructor before we print the field.

#include <iostream>

struct A {
	int x;
	const A *get() const { return this; } 
	~A() { std::cout << "bye\n"; }
};

int main() {
	const A *a = A().get();
	std::cout << a->x << "\n";
}


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120912/7706ac71/attachment.html>


More information about the cfe-dev mailing list