From 3a870bffb108516bc1c09818538466db4f1a505d Mon Sep 17 00:00:00 2001 From: David Spickett Date: Wed, 1 Dec 2021 14:56:18 +0000 Subject: [PATCH] [lldb] Add missing space in C string format memory read warning Also add tests to check that we print the warning in the right circumstances. Reviewed By: labath Differential Revision: https://reviews.llvm.org/D114877 --- lldb/source/Commands/CommandObjectMemory.cpp | 2 +- lldb/test/API/commands/memory/read/Makefile | 3 + .../API/commands/memory/read/TestMemoryRead.py | 64 ++++++++++++++++++++++ lldb/test/API/commands/memory/read/main.c | 4 ++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 lldb/test/API/commands/memory/read/Makefile create mode 100644 lldb/test/API/commands/memory/read/TestMemoryRead.py create mode 100644 lldb/test/API/commands/memory/read/main.c diff --git a/lldb/source/Commands/CommandObjectMemory.cpp b/lldb/source/Commands/CommandObjectMemory.cpp index 094ce6f..9df42f3 100644 --- a/lldb/source/Commands/CommandObjectMemory.cpp +++ b/lldb/source/Commands/CommandObjectMemory.cpp @@ -716,7 +716,7 @@ protected: if (item_byte_size == read) { result.AppendWarningWithFormat( "unable to find a NULL terminated string at 0x%" PRIx64 - ".Consider increasing the maximum read length.\n", + ". Consider increasing the maximum read length.\n", data_addr); --read; break_on_no_NULL = true; diff --git a/lldb/test/API/commands/memory/read/Makefile b/lldb/test/API/commands/memory/read/Makefile new file mode 100644 index 0000000..1049594 --- /dev/null +++ b/lldb/test/API/commands/memory/read/Makefile @@ -0,0 +1,3 @@ +C_SOURCES := main.c + +include Makefile.rules diff --git a/lldb/test/API/commands/memory/read/TestMemoryRead.py b/lldb/test/API/commands/memory/read/TestMemoryRead.py new file mode 100644 index 0000000..fe735ec --- /dev/null +++ b/lldb/test/API/commands/memory/read/TestMemoryRead.py @@ -0,0 +1,64 @@ +""" +Test the 'memory read' command. +""" + +import lldb +import lldbsuite.test.lldbutil as lldbutil + +from lldbsuite.test.decorators import * +from lldbsuite.test.lldbtest import * + + +class MemoryWriteTestCase(TestBase): + + mydir = TestBase.compute_mydir(__file__) + + def setUp(self): + # Call super's setUp(). + TestBase.setUp(self) + # Find the line number to break inside main(). + self.line = line_number('main.c', '// Set break point at this line.') + + def build_run_stop(self): + self.build() + exe = self.getBuildArtifact("a.out") + self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) + + # Break in main() after the variables are assigned values. + lldbutil.run_break_set_by_file_and_line(self, + "main.c", + self.line, + num_expected_locations=1, + loc_exact=True) + + self.runCmd("run", RUN_SUCCEEDED) + + # The stop reason of the thread should be breakpoint. + self.expect("thread list", + STOPPED_DUE_TO_BREAKPOINT, + substrs=['stopped', 'stop reason = breakpoint']) + + # The breakpoint should have a hit count of 1. + lldbutil.check_breakpoint(self, bpno = 1, expected_hit_count = 1) + + @no_debug_info_test + def test_memory_read_c_string(self): + """Test that reading memory as a c string respects the size limit given + and warns if the null terminator is missing.""" + self.build_run_stop() + + # The size here is the size in memory so it includes the null terminator. + cmd = "memory read --format \"c-string\" --size {} &the_string" + + # Size matches the size of the array. + self.expect(cmd.format(5), substrs=['\"abcd\"']) + + # If size would take us past the terminator we stop at the terminator. + self.expect(cmd.format(10), substrs=['\"abcd\"']) + + # Size 3 means 2 chars and a terminator. So we print 2 chars but warn because + # the third isn't 0 as expected. + self.expect(cmd.format(3), substrs=['\"ab\"']) + self.assertRegex(self.res.GetError(), + "unable to find a NULL terminated string at 0x[0-9A-fa-f]+." + " Consider increasing the maximum read length.") diff --git a/lldb/test/API/commands/memory/read/main.c b/lldb/test/API/commands/memory/read/main.c new file mode 100644 index 0000000..9dcf34e --- /dev/null +++ b/lldb/test/API/commands/memory/read/main.c @@ -0,0 +1,4 @@ +int main() { + char the_string[] = {'a', 'b', 'c', 'd', 0}; + return 0; // Set break point at this line. +} -- 2.7.4