[PATCH] D82200: [ADT] Fix itostr handling of min int64_t value

Thomas Preud'homme via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 19 08:38:15 PDT 2020


thopre created this revision.
thopre added reviewers: jhenderson, chandlerc, lattner.
thopre added a project: LLVM.
Herald added a subscriber: dexonsmith.

UBSan buildbot caught an undefined behavior in itostr with INT64_MIN.
The negation cannot be represented in the promoted operand (long long).
Negation is well defined on unsigned value though so this commit does
the negation after the static cast.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D82200

Files:
  llvm/include/llvm/ADT/StringExtras.h


Index: llvm/include/llvm/ADT/StringExtras.h
===================================================================
--- llvm/include/llvm/ADT/StringExtras.h
+++ llvm/include/llvm/ADT/StringExtras.h
@@ -245,7 +245,7 @@
 
 inline std::string itostr(int64_t X) {
   if (X < 0)
-    return utostr(static_cast<uint64_t>(-X), true);
+    return utostr(-static_cast<uint64_t>(X), true);
   else
     return utostr(static_cast<uint64_t>(X));
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82200.272090.patch
Type: text/x-patch
Size: 433 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200619/8e05fa6d/attachment.bin>


More information about the llvm-commits mailing list