[Lldb-commits] [lldb] r162921 - in /lldb/trunk: include/lldb/Host/File.h include/lldb/Host/FileSpec.h include/lldb/Interpreter/OptionValueFileSpec.h source/Host/common/File.cpp source/Host/common/FileSpec.cpp source/Interpreter/OptionValueFileSpec.cpp source/Target/Target.cpp

Greg Clayton gclayton at apple.com
Thu Aug 30 11:15:10 PDT 2012


Author: gclayton
Date: Thu Aug 30 13:15:10 2012
New Revision: 162921

URL: http://llvm.org/viewvc/llvm-project?rev=162921&view=rev
Log:
OptionValueFileSpec had an accessor to read the contents of the file and return the data. This can end up being used to get the string contents of a text file and could end up not being NULL terminated. I added accessors to get the file contents raw, or with a null terminator. Added the needed calls to make this happen in the FileSpec and File classes.


Modified:
    lldb/trunk/include/lldb/Host/File.h
    lldb/trunk/include/lldb/Host/FileSpec.h
    lldb/trunk/include/lldb/Interpreter/OptionValueFileSpec.h
    lldb/trunk/source/Host/common/File.cpp
    lldb/trunk/source/Host/common/FileSpec.cpp
    lldb/trunk/source/Interpreter/OptionValueFileSpec.cpp
    lldb/trunk/source/Target/Target.cpp

Modified: lldb/trunk/include/lldb/Host/File.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/File.h?rev=162921&r1=162920&r2=162921&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/File.h (original)
+++ lldb/trunk/include/lldb/Host/File.h Thu Aug 30 13:15:10 2012
@@ -378,6 +378,10 @@
     ///     bytes. This offset gets incremented by the number of bytes
     ///     that were read.
     ///
+    /// @param[in] null_terminate
+    ///     Ensure that the data that is read is terminated with a NULL
+    ///     character so that the data can be used as a C string.
+    ///
     /// @param[out] data_buffer_sp
     ///     A data buffer to create and fill in that will contain any
     ///     data that is read from the file. This buffer will be reset
@@ -388,7 +392,10 @@
     ///     failure.
     //------------------------------------------------------------------
     Error
-    Read (size_t &num_bytes, off_t &offset, lldb::DataBufferSP &data_buffer_sp);
+    Read (size_t &num_bytes,
+          off_t &offset,
+          bool null_terminate,
+          lldb::DataBufferSP &data_buffer_sp);
 
     //------------------------------------------------------------------
     /// Write bytes to a file at the specified file offset.

Modified: lldb/trunk/include/lldb/Host/FileSpec.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/FileSpec.h?rev=162921&r1=162920&r2=162921&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/FileSpec.h (original)
+++ lldb/trunk/include/lldb/Host/FileSpec.h Thu Aug 30 13:15:10 2012
@@ -479,6 +479,21 @@
     size_t
     ReadFileContents (off_t file_offset, void *dst, size_t dst_len, Error *error_ptr) const;
 
+    
+    //------------------------------------------------------------------
+    /// Read the entire contents of a file as data that can be used
+    /// as a C string.
+    ///
+    /// Read the entire contents of a file and ensure that the data
+    /// is NULL terminated so it can be used as a C string.
+    ///
+    /// @return
+    ///     A shared pointer to the data. This shared pointer can
+    ///     contain a NULL DataBuffer pointer, so the contained pointer
+    ///     must be checked prior to using it.
+    //------------------------------------------------------------------
+    lldb::DataBufferSP
+    ReadFileContentsAsCString(Error *error_ptr = NULL);
     //------------------------------------------------------------------
     /// Change the file specificed with a new path.
     ///

Modified: lldb/trunk/include/lldb/Interpreter/OptionValueFileSpec.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/OptionValueFileSpec.h?rev=162921&r1=162920&r2=162921&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/OptionValueFileSpec.h (original)
+++ lldb/trunk/include/lldb/Interpreter/OptionValueFileSpec.h Thu Aug 30 13:15:10 2012
@@ -127,7 +127,7 @@
     }
     
     const lldb::DataBufferSP &
-    GetFileContents();
+    GetFileContents(bool null_terminate);
     
 protected:
     FileSpec m_current_value;

Modified: lldb/trunk/source/Host/common/File.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/File.cpp?rev=162921&r1=162920&r2=162921&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/File.cpp (original)
+++ lldb/trunk/source/Host/common/File.cpp Thu Aug 30 13:15:10 2012
@@ -538,7 +538,7 @@
 }
 
 Error
-File::Read (size_t &num_bytes, off_t &offset, DataBufferSP &data_buffer_sp)
+File::Read (size_t &num_bytes, off_t &offset, bool null_terminate, DataBufferSP &data_buffer_sp)
 {
     Error error;
     
@@ -557,7 +557,7 @@
                         num_bytes = bytes_left;
                         
                     std::auto_ptr<DataBufferHeap> data_heap_ap;
-                    data_heap_ap.reset(new DataBufferHeap(num_bytes, '\0'));
+                    data_heap_ap.reset(new DataBufferHeap(num_bytes + (null_terminate ? 1 : 0), '\0'));
                         
                     if (data_heap_ap.get())
                     {

Modified: lldb/trunk/source/Host/common/FileSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/FileSpec.cpp?rev=162921&r1=162920&r2=162921&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/FileSpec.cpp (original)
+++ lldb/trunk/source/Host/common/FileSpec.cpp Thu Aug 30 13:15:10 2012
@@ -812,7 +812,37 @@
         File file;
         error = file.Open(resolved_path, File::eOpenOptionRead);
         if (error.Success())
-            error = file.Read (file_size, file_offset, data_sp);
+        {
+            const bool null_terminate = false;
+            error = file.Read (file_size, file_offset, null_terminate, data_sp);
+        }
+    }
+    else
+    {
+        error.SetErrorString("invalid file specification");
+    }
+    if (error_ptr)
+        *error_ptr = error;
+    return data_sp;
+}
+
+DataBufferSP
+FileSpec::ReadFileContentsAsCString(Error *error_ptr)
+{
+    Error error;
+    DataBufferSP data_sp;
+    char resolved_path[PATH_MAX];
+    if (GetPath(resolved_path, sizeof(resolved_path)))
+    {
+        File file;
+        error = file.Open(resolved_path, File::eOpenOptionRead);
+        if (error.Success())
+        {
+            off_t offset = 0;
+            size_t length = SIZE_MAX;
+            const bool null_terminate = true;
+            error = file.Read (length, offset, null_terminate, data_sp);
+        }
     }
     else
     {

Modified: lldb/trunk/source/Interpreter/OptionValueFileSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/OptionValueFileSpec.cpp?rev=162921&r1=162920&r2=162921&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/OptionValueFileSpec.cpp (original)
+++ lldb/trunk/source/Interpreter/OptionValueFileSpec.cpp Thu Aug 30 13:15:10 2012
@@ -114,10 +114,15 @@
 
 
 const lldb::DataBufferSP &
-OptionValueFileSpec::GetFileContents()
+OptionValueFileSpec::GetFileContents(bool null_terminate)
 {
     if (!m_data_sp && m_current_value)
-        m_data_sp = m_current_value.ReadFileContents();
+    {
+        if (null_terminate)
+            m_data_sp = m_current_value.ReadFileContentsAsCString();
+        else
+            m_data_sp = m_current_value.ReadFileContents();
+    }
     return m_data_sp;
 }
 

Modified: lldb/trunk/source/Target/Target.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Target.cpp?rev=162921&r1=162920&r2=162921&view=diff
==============================================================================
--- lldb/trunk/source/Target/Target.cpp (original)
+++ lldb/trunk/source/Target/Target.cpp Thu Aug 30 13:15:10 2012
@@ -2474,7 +2474,8 @@
     OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
     if (file)
     {
-        DataBufferSP data_sp(file->GetFileContents());
+        const bool null_terminate = true;
+        DataBufferSP data_sp(file->GetFileContents(null_terminate));
         if (data_sp)
             return (const char *) data_sp->GetBytes();
     }





More information about the lldb-commits mailing list