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.



git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@162921 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Host/common/File.cpp b/source/Host/common/File.cpp
index a84266f..ca0c223 100644
--- a/source/Host/common/File.cpp
+++ b/source/Host/common/File.cpp
@@ -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())
                     {
diff --git a/source/Host/common/FileSpec.cpp b/source/Host/common/FileSpec.cpp
index 0faa274..d104bd2 100644
--- a/source/Host/common/FileSpec.cpp
+++ b/source/Host/common/FileSpec.cpp
@@ -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
     {
diff --git a/source/Interpreter/OptionValueFileSpec.cpp b/source/Interpreter/OptionValueFileSpec.cpp
index 3a1f26d..0360875 100644
--- a/source/Interpreter/OptionValueFileSpec.cpp
+++ b/source/Interpreter/OptionValueFileSpec.cpp
@@ -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;
 }
 
diff --git a/source/Target/Target.cpp b/source/Target/Target.cpp
index 2298a1f..23c8678 100644
--- a/source/Target/Target.cpp
+++ b/source/Target/Target.cpp
@@ -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();
     }