From bd180cb623b7e7b97c9179319b7562e3faa32fbc Mon Sep 17 00:00:00 2001 From: Greg Clayton Date: Thu, 21 Mar 2013 00:29:45 +0000 Subject: [PATCH] Fixed the ValidOffsetForDataOfSize() to use simpler logic. Fixed DataExtractor::BytesLeft() to return the correct value. llvm-svn: 177616 --- lldb/include/lldb/Core/DataEncoder.h | 14 +++++++++++++- lldb/include/lldb/Core/DataExtractor.h | 9 ++++----- lldb/source/Core/DataEncoder.cpp | 26 -------------------------- 3 files changed, 17 insertions(+), 32 deletions(-) diff --git a/lldb/include/lldb/Core/DataEncoder.h b/lldb/include/lldb/Core/DataEncoder.h index 20da546..658cce0 100644 --- a/lldb/include/lldb/Core/DataEncoder.h +++ b/lldb/include/lldb/Core/DataEncoder.h @@ -424,7 +424,19 @@ public: /// length bytes available at that offset, \b false otherwise. //------------------------------------------------------------------ bool - ValidOffsetForDataOfSize (uint32_t offset, uint32_t length) const; + ValidOffsetForDataOfSize (uint32_t offset, uint32_t length) const + { + return length <= BytesLeft (offset); + } + + uint32_t + BytesLeft (uint32_t offset) const + { + const uint32_t size = GetByteSize(); + if (size > offset) + return size - offset; + return 0; + } protected: //------------------------------------------------------------------ diff --git a/lldb/include/lldb/Core/DataExtractor.h b/lldb/include/lldb/Core/DataExtractor.h index 57c36d8..38dfd2f 100644 --- a/lldb/include/lldb/Core/DataExtractor.h +++ b/lldb/include/lldb/Core/DataExtractor.h @@ -1234,8 +1234,7 @@ public: bool ValidOffsetForDataOfSize (lldb::offset_t offset, lldb::offset_t length) const { - lldb::offset_t bytes_left = BytesLeft (offset); - return length <= bytes_left; + return length <= BytesLeft (offset); } size_t @@ -1253,9 +1252,9 @@ protected: BytesLeft (lldb::offset_t offset) const { const lldb::offset_t size = GetByteSize(); - if (offset >= size) - return 0; - return offset - size; + if (size > offset) + return size - offset; + return 0; } //------------------------------------------------------------------ diff --git a/lldb/source/Core/DataEncoder.cpp b/lldb/source/Core/DataEncoder.cpp index 933cc3e..92a9104 100644 --- a/lldb/source/Core/DataEncoder.cpp +++ b/lldb/source/Core/DataEncoder.cpp @@ -142,32 +142,6 @@ DataEncoder::GetSharedDataOffset () const return 0; } -//------------------------------------------------------------------ -// Returns true if there are LENGTH bytes availabe starting OFFSET -// into the data that is in this object. -//------------------------------------------------------------------ -bool -DataEncoder::ValidOffsetForDataOfSize (uint32_t offset, uint32_t length) const -{ - size_t size = GetByteSize(); - if (offset >= size) - return false; // offset isn't valid - - if (length == 0) - return true; // No bytes requested at this offset, return true - - // If we flip the bits in offset we can figure out how - // many bytes we have left before "offset + length" - // could overflow when doing unsigned arithmetic. - if (length > ~offset) - return false; // unsigned overflow - - // Make sure "offset + length" is a valid offset as well. - // length must be greater than zero for this to be a - // valid expression, and we have already checked for this. - return ((offset + length) <= size); -} - //---------------------------------------------------------------------- // Set the data with which this object will extract from to data // starting at BYTES and set the length of the data to LENGTH bytes -- 2.7.4