From 8f87412b097475e203ad9aa5289f35905c4a8327 Mon Sep 17 00:00:00 2001
From: Dmitry Timoshkov <dmitry@baikal.ru>
Date: Tue, 10 Feb 2015 06:36:52 +0100
Subject: ntdll: Avoid race-conditions with write watches in NtReadFile.

Rebased against kernel32-NamedPipe patchset by Sebastian Lackner <sebastian@fds-team.de>.
---
 dlls/kernel32/tests/virtual.c | 8 ++++----
 dlls/ntdll/file.c             | 5 +++++
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/dlls/kernel32/tests/virtual.c b/dlls/kernel32/tests/virtual.c
index e80ba38..29740b5 100644
--- a/dlls/kernel32/tests/virtual.c
+++ b/dlls/kernel32/tests/virtual.c
@@ -1637,16 +1637,16 @@ static void test_write_watch(void)
 
     num_bytes = 0;
     success = GetOverlappedResult( readpipe, &overlapped, &num_bytes, TRUE );
-    todo_wine ok( success, "GetOverlappedResult failed %u\n", GetLastError() );
-    todo_wine ok( num_bytes == sizeof(testdata), "wrong number of bytes read\n" );
-    todo_wine ok( !memcmp( base, testdata, sizeof(testdata)), "didn't receive expected data\n" );
+    ok( success, "GetOverlappedResult failed %u\n", GetLastError() );
+    ok( num_bytes == sizeof(testdata), "wrong number of bytes read\n" );
+    ok( !memcmp( base, testdata, sizeof(testdata)), "didn't receive expected data\n" );
 
     count = 64;
     memset( results, 0, sizeof(results) );
     ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
     todo_wine ok( count == 1, "wrong count %lu\n", count );
-    todo_wine ok( results[0] == base, "wrong result %p\n", results[0] );
+    ok( results[0] == base, "wrong result %p\n", results[0] );
 
     CloseHandle( readpipe );
     CloseHandle( writepipe );
diff --git a/dlls/ntdll/file.c b/dlls/ntdll/file.c
index 15500b9..1f35a55 100644
--- a/dlls/ntdll/file.c
+++ b/dlls/ntdll/file.c
@@ -627,6 +627,8 @@ static NTSTATUS read_unix_fd(int fd, char *buf, ULONG *total, ULONG length,
             else
                 return STATUS_PENDING;
         }
+        else if (errno == EFAULT && wine_uninterrupted_write_memory( buf + *total, NULL, length - *total ) >= (length - *total))
+            continue;
         else if (errno != EINTR)
             return FILE_GetNtStatus();
     }
@@ -968,6 +970,9 @@ NTSTATUS WINAPI NtReadFile(HANDLE hFile, HANDLE hEvent,
             /* async I/O doesn't make sense on regular files */
             while ((result = pread( unix_handle, buffer, length, offset->QuadPart )) == -1)
             {
+                if (errno == EFAULT && virtual_check_buffer_for_write( buffer, length ))
+                    continue;
+
                 if (errno != EINTR)
                 {
                     status = FILE_GetNtStatus();
-- 
2.6.1

