summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrhadley <rhadley@microsoft.com>2016-07-25 10:40:27 -0700
committerrhadley <rhadley@microsoft.com>2016-07-26 10:28:50 -0700
commit7f106c520f7ffd353184e8c0db35e4155ac5e4ce (patch)
tree2b17f7824544f113b547f726a76ff3c50093a57b
parent6fd8cd393aabce43eefa20e6e965f0c10d65e7de (diff)
downloadcoreclr-7f106c520f7ffd353184e8c0db35e4155ac5e4ce.tar.gz
coreclr-7f106c520f7ffd353184e8c0db35e4155ac5e4ce.tar.bz2
coreclr-7f106c520f7ffd353184e8c0db35e4155ac5e4ce.zip
Harden stdout at startup
Check the results of the dup call to ensure that we're working with a valid file handle. In the case where we aren't we leave the jitstdout set to nullptr.
-rwxr-xr-xsrc/jit/ee_il_dll.cpp37
1 files changed, 28 insertions, 9 deletions
diff --git a/src/jit/ee_il_dll.cpp b/src/jit/ee_il_dll.cpp
index e4d05739b6..c726856b9b 100755
--- a/src/jit/ee_il_dll.cpp
+++ b/src/jit/ee_il_dll.cpp
@@ -72,16 +72,35 @@ void __stdcall jitStartup(ICorJitHost* jitHost)
#else
if (jitstdout == nullptr)
{
- int jitstdoutFd = _dup(_fileno(procstdout()));
- _setmode(jitstdoutFd, _O_TEXT);
- jitstdout = _fdopen(jitstdoutFd, "w");
- assert(jitstdout != nullptr);
-
- // Prevent the FILE* from buffering its output in order to avoid calls to
- // `fflush()` throughout the code.
- setvbuf(jitstdout, nullptr, _IONBF, 0);
+ int stdoutFd = _fileno(procstdout());
+ // Check fileno error output(s) -1 may overlap with errno result
+ // but is included for completness.
+ // We want to detect the case where the initial handle is null
+ // or bogus and avoid making further calls.
+ if ((stdoutFd != -1) && (stdoutFd != -2) && (errno != EINVAL))
+ {
+ int jitstdoutFd = _dup(_fileno(procstdout()));
+ // Check the error status returned by dup.
+ if (jitstdoutFd != -1)
+ {
+ _setmode(jitstdoutFd, _O_TEXT);
+ jitstdout = _fdopen(jitstdoutFd, "w");
+ assert(jitstdout != nullptr);
+
+ // Prevent the FILE* from buffering its output in order to avoid calls to
+ // `fflush()` throughout the code.
+ setvbuf(jitstdout, nullptr, _IONBF, 0);
+ }
+ }
}
-#endif
+
+ // If jitstdout is still null, fallback to whatever procstdout() was
+ // initially set to.
+ if (jitstdout == nullptr)
+ {
+ jitstdout = procstdout();
+ }
+#endif // PLATFORM_UNIX
#ifdef FEATURE_TRACELOGGING
JitTelemetry::NotifyDllProcessAttach();