<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - dynamic_cast to ambiguous base produces the wrong answer"
   href="https://bugs.llvm.org/show_bug.cgi?id=33439">33439</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>dynamic_cast to ambiguous base produces the wrong answer
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>All
          </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>C++
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>arthur.j.odwyer@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the following class hierarchy:

#include <stdio.h>

struct Class2 { virtual ~Class2() {} };
struct Class3 { virtual ~Class3() {} };
struct Class4 : public Class3 {};
struct Class8 : public Class2, public virtual Class4 {};
struct Class9 : public Class4, public Class8 {};

int main() {
    Class9 c9;
    Class2 *c2 = static_cast<Class2 *>(&c9);
    printf("cast Class2 to Class4: %p\n", dynamic_cast<Class4 *>(c2));
}

Clang correctly produces a warning diagnostic:

prog.cc:7:17: warning: direct base 'Class4' is inaccessible due to ambiguity:
    struct Class9 -> struct Class4
    struct Class9 -> struct Class8 -> struct Class4 [-Winaccessible-base]
struct Class9 : public Class4, public Class8 {};
                ^~~~~~~~~~~~~
1 warning generated.

But when you run the code, the dynamic_cast succeeds instead of failing as
expected.

cast Class2 to Class4: 0x7ffc2c41edd0

What we see there is the address of the inaccessible non-virtual base object,
NOT the address of the accessible virtual base object. (Not that the latter
would have been any more correct.)  What we SHOULD see is a null pointer,
because dynamic_cast to an ambiguous base is supposed to fail.

GCC and MSVC get it right.

(This was caught by the same fuzzer as <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - dynamic_cast from one virtual base to another produces the wrong answer"
   href="show_bug.cgi?id=33425">bug 33425</a>, but it is a distinct bug
triggered under different conditions.)</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>