<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 --- - Wrong code generation at -O2 leading to division by zero"
   href="https://llvm.org/bugs/show_bug.cgi?id=30806">30806</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Wrong code generation at -O2 leading to division by zero
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.9
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Linux
          </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>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>clang@martinien.de
          </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>Created <span class=""><a href="attachment.cgi?id=17500" name="attach_17500" title="Repro">attachment 17500</a> <a href="attachment.cgi?id=17500&action=edit" title="Repro">[details]</a></span>
Repro

Attached repro causes a division by zero although it shouldn't (I tried my best
to rule out undefined behavior, but if I missed something let me know).

Build with build.sh and run "div_by_zero"

In run.cpp "array" is only being assigned a non-NULL pointer when both a and b
are both > 0:

  if(a > 0 && b > 0) // false at runtime!
  {
    try {
      array.reset(new size_t[b]());
    }
    catch (...)
    {
    }
  }

But later on the following code gets executed even though array.get() must
return NULL:

    if (array.get() != 0) // Should be false at runtime; even adding "&& a > 0"
here does not help
    {
      // Also this crashes:
      //const size_t partSize = a != 0 ? b / a : 0;
      const size_t partSize = b / a;  // Crash here - Division by zero


Side note:
It works at -O1. I know that for earlier clang versions (I think 3.6) this
broke even at -O1, but the workaround seen above ("Also this crashes") worked
there.

Best regards,
Martin</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>