Author: Jonas Devlieghere Date: 2023-06-23T11:00:21-07:00 New Revision: 85f40fc676df65416568750f328c71758c84264a
URL: https://github.com/llvm/llvm-project/commit/85f40fc676df65416568750f328c71758c84264a DIFF: https://github.com/llvm/llvm-project/commit/85f40fc676df65416568750f328c71758c84264a.diff LOG: [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 Added: Modified: lldb/source/Core/DumpDataExtractor.cpp lldb/unittests/Core/DumpDataExtractorTest.cpp Removed: ################################################################################ diff --git a/lldb/source/Core/DumpDataExtractor.cpp b/lldb/source/Core/DumpDataExtractor.cpp index 7f6108f40c59f..728297a3a5ad8 100644 --- a/lldb/source/Core/DumpDataExtractor.cpp +++ b/lldb/source/Core/DumpDataExtractor.cpp @@ -212,7 +212,8 @@ static void DumpCharacter(Stream &s, const char c) { s.PutChar(c); return; } - s.Printf("\\x%2.2x", c); + // Non-print characters can be assumed to be unsigned. + s.Printf("\\x%2.2x", static_cast<unsigned char>(c)); } /// Dump a floating point type. diff --git a/lldb/unittests/Core/DumpDataExtractorTest.cpp b/lldb/unittests/Core/DumpDataExtractorTest.cpp index 226f2b7869664..bbe5e9e5ed9e2 100644 --- a/lldb/unittests/Core/DumpDataExtractorTest.cpp +++ b/lldb/unittests/Core/DumpDataExtractorTest.cpp @@ -131,8 +131,16 @@ TEST(DumpDataExtractorTest, Formats) { // set of bytes to match the 10 byte format but then if the test runs on a // machine where we don't use 10 it'll break. + // Test printable characters. TestDump(llvm::StringRef("aardvark"), lldb::Format::eFormatCString, "\"aardvark\""); + // Test unprintable characters. + TestDump(llvm::StringRef("\xcf\xfa\xed\xfe\f"), lldb::Format::eFormatCString, + "\"\\xcf\\xfa\\xed\\xfe\\f\""); + // Test a mix of printable and unprintable characters. + TestDump(llvm::StringRef("\xcf\xfa\ffoo"), lldb::Format::eFormatCString, + "\"\\xcf\\xfa\\ffoo\""); + TestDump<uint16_t>(99, lldb::Format::eFormatDecimal, "99"); // Just prints as a signed integer. TestDump(-1, lldb::Format::eFormatEnum, "-1"); _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits