[llvm] f3e8f96 - [ADT] Fix itostr handling of min int64_t value

Thomas Preud'homme via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 19 10:38:59 PDT 2020


Author: Thomas Preud'homme
Date: 2020-06-19T18:38:43+01:00
New Revision: f3e8f9617365f129a0e9876fc3d38f15d6f2d4fd

URL: https://github.com/llvm/llvm-project/commit/f3e8f9617365f129a0e9876fc3d38f15d6f2d4fd
DIFF: https://github.com/llvm/llvm-project/commit/f3e8f9617365f129a0e9876fc3d38f15d6f2d4fd.diff

LOG: [ADT] Fix itostr handling of min int64_t value

Summary:
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.

Reviewers: jhenderson, chandlerc, lattner

Reviewed By: lattner

Subscribers: dexonsmith, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D82200

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/StringExtras.h b/llvm/include/llvm/ADT/StringExtras.h
index 56d5f3d05857..990a3054a9d2 100644
--- a/llvm/include/llvm/ADT/StringExtras.h
+++ b/llvm/include/llvm/ADT/StringExtras.h
@@ -245,7 +245,7 @@ inline std::string utostr(uint64_t X, bool isNeg = false) {
 
 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));
 }


        


More information about the llvm-commits mailing list