Skip to content

Commit 85f40fc

Browse files
committed
[lldb] Print unprintable characters as unsigned
When specifying the C-string format for dumping memory, we treat unprintable characters as signed. Whether a character is signed or not is implementation defined, but all printable characters are signed. Therefore it's fair to assume that unprintable characters are unsigned. Before this patch, "\xcf\xfa\xed\xfe\f" would be printed as "\xffffffcf\xfffffffa\xffffffed\xfffffffe\f". Now we correctly print the original string. rdar://111126134 Differential revision: https://reviews.llvm.org/D153644
1 parent 60941f1 commit 85f40fc

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lldb/source/Core/DumpDataExtractor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ static void DumpCharacter(Stream &s, const char c) {
212212
s.PutChar(c);
213213
return;
214214
}
215-
s.Printf("\\x%2.2x", c);
215+
// Non-print characters can be assumed to be unsigned.
216+
s.Printf("\\x%2.2x", static_cast<unsigned char>(c));
216217
}
217218

218219
/// Dump a floating point type.

lldb/unittests/Core/DumpDataExtractorTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,16 @@ TEST(DumpDataExtractorTest, Formats) {
131131
// set of bytes to match the 10 byte format but then if the test runs on a
132132
// machine where we don't use 10 it'll break.
133133

134+
// Test printable characters.
134135
TestDump(llvm::StringRef("aardvark"), lldb::Format::eFormatCString,
135136
"\"aardvark\"");
137+
// Test unprintable characters.
138+
TestDump(llvm::StringRef("\xcf\xfa\xed\xfe\f"), lldb::Format::eFormatCString,
139+
"\"\\xcf\\xfa\\xed\\xfe\\f\"");
140+
// Test a mix of printable and unprintable characters.
141+
TestDump(llvm::StringRef("\xcf\xfa\ffoo"), lldb::Format::eFormatCString,
142+
"\"\\xcf\\xfa\\ffoo\"");
143+
136144
TestDump<uint16_t>(99, lldb::Format::eFormatDecimal, "99");
137145
// Just prints as a signed integer.
138146
TestDump(-1, lldb::Format::eFormatEnum, "-1");

0 commit comments

Comments
 (0)