<br><br><div class="gmail_quote">On Fri, Nov 16, 2012 at 2:58 AM, David Blaikie <span dir="ltr"><<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Can we warn about this?<br>
<div class="im HOEnZb"><br></div></blockquote><div><br>Unfortunately no. I don't think so. It would require inspecting the body of `cast`...<br><br>Argyrios implemented a whole class of warnings around this theme a while ago now, and as we drilled into it (and I believe you helped) we just decided that looking through functions was too difficult. As such: `T const& foo(T const& t) { return t; }` completely dulls out binding to temporary warnings => the function itself is correct, but its use may not.<br>
<br><br>However it seems to be that the problem is caused by the implementation of `cast` is wrong.<br><br>template <class X, class Y><br>inline typename cast_retty<X, Y>::ret_type cast(const Y &Val) {<br>
  assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");<br>  return cast_convert_val<X, Y,<br>                          typename simplify_type<Y>::SimpleType>::doit(Val);<br>
}<br><br>We accept a `const&`, allowing bind to temporary, but then the return type can be bound to X& which means the `const` qualifier was dropped on the floor. This is pretty nasty.<br><br>The culprit is actually just above:<br>
<br>template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {<br>  // This _is_ a simple type, just cast it.<br>  static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {<br>
    typename cast_retty<To, FromTy>::ret_type Res2<br>     = (typename cast_retty<To, FromTy>::ret_type)<b>const_cast</b><FromTy&>(Val);<br>    return Res2;<br>  }<br>};<br><br><br>However, even overloading on const-ness would not make bind-to-const issues disappear. The only thing I can thing of is:<br>
=> allow `cast` (and al) only on non-const reference<br>=> allow on non-const and const pointers<br><br>which is the only way to circumvent bind-to-temporary situations I know of.<br><br>I am not sure it's worth it.<br>
<br><br>For detection I see only two possibilities:<br>=> Interprocedural (*) static analysis: when analyzing the function, note that its return may be an alias of the parameter; when analyzing the use, note that the statement is only valid if the return is not an alias of the temporary; cross-reference the two and warn about the incompatibility<br>
=> Dynamic instrumentation: I wonder if <b>Asan</b> could catch this (maybe it does already). With the proper instrumentation the destructor of the temporary could trigger poisoning of the memory so that the next use cause an assertion.<br>
<br>(*) Interprocedural in the general case, here it would not be.<br><br>-- Matthieu<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im HOEnZb">

On Thu, Nov 15, 2012 at 5:14 PM, Matt Beaumont-Gay <<a href="mailto:matthewbg@google.com">matthewbg@google.com</a>> wrote:<br>
</div><div class="HOEnZb"><div class="h5">> Author: matthewbg<br>
> Date: Thu Nov 15 19:14:52 2012<br>
> New Revision: 168124<br>
><br>
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=168124&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=168124&view=rev</a><br>
> Log:<br>
> Fix PR14321, a crash when Clang is built with GCC 4.7 at -O1 or greater.<br>
><br>
> GCC 4.7 reuses stack slots fairly aggressively, which exposes more temporary<br>
> lifetime bugs.<br>
><br>
> No new test, this was caught by the existing CodeGenCXX/mangle-ms-templates.cpp.<br>
><br>
> Modified:<br>
>     cfe/trunk/lib/AST/MicrosoftMangle.cpp<br>
><br>
> Modified: cfe/trunk/lib/AST/MicrosoftMangle.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/MicrosoftMangle.cpp?rev=168124&r1=168123&r2=168124&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/MicrosoftMangle.cpp?rev=168124&r1=168123&r2=168124&view=diff</a><br>

> ==============================================================================<br>
> --- cfe/trunk/lib/AST/MicrosoftMangle.cpp (original)<br>
> +++ cfe/trunk/lib/AST/MicrosoftMangle.cpp Thu Nov 15 19:14:52 2012<br>
> @@ -373,7 +373,7 @@<br>
>        dyn_cast<ClassTemplateSpecializationDecl>(ND)) {<br>
>      TypeSourceInfo *TSI = Spec->getTypeAsWritten();<br>
>      if (TSI) {<br>
> -      TemplateSpecializationTypeLoc &TSTL =<br>
> +      TemplateSpecializationTypeLoc TSTL =<br>
>          cast<TemplateSpecializationTypeLoc>(TSI->getTypeLoc());<br>
>        TemplateArgumentListInfo LI(TSTL.getLAngleLoc(), TSTL.getRAngleLoc());<br>
>        for (unsigned i = 0, e = TSTL.getNumArgs(); i != e; ++i)<br>
><br>
><br>
> _______________________________________________<br>
> cfe-commits mailing list<br>
> <a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</div></div></blockquote></div><br>