Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 7f106c5

Browse files
committed
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.
1 parent 6fd8cd3 commit 7f106c5

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

src/jit/ee_il_dll.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,35 @@ void __stdcall jitStartup(ICorJitHost* jitHost)
7272
#else
7373
if (jitstdout == nullptr)
7474
{
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+
}
8395
}
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
85104

86105
#ifdef FEATURE_TRACELOGGING
87106
JitTelemetry::NotifyDllProcessAttach();

0 commit comments

Comments
 (0)