<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 - Undefined behavior in int division with INT_MIN param"
   href="https://bugs.llvm.org/show_bug.cgi?id=47973">47973</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Undefined behavior in int division with INT_MIN param
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>compiler-rt
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>11.0
          </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>builtins
          </td>
        </tr>

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

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

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>In the implementation for software integer division a/b (and mod a%b) in
int_div_impl.inc, the absolute values of a and b are calculated like so:

a = (a ^ s_a) - s_a;

(The variable s_a is -1 or 0 depending on the signedness of a.)

In the case where a (or b) is INT_MIN, this can mean that we calculate INT_MAX
+ 1 in signed integer operations. Calculating these as unsigned integers would
avoid this undefined behaviour.

In a similar fashion, the return statement can result in unsigned underflow
because it substracts s_a which gets promoted to unsigned UINT_MAX due to the
COMPUTE_UDIV being unsigned. Rewriting it as + (-s_a) would remove the
undefined behaviour.</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>