[llvm-bugs] [Bug 25580] UBSan detects signed integer overflow for multiplication of uint16_t

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Nov 19 17:22:45 PST 2015


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

Richard Smith <richard-llvm at metafoo.co.uk> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |richard-llvm at metafoo.co.uk
         Resolution|---                         |INVALID

--- Comment #1 from Richard Smith <richard-llvm at metafoo.co.uk> ---
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.

-- 
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/20151120/7f913476/attachment.html>


More information about the llvm-bugs mailing list