<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <br>
    <div class="moz-cite-prefix">On 07/18/2014 02:29 AM, Andreas Weber
      wrote:<br>
    </div>
    <blockquote
cite="mid:CAMehOd0GDNCVCg2Sc1+RP_YadVw8+2PtCqiFYW2RhTWmAiaY0w@mail.gmail.com"
      type="cite">
      <div dir="ltr">Hi,<br>
        <br>
        I think I ran into a rather subtle bug inside
        llvm/ADT/ArrayRef.h which only shows up when compiling the code
        with GCC-4.8 with switched-off optimizations. (Both clang and
        GCC-4.7 don't trigger the bug.)<br>
        <br>
        I already filed a bug against GCC-4.8 which was rejected by the
        GCC-folks as being invalid, because the code (basically
        ArrayRef.h)  "is doing something bad - it's retaining a pointer
        to a temporary object across a function call." They also
        provided a detailed explanation for their opinion, which I think
        is correct. See this link for the full story: <a
          moz-do-not-send="true"
          href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61015"
          target="_blank">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61015</a><br>
      </div>
    </blockquote>
    I think you may have misread the explanation.  I'm not entirely sure
    the explanation of the spec behaviour is correct, but let's leave
    that discussion to people more knowledgeable then myself.  <br>
    <br>
    If I'm reading the link above right, the issue is that you used:<br>
    llvm::ArrayRef<Obj*> arrayRef( pSpecial );<br>
    Instead of:<br>
    llvm::ArrayRef<SpecialObj*> arrayRef( pSpecial );<br>
    <br>
    The former requires the creation of a temporary variable; the latter
    does not.  GCC 4.8 exploits that temporary variable.  Clang should
    if we don't already.  :)<br>
    <br>
    <br>
    <blockquote
cite="mid:CAMehOd0GDNCVCg2Sc1+RP_YadVw8+2PtCqiFYW2RhTWmAiaY0w@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <br>
        The following program demonstrates the bug using LLVM's own
        ArrayRef:<br>
        #define __STDC_LIMIT_MACROS<br>
        #define __STDC_CONSTANT_MACROS<br>
        <br>
        #include <cstdio><br>
        #include <llvm/ADT/ArrayRef.h><br>
        <br>
        class Obj {};<br>
        class SpecialObj : public Obj {};<br>
        <br>
        int main()<br>
        {<br>
          SpecialObj* pSpecial = new SpecialObj();<br>
        <br>
          llvm::ArrayRef<Obj*> arrayRef( pSpecial ); //Breaks on
        GCC-4.8.<br>
        <br>
          /* Obj* pObj = pSpecial;<br>
          llvm::ArrayRef<Obj*> arrayRef( pObj ); //Possible
        Workaround */<br>
        <br>
          int someStackArray[500];<br>
          memset( someStackArray, 0xdd, sizeof(someStackArray) );<br>
        <br>
          if( arrayRef[0] != pSpecial )<br>
            printf( "This shouldn't happen: %p\n", arrayRef[0] );<br>
          else<br>
            printf( "Expected behaviour.\n" );<br>
          return 0;<br>
        }<br>
        <br>
        Compiling (and then executing) this program with <br>
          g++-4.8 -Wall -O0 -std=c++11 -I./LLVM_SVN/installed/include
        main.cpp<br>
        prints: "This shouldn't happen: 0xdddddddddddddddd"<br>
        <br>
        Compiling with<br>
          clang++ -Wall -O0 -std=c++11 -I./LLVM_SVN/installed/include
        main.cpp<br>
        prints: "Expected behaviour." <br>
        <br>
        I think (as a quick fix) we should remove the const qualifier
        from ArrayRef's CTor argument, so that the above code won't
        compile anymore and thus avoiding a silent failure.<br>
        To be precise:<br>
        Change llvm/ADT/ArrayRef.h:57 from<br>
         ArrayRef(const T &OneElt)<br>
              : Data(&OneElt), Length(1) {}<br>
        to<br>
         ArrayRef(T &OneElt)<br>
              : Data(&OneElt), Length(1) {}<br>
        <br>
        Best regards,<br>
        Andreas</div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
LLVM Developers mailing list
<a class="moz-txt-link-abbreviated" href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a class="moz-txt-link-freetext" href="http://llvm.cs.uiuc.edu">http://llvm.cs.uiuc.edu</a>
<a class="moz-txt-link-freetext" href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>