summaryrefslogtreecommitdiff
path: root/contrib/llvm-project/lldb/source/Utility/DataExtractor.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2020-01-23 21:36:25 +0000
committerDimitry Andric <dim@FreeBSD.org>2020-01-23 21:36:25 +0000
commit9dba64be9536c28e4800e06512b7f29b43ade345 (patch)
tree7f0f30947225ecb30ab0fdae8059a936537b0dfe /contrib/llvm-project/lldb/source/Utility/DataExtractor.cpp
parent85868e8a1daeaae7a0e48effb2ea2310ae3b02c6 (diff)
parentead246455adf1a215ec2715dad6533073a6beb4e (diff)
Notes
Diffstat (limited to 'contrib/llvm-project/lldb/source/Utility/DataExtractor.cpp')
-rw-r--r--contrib/llvm-project/lldb/source/Utility/DataExtractor.cpp37
1 files changed, 18 insertions, 19 deletions
diff --git a/contrib/llvm-project/lldb/source/Utility/DataExtractor.cpp b/contrib/llvm-project/lldb/source/Utility/DataExtractor.cpp
index 79a1f75d737c..f642a8fc7639 100644
--- a/contrib/llvm-project/lldb/source/Utility/DataExtractor.cpp
+++ b/contrib/llvm-project/lldb/source/Utility/DataExtractor.cpp
@@ -816,26 +816,25 @@ DataExtractor::CopyByteOrderedData(offset_t src_offset, offset_t src_len,
// non-zero and there aren't enough available bytes, nullptr will be returned
// and "offset_ptr" will not be updated.
const char *DataExtractor::GetCStr(offset_t *offset_ptr) const {
- const char *cstr = reinterpret_cast<const char *>(PeekData(*offset_ptr, 1));
- if (cstr) {
- const char *cstr_end = cstr;
- const char *end = reinterpret_cast<const char *>(m_end);
- while (cstr_end < end && *cstr_end)
- ++cstr_end;
-
- // Now we are either at the end of the data or we point to the
- // NULL C string terminator with cstr_end...
- if (*cstr_end == '\0') {
- // Advance the offset with one extra byte for the NULL terminator
- *offset_ptr += (cstr_end - cstr + 1);
- return cstr;
- }
+ const char *start = reinterpret_cast<const char *>(PeekData(*offset_ptr, 1));
+ // Already at the end of the data.
+ if (!start)
+ return nullptr;
- // We reached the end of the data without finding a NULL C string
- // terminator. Fall through and return nullptr otherwise anyone that would
- // have used the result as a C string can wander into unknown memory...
- }
- return nullptr;
+ const char *end = reinterpret_cast<const char *>(m_end);
+
+ // Check all bytes for a null terminator that terminates a C string.
+ const char *terminator_or_end = std::find(start, end, '\0');
+
+ // We didn't find a null terminator, so return nullptr to indicate that there
+ // is no valid C string at that offset.
+ if (terminator_or_end == end)
+ return nullptr;
+
+ // Update offset_ptr for the caller to point to the data behind the
+ // terminator (which is 1 byte long).
+ *offset_ptr += (terminator_or_end - start + 1UL);
+ return start;
}
// Extracts a NULL terminated C string from the fixed length field of length