[Lldb-commits] [PATCH] DataExtractor: Add fixed length field string fn

Ed Maste emaste at freebsd.org
Fri Jul 19 08:31:27 PDT 2013


---
I'm looking to improve the ELF note parsing in the new ELF coredump code,
and need to extract strings from fixed-length fields.

I see that the original comment for GetCStr in DataExtractor.cpp makes
reference to a length parameter, but the function currently doesn't take
one.  It looks like this change was made prior to the first public release
of this code.

For my use case it's convenient to be able to specify a zero-length field.
I could handle that in the new method's caller instead, and implement the
functionality originally described in the comment, if there's a potential
future use for that interface.

 include/lldb/Core/DataExtractor.h | 25 +++++++++++++++++++++++++
 source/Core/DataExtractor.cpp     | 39 +++++++++++++++++++++++++++++++--------
 2 files changed, 56 insertions(+), 8 deletions(-)

diff --git a/include/lldb/Core/DataExtractor.h b/include/lldb/Core/DataExtractor.h
index 8732047..38986e7 100644
--- a/include/lldb/Core/DataExtractor.h
+++ b/include/lldb/Core/DataExtractor.h
@@ -411,6 +411,31 @@ public:
     GetCStr (lldb::offset_t *offset_ptr) const;
 
     //------------------------------------------------------------------
+    /// Extract a C string from \a *offset_ptr with field size \a len.
+    ///
+    /// Returns a pointer to a C String from the data at the offset
+    /// pointed to by \a offset_ptr, with a field length of \a len.
+    /// A NULL terminated C string will be extracted and the \a offset_ptr
+    /// will be updated with the offset of the byte that follows the fixed
+    /// length field.
+    ///
+    /// @param[in,out] offset_ptr
+    ///     A pointer to an offset within the data that will be advanced
+    ///     by the appropriate number of bytes if the value is extracted
+    ///     correctly. If the offset is out of bounds or there are not
+    ///     enough bytes to extract this value, the offset will be left
+    ///     unmodified.
+    ///
+    /// @return
+    ///     A pointer to the C string value in the data. If the offset
+    ///     pointed to by \a offset_ptr is out of bounds, or if the
+    ///     offset plus the length of the field is out of bounds, or if
+    ///     the field does not contain a NULL terminator byte, NULL will
+    ///     be returned.
+    const char *
+    GetCStr (lldb::offset_t *offset_ptr, lldb::offset_t len) const;
+
+    //------------------------------------------------------------------
     /// Extract \a length bytes from \a *offset_ptr.
     ///
     /// Returns a pointer to a bytes in this object's data at the offset
diff --git a/source/Core/DataExtractor.cpp b/source/Core/DataExtractor.cpp
index 2b9dbe3..f9ffc90 100644
--- a/source/Core/DataExtractor.cpp
+++ b/source/Core/DataExtractor.cpp
@@ -1069,14 +1069,10 @@ DataExtractor::CopyByteOrderedData (offset_t src_offset,
 
 
 //----------------------------------------------------------------------
-// Extracts a AsCString (fixed length, or variable length) from
-// the data at the offset pointed to by "offset_ptr". If
-// "length" is zero, then a variable length NULL terminated C
-// string will be extracted from the data the "offset_ptr" will be
-// updated with the offset of the byte that follows the NULL
-// terminator byte. If "length" is greater than zero, then
-// the function will make sure there are "length" bytes
-// available in the current data and if so, return a valid pointer.
+// Extracts a variable length NULL terminated C string from
+// the data at the offset pointed to by "offset_ptr".  The
+// "offset_ptr" will be updated with the offset of the byte that
+// follows the NULL terminator byte.
 //
 // If the offset pointed to by "offset_ptr" is out of bounds, or if
 // "length" is non-zero and there aren't enough avaialable
@@ -1111,6 +1107,33 @@ DataExtractor::GetCStr (offset_t *offset_ptr) const
     return NULL;
 }
 
+//----------------------------------------------------------------------
+// Extracts a NULL terminated C string from the fixed length field of
+// length "len" at the offset pointed to by "offset_ptr".
+// The "offset_ptr" will be updated with the offset of the byte that
+// follows the fixed length field.
+//
+// If the offset pointed to by "offset_ptr" is out of bounds, or if
+// the offset plus the length of the field is out of bounds, or if the
+// field does not contain a NULL terminator byte, NULL will be returned
+// and "offset_ptr" will not be updated.
+//----------------------------------------------------------------------
+const char*
+DataExtractor::GetCStr (offset_t *offset_ptr, offset_t len) const
+{
+    const char *cstr = (const char *)PeekData (*offset_ptr, len);
+    if (cstr)
+    {
+        if (memchr (cstr, '\0', len) == NULL)
+        {
+            return NULL;
+        }
+        *offset_ptr += len;
+        return cstr;
+    }
+    return NULL;
+}
+
 //------------------------------------------------------------------
 // Peeks at a string in the contained data. No verification is done
 // to make sure the entire string lies within the bounds of this
-- 
1.7.11.5




More information about the lldb-commits mailing list