From: rhadley Date: Mon, 25 Jul 2016 17:40:27 +0000 (-0700) Subject: Harden stdout at startup X-Git-Tag: accepted/tizen/base/20180629.140029~3942^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7f106c520f7ffd353184e8c0db35e4155ac5e4ce;p=platform%2Fupstream%2Fcoreclr.git 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. --- diff --git a/src/jit/ee_il_dll.cpp b/src/jit/ee_il_dll.cpp index e4d0573..c726856 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();