[PATCH] D46621: [Support] call FlushFileBuffers when closing raw_fd_ostream on Windows

Bob Haarman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 8 19:27:41 PDT 2018


inglorion created this revision.
inglorion added reviewers: rnk, zturner.
Herald added subscribers: hiraditya, mehdi_amini.

We have observed a problem where files we write on Windows are
sometimes read back with bytes having value 0, when that was not the
value that was written. https://reviews.llvm.org/D42925 works around this problem for files we
write to by memory-mapping them. This change applies a similar fix for
files we write using raw_fd_ostream, as we do for archives and in the
LTO cache.


https://reviews.llvm.org/D46621

Files:
  llvm/lib/Support/raw_ostream.cpp


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<HANDLE>(_get_osfhandle(FD));
+      if (FileHandle != INVALID_HANDLE_VALUE)
+        ::FlushFileBuffers(FileHandle);
+#endif
       if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
         error_detected(EC);
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46621.145844.patch
Type: text/x-patch
Size: 847 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180509/eb34031c/attachment.bin>


More information about the llvm-commits mailing list