Windows: Fixed `os::abort()` to use `WriteFile()`.

Review: https://reviews.apache.org/r/66442


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/b5f8769f
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/b5f8769f
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/b5f8769f

Branch: refs/heads/master
Commit: b5f8769f3cf0a97b60ee2477f3830aaf688d850b
Parents: 824b76f
Author: Andrew Schwartzmeyer <[email protected]>
Authored: Tue Apr 3 12:02:24 2018 -0700
Committer: Andrew Schwartzmeyer <[email protected]>
Committed: Tue May 1 18:36:04 2018 -0700

----------------------------------------------------------------------
 3rdparty/stout/include/stout/abort.hpp | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/b5f8769f/3rdparty/stout/include/stout/abort.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/abort.hpp 
b/3rdparty/stout/include/stout/abort.hpp
index 4fd233d..43ed5ce 100644
--- a/3rdparty/stout/include/stout/abort.hpp
+++ b/3rdparty/stout/include/stout/abort.hpp
@@ -19,7 +19,7 @@
 #include <string.h>
 
 #ifdef __WINDOWS__
-#include <stout/windows.hpp>
+#include <stout/windows.hpp> // For `windows.h`.
 #else
 #include <unistd.h>
 #endif // __WINDOWS__
@@ -42,17 +42,9 @@
 
 inline NORETURN void _Abort(const char* prefix, const char* message)
 {
-  // NOTE: On Windows, `_write` takes an `unsigned int`, not `size_t`. We
-  // preform an explicit type conversion here to silence the warning. `strlen`
-  // always returns a positive result, which means it is safe to cast it to an
-  // unsigned value.
 #ifndef __WINDOWS__
   const size_t prefix_len = strlen(prefix);
   const size_t message_len = strlen(message);
-#else
-  const unsigned int prefix_len = static_cast<unsigned int>(strlen(prefix));
-  const unsigned int message_len = static_cast<unsigned int>(strlen(message));
-#endif // !__WINDOWS__
 
   // Write the failure message in an async-signal safe manner,
   // assuming strlen is async-signal safe or optimized out.
@@ -73,6 +65,24 @@ inline NORETURN void _Abort(const char* prefix, const char* 
message)
   // Windows CRT headers.
   while (::write(STDERR_FILENO, "\n", static_cast<size_t>(1)) == -1 &&
          errno == EINTR);
+#else
+  // NOTE: On Windows, `WriteFile` takes an `DWORD`, not `size_t`. We
+  // perform an explicit type conversion here to silence the warning.
+  // `strlen` always returns a positive result, which means it is safe
+  // to cast it to an unsigned value.
+  const DWORD prefix_len = static_cast<DWORD>(strlen(prefix));
+  const DWORD message_len = static_cast<DWORD>(strlen(message));
+
+  const HANDLE fd = ::GetStdHandle(STD_ERROR_HANDLE);
+
+  // NOTE: There is really nothing to do if these fail during an
+  // abort, so we don't check for errors, or care about `bytes`.
+  DWORD bytes;
+  ::WriteFile(fd, prefix, prefix_len, &bytes, nullptr);
+  ::WriteFile(fd, message, message_len, &bytes, nullptr);
+  ::WriteFile(fd, "\n", 1, &bytes, nullptr);
+#endif // __WINDOWS__
+
   abort();
 }
 

Reply via email to