Index: llvm/lib/Support/raw_ostream.cpp =================================================================== --- llvm/lib/Support/raw_ostream.cpp +++ llvm/lib/Support/raw_ostream.cpp @@ -551,6 +551,15 @@ if (FD >= 0) { flush(); if (ShouldClose) { +#if defined(LLVM_ON_WIN32) + // On Windows, we have observed that programs that read a file after it + // was written will sometimes read bytes with value 0, when that isn't + // actually the value that was written. Calling FlushFileBuffers before + // closing the file seems to avoid the issue. + HANDLE FileHandle = reinterpret_cast(_get_osfhandle(FD)); + if (FileHandle != INVALID_HANDLE_VALUE) + ::FlushFileBuffers(FileHandle); +#endif if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD)) error_detected(EC); }