[Lldb-commits] [lldb] 2501e86 - [lldb/Scalar] Fix undefined behavior

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Tue Jun 30 12:41:48 PDT 2020


Author: Jonas Devlieghere
Date: 2020-06-30T12:41:41-07:00
New Revision: 2501e86acda2905e50012f7e9fc1942517c1237d

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

LOG: [lldb/Scalar] Fix undefined behavior

Fix UBSan error detected in TestDataFormatterObjCCF.py and
TestDataFormatterObjCNSDate.py:

Scalar.cpp:698:27: runtime error: -4.96303e+08 is outside the range of
representable values of type 'unsigned long long'.

Added: 
    

Modified: 
    lldb/source/Utility/Scalar.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Utility/Scalar.cpp b/lldb/source/Utility/Scalar.cpp
index 610c935409ac..d275f6211e5c 100644
--- a/lldb/source/Utility/Scalar.cpp
+++ b/lldb/source/Utility/Scalar.cpp
@@ -736,7 +736,17 @@ long long Scalar::SLongLong(long long fail_value) const {
 }
 
 unsigned long long Scalar::ULongLong(unsigned long long fail_value) const {
-  return GetAsUnsigned<unsigned long long>(fail_value);
+  switch (m_type) {
+  case e_double: {
+    double d_val = m_float.convertToDouble();
+    llvm::APInt rounded_double =
+        llvm::APIntOps::RoundDoubleToAPInt(d_val, sizeof(ulonglong_t) * 8);
+    return static_cast<ulonglong_t>(
+        (rounded_double.zextOrTrunc(sizeof(ulonglong_t) * 8)).getZExtValue());
+  }
+  default:
+    return GetAsUnsigned<unsigned long long>(fail_value);
+  }
 }
 
 llvm::APInt Scalar::SInt128(const llvm::APInt &fail_value) const {


        


More information about the lldb-commits mailing list