[PATCH] D146623: [compiler-rt] Fix signed integer overflow in int_mulo_impl.inc

Karl-Johan Karlsson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 22 04:47:18 PDT 2023


Ka-Ka created this revision.
Ka-Ka added reviewers: MaskRay, phosek, atrosinenko.
Herald added subscribers: Enna1, dberris.
Herald added a project: All.
Ka-Ka requested review of this revision.
Herald added a project: Sanitizers.
Herald added a subscriber: Sanitizers.

When compiling compiler-rt with -fsanitize=undefined and running testcases you
end up with the following warning:

UBSan:/repo/uabkaka/llvm-project/compiler-rt/lib/builtins/int_mulo_impl.inc:24:23: signed integer overflow: -1 * -2147483648 cannot be represented in type 'si_int' (aka 'long')

This can be avoided by doing the multiplication in a matching unsigned variant
of the type.

This was found in an out of tree target.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146623

Files:
  compiler-rt/lib/builtins/int_mulo_impl.inc


Index: compiler-rt/lib/builtins/int_mulo_impl.inc
===================================================================
--- compiler-rt/lib/builtins/int_mulo_impl.inc
+++ compiler-rt/lib/builtins/int_mulo_impl.inc
@@ -21,7 +21,7 @@
   const fixint_t MIN = (fixint_t)((fixuint_t)1 << (N - 1));
   const fixint_t MAX = ~MIN;
   *overflow = 0;
-  fixint_t result = a * b;
+  fixint_t result = (fixuint_t)a * b;
   if (a == MIN) {
     if (b != 0 && b != 1)
       *overflow = 1;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146623.507310.patch
Type: text/x-patch
Size: 474 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230322/5b476280/attachment.bin>


More information about the llvm-commits mailing list