<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - UBSan: missing check for accessing an inactive member of an union"
   href="https://llvm.org/bugs/show_bug.cgi?id=25887">25887</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>UBSan: missing check for accessing an inactive member of an union
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Frontend
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>gonzalobg88@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Just discussed in this range-v3 issue:
<a href="https://github.com/ericniebler/range-v3/issues/239">https://github.com/ericniebler/range-v3/issues/239</a>

both Eric and Casey give good examples. Here Eric's example:

template<typename F, typename S>
struct pair_data
{
    union
    {
        F non_const_first_;
        F const first;
    };
    union
    {
        S non_const_second_;
        S const second;
    };
};

template<typename F, typename S>
struct pair : private pair_data<F, S>
{
    using pair_data<F, S>::first;
    using pair_data<F, S>::second;
    pair() : pair_data<F, S>{} {}
    pair(F f, S s) : pair_data<F, S>{f, s} {}
    ~pair()
    {
        first.~F();
        second.~S();
    }
};

int main()
{
    pair<int, float> f{1, 3.14f};
    return f.first;  // UB
    // more UB: destructors of the non-active members (first, second) are
called
    // instead of the destructors of the active members (first_, second_)
}

Aggregate initialization of the union initialize the first member "first_".
Accessing first thus access a non-active member of the union and results in
undefined behavior.</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>