<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><span class="vcard"><a class="email" href="mailto:richard-llvm@metafoo.co.uk" title="Richard Smith <richard-llvm@metafoo.co.uk>"> <span class="fn">Richard Smith</span></a>
</span> changed
              <a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED INVALID - UBSan detects signed integer overflow for multiplication of uint16_t"
   href="https://llvm.org/bugs/show_bug.cgi?id=25580">bug 25580</a>
        <br>
             <table border="1" cellspacing="0" cellpadding="8">
          <tr>
            <th>What</th>
            <th>Removed</th>
            <th>Added</th>
          </tr>

         <tr>
           <td style="text-align:right;">Status</td>
           <td>NEW
           </td>
           <td>RESOLVED
           </td>
         </tr>

         <tr>
           <td style="text-align:right;">CC</td>
           <td>
                
           </td>
           <td>richard-llvm@metafoo.co.uk
           </td>
         </tr>

         <tr>
           <td style="text-align:right;">Resolution</td>
           <td>---
           </td>
           <td>INVALID
           </td>
         </tr></table>
      <p>
        <div>
            <b><a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED INVALID - UBSan detects signed integer overflow for multiplication of uint16_t"
   href="https://llvm.org/bugs/show_bug.cgi?id=25580#c1">Comment # 1</a>
              on <a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED INVALID - UBSan detects signed integer overflow for multiplication of uint16_t"
   href="https://llvm.org/bugs/show_bug.cgi?id=25580">bug 25580</a>
              from <span class="vcard"><a class="email" href="mailto:richard-llvm@metafoo.co.uk" title="Richard Smith <richard-llvm@metafoo.co.uk>"> <span class="fn">Richard Smith</span></a>
</span></b>
        <pre>Not a bug: this multiplication has undefined behavior.

Remember that C and C++ perform the usual arithemtic conversions on operands of
arithmetic operations prior to performing the arithmetic, and that the usual
arithmetic conversions always result in a type at least as large as 'int' (and
generally give the smallest integral type that can hold all the positive values
of both operands and of int).

So in your case, the multiplication is performed by first converting both
operands to 'int', then multiplying (which overflows, resulting in undefined
behavior), then converting back to uint16_t to initialize your function's
return value.

Note in particular that decltype(X * Y) is int, not unsigned short.</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>