<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><span class="vcard"><a class="email" href="mailto:ahmed.bougacha@gmail.com" title="Ahmed Bougacha <ahmed.bougacha@gmail.com>"> <span class="fn">Ahmed Bougacha</span></a>
</span> changed
              <a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED INVALID - UBSan reports downcast/upcast of misaligned address in std::set<long long>"
   href="https://llvm.org/bugs/show_bug.cgi?id=23413">bug 23413</a>
        <br>
             <table border="1" cellspacing="0" cellpadding="8">
          <tr>
            <th>What</th>
            <th>Removed</th>
            <th>Added</th>
          </tr>

         <tr>
           <td style="text-align:right;">Status</td>
           <td>NEW
           </td>
           <td>RESOLVED
           </td>
         </tr>

         <tr>
           <td style="text-align:right;">CC</td>
           <td>
                
           </td>
           <td>ahmed.bougacha@gmail.com
           </td>
         </tr>

         <tr>
           <td style="text-align:right;">Resolution</td>
           <td>---
           </td>
           <td>INVALID
           </td>
         </tr></table>
      <p>
        <div>
            <b><a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED INVALID - UBSan reports downcast/upcast of misaligned address in std::set<long long>"
   href="https://llvm.org/bugs/show_bug.cgi?id=23413#c2">Comment # 2</a>
              on <a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED INVALID - UBSan reports downcast/upcast of misaligned address in std::set<long long>"
   href="https://llvm.org/bugs/show_bug.cgi?id=23413">bug 23413</a>
              from <span class="vcard"><a class="email" href="mailto:ahmed.bougacha@gmail.com" title="Ahmed Bougacha <ahmed.bougacha@gmail.com>"> <span class="fn">Ahmed Bougacha</span></a>
</span></b>
        <pre>Aah, I remember this!  I think this is actually a legitimate libstdc++ bug;
gcc's implementation of UBSan doesn't catch it, but there's undefined behavior
nonetheless.

The RB tree iterator (used for std::set/map) is defined as a pointer to a node
struct.  This struct subclasses the "base" node type (with the color and
children and whatnot), adding the value type as a (properly aligned) field.

std::set/map::end(), however, takes a pointer to the base node type, and casts
it to the iterator type, i.e., a pointer to the actual node type.  The latter
has alignment >= that of the value type.  The former can be (and is) less
aligned, with an alignment == that of the children/color types.

The alignment sanitizer complains, because the cast by itself is undefined
behavior: quoting C11 6.3.2.3, paragraph 7:

<span class="quote">> A pointer to an object type may be converted to a pointer to a different
> object type. If the resulting pointer is not correctly aligned 68) for the
> referenced type, the behavior is undefined.</span >

Let me know if I missed something!</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>