[Lldb-commits] [PATCH] D67792: bugfix: llvm_private::File::GetStream() can fail if m_options == 0

Lawrence D'Anna via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Sep 19 21:24:20 PDT 2019


lawrence_danna created this revision.
lawrence_danna added reviewers: JDevlieghere, jasonmolenda.
Herald added a project: LLDB.

It's not clear from the header a File created with a descriptor will be
not be usable by LLDB unless SetOptions is also called, but it is.

This is because some parts of  LLDB relies on GetStream() to use the
file, and that in turn relies on calling fdopen on the descriptor.  When
calling fdopen, GetStream relies on m_options to determine the access
mode.   If m_options has never been set, GetStream() will fail.

Instead of giving up if m_options==0, GetStream() will now call fcntl to
get the access flags.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D67792

Files:
  lldb/source/Host/common/File.cpp


Index: lldb/source/Host/common/File.cpp
===================================================================
--- lldb/source/Host/common/File.cpp
+++ lldb/source/Host/common/File.cpp
@@ -103,7 +103,22 @@
 FILE *File::GetStream() {
   if (!StreamIsValid()) {
     if (DescriptorIsValid()) {
-      const char *mode = GetStreamOpenModeFromOptions(m_options);
+      const char *mode = nullptr;
+      if (m_options) {
+        mode = GetStreamOpenModeFromOptions(m_options);
+      } else {
+       int fdflags = ::fcntl(m_descriptor, F_GETFL);
+       if (fdflags >= 0) {
+          if (fdflags & O_RDWR) {
+            mode = "r+";
+          } else if (fdflags & O_WRONLY) {
+            mode = "w";
+          } else {
+            mode = "r";
+          }
+        }
+      }
+
       if (mode) {
         if (!m_should_close_fd) {
 // We must duplicate the file descriptor if we don't own it because when you


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67792.220942.patch
Type: text/x-patch
Size: 912 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20190920/d5dc63bc/attachment.bin>


More information about the lldb-commits mailing list