Provided a variant of ReadCStringFromMemory that supports null terminators of any character width.
This prevents unbounded reads (i.e. reads of GetMaximumSizeOfStringSummary() bytes)
from causing test failures (i.e. due to ptrace EIO or EFAULT on Linux).
Note that ReadCStringFromMemory is marked as deprecated because the loop that calls
ReadMemory does not continue until the string has been completely read.
The expected behavior is to read until until max_bytes or a null terminator.
Note: As discussed on lldb-dev, further testing will be performed with ReadStringFromMemory
before further changes are made for users of ReadCStringFromMemory.
Thanks to Enrico, Matt and Andy for their review feedback.
git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@179857 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/DataFormatters/CXXFormatterFunctions.cpp b/source/DataFormatters/CXXFormatterFunctions.cpp
index 072649d..4f3c6d8 100644
--- a/source/DataFormatters/CXXFormatterFunctions.cpp
+++ b/source/DataFormatters/CXXFormatterFunctions.cpp
@@ -264,7 +264,8 @@
if (!process_sp)
return false;
- const int origin_encoding = 8*sizeof(SourceDataType);
+ const int type_width = sizeof(SourceDataType);
+ const int origin_encoding = 8 * type_width ;
if (origin_encoding != 8 && origin_encoding != 16 && origin_encoding != 32)
return false;
// if not UTF8, I need a conversion function to return proper UTF8
@@ -276,15 +277,17 @@
else
sourceSize = std::min(sourceSize,process_sp->GetTarget().GetMaximumSizeOfStringSummary());
- const int bufferSPSize = sourceSize * (origin_encoding >> 2);
+ const int bufferSPSize = sourceSize * type_width;
- Error error;
lldb::DataBufferSP buffer_sp(new DataBufferHeap(bufferSPSize,0));
if (!buffer_sp->GetBytes())
return false;
- size_t data_read = process_sp->ReadMemoryFromInferior(location, (char*)buffer_sp->GetBytes(), bufferSPSize, error);
+ Error error;
+ char *buffer = reinterpret_cast<char *>(buffer_sp->GetBytes());
+
+ size_t data_read = process_sp->ReadStringFromMemory(location, buffer, bufferSPSize, error, type_width);
if (error.Fail() || data_read == 0)
{
stream.Printf("unable to read data");