@@ -72,16 +72,35 @@ void __stdcall jitStartup(ICorJitHost* jitHost)
72
72
#else
73
73
if (jitstdout == nullptr )
74
74
{
75
- int jitstdoutFd = _dup (_fileno (procstdout ()));
76
- _setmode (jitstdoutFd, _O_TEXT);
77
- jitstdout = _fdopen (jitstdoutFd, " w" );
78
- assert (jitstdout != nullptr );
79
-
80
- // Prevent the FILE* from buffering its output in order to avoid calls to
81
- // `fflush()` throughout the code.
82
- setvbuf (jitstdout, nullptr , _IONBF, 0 );
75
+ int stdoutFd = _fileno (procstdout ());
76
+ // Check fileno error output(s) -1 may overlap with errno result
77
+ // but is included for completness.
78
+ // We want to detect the case where the initial handle is null
79
+ // or bogus and avoid making further calls.
80
+ if ((stdoutFd != -1 ) && (stdoutFd != -2 ) && (errno != EINVAL))
81
+ {
82
+ int jitstdoutFd = _dup (_fileno (procstdout ()));
83
+ // Check the error status returned by dup.
84
+ if (jitstdoutFd != -1 )
85
+ {
86
+ _setmode (jitstdoutFd, _O_TEXT);
87
+ jitstdout = _fdopen (jitstdoutFd, " w" );
88
+ assert (jitstdout != nullptr );
89
+
90
+ // Prevent the FILE* from buffering its output in order to avoid calls to
91
+ // `fflush()` throughout the code.
92
+ setvbuf (jitstdout, nullptr , _IONBF, 0 );
93
+ }
94
+ }
83
95
}
84
- #endif
96
+
97
+ // If jitstdout is still null, fallback to whatever procstdout() was
98
+ // initially set to.
99
+ if (jitstdout == nullptr )
100
+ {
101
+ jitstdout = procstdout ();
102
+ }
103
+ #endif // PLATFORM_UNIX
85
104
86
105
#ifdef FEATURE_TRACELOGGING
87
106
JitTelemetry::NotifyDllProcessAttach ();
0 commit comments