Skip to content

Commit 2f88c07

Browse files
committed
[lldb] StructuredData should not truncate uint64_t values
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. rdar://105556974 Differential Revision: https://reviews.llvm.org/D144238
1 parent 8caa8d9 commit 2f88c07

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

lldb/source/Utility/StructuredData.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ static StructuredData::ObjectSP ParseJSONValue(json::Value &value) {
6868
if (auto b = value.getAsBoolean())
6969
return std::make_shared<StructuredData::Boolean>(*b);
7070

71+
if (auto u = value.getAsUINT64())
72+
return std::make_shared<StructuredData::Integer>(*u);
73+
7174
if (auto i = value.getAsInteger())
7275
return std::make_shared<StructuredData::Integer>(*i);
7376

lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from lldbsuite.test.lldbtest import *
1010
from lldbsuite.test import lldbutil
1111

12+
import json
1213

1314
class TestStructuredDataAPI(TestBase):
1415
NO_DEBUG_INFO_TESTCASE = True
@@ -19,8 +20,15 @@ def test(self):
1920
def structured_data_api_test(self):
2021
error = lldb.SBError()
2122
s = lldb.SBStream()
22-
s.Print(
23-
"{\"key_dict\":{\"key_string\":\"STRING\",\"key_int\":3,\"key_float\":2.99,\"key_bool\":true,\"key_array\":[\"23\",\"arr\"]}}")
23+
24+
dict_str = json.dumps(
25+
{"key_dict":
26+
{"key_string":"STRING",
27+
"key_uint":0xffffffff00000000,
28+
"key_float":2.99,
29+
"key_bool":True,
30+
"key_array":["23","arr"]}})
31+
s.Print(dict_str)
2432
example = lldb.SBStructuredData()
2533

2634
# Check SetFromJSON API for dictionaries, integers, floating point
@@ -49,7 +57,7 @@ def structured_data_api_test(self):
4957
self.string_struct_test(dict_struct)
5058

5159
# Tests for integer data type
52-
self.int_struct_test(dict_struct)
60+
self.uint_struct_test(dict_struct)
5361

5462
# Tests for floating point data type
5563
self.double_struct_test(dict_struct)
@@ -110,25 +118,27 @@ def string_struct_test(self, dict_struct):
110118
str(output) +
111119
" returned for a string object")
112120

113-
def int_struct_test(self, dict_struct):
114-
# Check a valid SBStructuredData containing an 'integer' by
115-
int_struct = lldb.SBStructuredData()
116-
int_struct = dict_struct.GetValueForKey("key_int")
117-
if not int_struct.IsValid():
121+
def uint_struct_test(self, dict_struct):
122+
# Check a valid SBStructuredData containing an unsigned integer.
123+
# We intentionally make this larger than what an int64_t can hold but
124+
# still small enough to fit a uint64_t
125+
uint_struct = lldb.SBStructuredData()
126+
uint_struct = dict_struct.GetValueForKey("key_uint")
127+
if not uint_struct.IsValid():
118128
self.fail("A valid object should have been returned")
119129

120130
# Check Type API
121-
if not int_struct.GetType() == lldb.eStructuredDataTypeInteger:
122-
self.fail("Wrong type returned: " + str(int_struct.GetType()))
131+
if not uint_struct.GetType() == lldb.eStructuredDataTypeInteger:
132+
self.fail("Wrong type returned: " + str(uint_struct.GetType()))
123133

124134
# Check API returning 'integer' value
125-
output = int_struct.GetIntegerValue()
126-
if not output == 3:
135+
output = uint_struct.GetIntegerValue()
136+
if not output == 0xffffffff00000000:
127137
self.fail("wrong output: " + str(output))
128138

129139
# Calling wrong API on a SBStructuredData
130140
# (e.g. getting a string value from an integer type structure)
131-
output = int_struct.GetStringValue(25)
141+
output = uint_struct.GetStringValue(25)
132142
if output:
133143
self.fail(
134144
"Valid string " +

0 commit comments

Comments
 (0)