Skip to content

Commit ed0a141

Browse files
[LLDB] Fix type formatting empty c-strings (#68924)
The type formatter code is effectively considering empty strings as read errors, which is wrong. The fix is very simple. We should rely on the error object and stop checking the size. I also added a test.
1 parent 475e154 commit ed0a141

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

lldb/source/DataFormatters/TypeFormat.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ bool TypeFormatImpl_Format::FormatObject(ValueObject *valobj,
8181
WritableDataBufferSP buffer_sp(
8282
new DataBufferHeap(max_len + 1, 0));
8383
Address address(valobj->GetPointerValue());
84-
if (target_sp->ReadCStringFromMemory(
85-
address, (char *)buffer_sp->GetBytes(), max_len, error) &&
86-
error.Success())
84+
target_sp->ReadCStringFromMemory(
85+
address, (char *)buffer_sp->GetBytes(), max_len, error);
86+
if (error.Success())
8787
data.SetData(buffer_sp);
8888
}
8989
}

lldb/test/API/functionalities/data-formatter/builtin-formats/TestBuiltinFormats.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"""
44

55
import lldb
6+
from lldbsuite.test import lldbutil
67
from lldbsuite.test.decorators import *
78
from lldbsuite.test.lldbtest import *
8-
from lldbsuite.test import lldbutil
99

1010

1111
class TestCase(TestBase):
@@ -19,6 +19,20 @@ def getFormatted(self, format, expr):
1919
self.assertTrue(result.Succeeded(), result.GetError())
2020
return result.GetOutput()
2121

22+
@no_debug_info_test
23+
@skipIfWindows
24+
def testAllPlatforms(self):
25+
self.build()
26+
lldbutil.run_to_source_breakpoint(
27+
self, "// break here", lldb.SBFileSpec("main.cpp")
28+
)
29+
# We can dump correctly non char* c-strings with explicit formatting.
30+
self.assertIn(' = ""', self.getFormatted("c-string", "void_empty_cstring"))
31+
self.assertIn(' = ""', self.getFormatted("c-string", "empty_cstring"))
32+
33+
# TODO: Move as many asserts as possible within this function to `testAllPlatforms`.
34+
# Currently `arm` is being skipped even though many asserts would effectively
35+
# pass.
2236
@no_debug_info_test
2337
@skipIfWindows
2438
# uint128_t not available on arm.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include <cstdint>
22

33
const char cstring[15] = " \033\a\b\f\n\r\t\vaA09\0";
4+
const char *empty_cstring = "";
45

56
int main() {
67
int use = *cstring;
8+
void *void_empty_cstring = (void *)empty_cstring;
79
return use; // break here
810
}

0 commit comments

Comments
 (0)