Try passing _O_BINARY to the original open() call and then seeing if fdopen works.<br><br>If you really want true reliability you should use native win32 APIs for file i/o, but that’s a large rewrite, so I agree targeted fixes make more sense here<br><div class="gmail_quote"><div dir="ltr">On Mon, Oct 16, 2017 at 5:14 PM Marco Castelluccio via Phabricator <<a href="mailto:reviews@reviews.llvm.org">reviews@reviews.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">marco-c created this revision.<br>
<br>
Fixes <a href="https://bugs.llvm.org/show_bug.cgi?id=34922" rel="noreferrer" target="_blank">https://bugs.llvm.org/show_bug.cgi?id=34922</a>.<br>
<br>
When using fdopen, the GCDA file is very often not correctly written to disk. I honestly have no idea why, perhaps somebody more familiar with Windows would know.<br>
This doesn't only apply to the simple case from bug 34922. I have built Firefox to collect coverage and before this change most GCDA files were corrupted.<br>
<br>
<br>
<a href="https://reviews.llvm.org/D38984" rel="noreferrer" target="_blank">https://reviews.llvm.org/D38984</a><br>
<br>
Files:<br>
  lib/profile/GCDAProfiling.c<br>
<br>
<br>
Index: lib/profile/GCDAProfiling.c<br>
===================================================================<br>
--- lib/profile/GCDAProfiling.c<br>
+++ lib/profile/GCDAProfiling.c<br>
@@ -263,8 +263,9 @@<br>
    * same GCDA. This can fail if the filesystem doesn't support it, but in that<br>
    * case we'll just carry on with the old racy behaviour and hope for the best.<br>
    */<br>
-  flock(fd, LOCK_EX);<br>
-  output_file = fdopen(fd, mode);<br>
+  close(fd);<br>
+  output_file = fopen(filename, mode);<br>
+  flock(fileno(output_file), LOCK_EX);<br>
<br>
   /* Initialize the write buffer. */<br>
   write_buffer = NULL;<br>
@@ -462,7 +463,7 @@<br>
       unmap_file();<br>
     }<br>
<br>
-    flock(fd, LOCK_UN);<br>
+    flock(fileno(output_file), LOCK_UN);<br>
     fclose(output_file);<br>
     output_file = NULL;<br>
     write_buffer = NULL;<br>
<br>
<br>
</blockquote></div>