[llvm-bugs] [Bug 30806] New: Wrong code generation at -O2 leading to division by zero

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Oct 27 04:37:12 PDT 2016


https://llvm.org/bugs/show_bug.cgi?id=30806

            Bug ID: 30806
           Summary: Wrong code generation at -O2 leading to division by
                    zero
           Product: new-bugs
           Version: 3.9
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: clang at martinien.de
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

Created attachment 17500
  --> https://llvm.org/bugs/attachment.cgi?id=17500&action=edit
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

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20161027/f0fa36af/attachment-0001.html>


More information about the llvm-bugs mailing list