[Lldb-commits] [lldb] r217355 - driver: handle write error better

Saleem Abdulrasool compnerd at compnerd.org
Sun Sep 7 19:47:13 PDT 2014


Author: compnerd
Date: Sun Sep  7 21:47:13 2014
New Revision: 217355

URL: http://llvm.org/viewvc/llvm-project?rev=217355&view=rev
Log:
driver: handle write error better

We would previously simply assume that the write would always succeed.  However,
write(2) may return -1 for error as well as fail to perform a complete write (in
which case the returned number of bytes will be less than the requested bytes).

Explicitly check if an error condition is encountered.  This would previously
not be caught as we default initialized success to true.  Add an assertion that
we always perform a complete write (a continuous retry could be added to ensure
that we finish writing completely).

This was caught by GCC's signed comparison warning and manual inspection.

Modified:
    lldb/trunk/tools/driver/Driver.cpp

Modified: lldb/trunk/tools/driver/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/driver/Driver.cpp?rev=217355&r1=217354&r2=217355&view=diff
==============================================================================
--- lldb/trunk/tools/driver/Driver.cpp (original)
+++ lldb/trunk/tools/driver/Driver.cpp Sun Sep  7 21:47:13 2014
@@ -931,7 +931,15 @@ Driver::MainLoop ()
 #endif
         if (err == 0)
         {
-            if (write (fds[WRITE], commands_data, commands_size) == commands_size)
+            ssize_t nrwr = write(fds[WRITE], commands_data, commands_size);
+            if (nrwr < 0)
+            {
+                fprintf(stderr, "error: write(%i, %p, %zd) failed (errno = %i) "
+                                "when trying to open LLDB commands pipe\n",
+                        fds[WRITE], commands_data, commands_size, errno);
+                success = false;
+            }
+            else if (static_cast<size_t>(nrwr) == commands_size)
             {
                 // Close the write end of the pipe so when we give the read end to
                 // the debugger/command interpreter it will exit when it consumes all
@@ -953,10 +961,18 @@ Driver::MainLoop ()
                 }
                 else
                 {
-                    fprintf(stderr, "error: fdopen(%i, \"r\") failed (errno = %i) when trying to open LLDB commands pipe\n", fds[READ], errno);
+                    fprintf(stderr,
+                            "error: fdopen(%i, \"r\") failed (errno = %i) when "
+                            "trying to open LLDB commands pipe\n",
+                            fds[READ], errno);
                     success = false;
                 }
             }
+            else
+            {
+                assert(!"partial writes not handled");
+                success = false;
+            }
         }
         else
         {





More information about the lldb-commits mailing list