[Lldb-commits] [PATCH] D144238: [lldb] StructuredData should not truncate uint64_t values

Alex Langford via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Feb 16 18:26:06 PST 2023


bulbazord created this revision.
bulbazord added a reviewer: mib.
Herald added a project: All.
bulbazord requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

In json::Value, getAsInteger returns an optional<int64_t> and getAsNumber
returns an optional<double>. If a value is larger than what an int64_t
can hold but smaller than what a uint64_t can hold, the getAsInteger
function will fail but the getAsNumber will succeed. However, the value
shouldn't be interpreted as a double.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D144238

Files:
  lldb/source/Utility/StructuredData.cpp
  lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py


Index: lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
===================================================================
--- lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
+++ lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
@@ -19,8 +19,10 @@
     def structured_data_api_test(self):
         error = lldb.SBError()
         s = lldb.SBStream()
+
+        # Note: 18446744069414584320 <-> 0xFFFFFFFF00000000
         s.Print(
-            "{\"key_dict\":{\"key_string\":\"STRING\",\"key_int\":3,\"key_float\":2.99,\"key_bool\":true,\"key_array\":[\"23\",\"arr\"]}}")
+            "{\"key_dict\":{\"key_string\":\"STRING\",\"key_uint\":18446744069414584320,\"key_float\":2.99,\"key_bool\":true,\"key_array\":[\"23\",\"arr\"]}}")
         example = lldb.SBStructuredData()
 
         # Check SetFromJSON API for dictionaries, integers, floating point
@@ -49,7 +51,7 @@
         self.string_struct_test(dict_struct)
 
         # Tests for integer data type
-        self.int_struct_test(dict_struct)
+        self.uint_struct_test(dict_struct)
 
         # Tests for floating point data type
         self.double_struct_test(dict_struct)
@@ -110,25 +112,27 @@
                 str(output) +
                 " returned for a string object")
 
-    def int_struct_test(self, dict_struct):
-        # Check a valid SBStructuredData containing an 'integer' by
-        int_struct = lldb.SBStructuredData()
-        int_struct = dict_struct.GetValueForKey("key_int")
-        if not int_struct.IsValid():
+    def uint_struct_test(self, dict_struct):
+        # Check a valid SBStructuredData containing an unsigned integer.
+        # We intentionally make this larger than what an int64_t can hold but
+        # still small enough to fit a uint64_t
+        uint_struct = lldb.SBStructuredData()
+        uint_struct = dict_struct.GetValueForKey("key_uint")
+        if not uint_struct.IsValid():
             self.fail("A valid object should have been returned")
 
         # Check Type API
-        if not int_struct.GetType() == lldb.eStructuredDataTypeInteger:
-            self.fail("Wrong type returned: " + str(int_struct.GetType()))
+        if not uint_struct.GetType() == lldb.eStructuredDataTypeInteger:
+            self.fail("Wrong type returned: " + str(uint_struct.GetType()))
 
         # Check API returning 'integer' value
-        output = int_struct.GetIntegerValue()
-        if not output == 3:
+        output = uint_struct.GetIntegerValue()
+        if not output == 18446744069414584320:
             self.fail("wrong output: " + str(output))
 
         # Calling wrong API on a SBStructuredData
         # (e.g. getting a string value from an integer type structure)
-        output = int_struct.GetStringValue(25)
+        output = uint_struct.GetStringValue(25)
         if output:
             self.fail(
                 "Valid string " +
Index: lldb/source/Utility/StructuredData.cpp
===================================================================
--- lldb/source/Utility/StructuredData.cpp
+++ lldb/source/Utility/StructuredData.cpp
@@ -68,6 +68,9 @@
   if (auto b = value.getAsBoolean())
     return std::make_shared<StructuredData::Boolean>(*b);
 
+  if (auto u = value.getAsUINT64())
+    return std::make_shared<StructuredData::Integer>(*u);
+
   if (auto i = value.getAsInteger())
     return std::make_shared<StructuredData::Integer>(*i);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144238.498228.patch
Type: text/x-patch
Size: 3439 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20230217/fc12ac17/attachment.bin>


More information about the lldb-commits mailing list