Fix potential null pointer dereference
authorJan Kotas <jkotas@microsoft.com>
Tue, 19 May 2015 07:55:02 +0000 (00:55 -0700)
committerJan Kotas <jkotas@microsoft.com>
Tue, 19 May 2015 07:55:02 +0000 (00:55 -0700)
Calling WriteFile with both lpOverlapped and lpNumberOfBytesWritten set to null is invalid combination on Windows 7

[tfs-changeset: 1472978]

src/zap/zapimage.cpp
src/zap/zapwriter.cpp

index a7723c7..09e2b4d 100644 (file)
@@ -914,6 +914,10 @@ public:
 
         m_hasher.HashMore(pv, cb);
 
+        // We are calling with lpOverlapped == NULL so pcbWritten has to be present
+        // to prevent crashes in Win7 and below.
+        _ASSERTE(pcbWritten);
+
         if (!::WriteFile(m_hFile, pv, cb, pcbWritten, NULL))
         {
             hr = HRESULT_FROM_GetLastError();
index 2cd784b..357aebd 100644 (file)
@@ -388,8 +388,11 @@ void ZapWriter::WritePad(DWORD dwSize, BYTE fill)
 
     while (dwSize >= WRITE_BUFFER_SIZE)
     {
+        ULONG cbWritten;
         cbAvailable = min(WRITE_BUFFER_SIZE, dwSize);
-        IfFailThrow(m_pStream->Write(m_pBuffer, cbAvailable, NULL));
+        IfFailThrow(m_pStream->Write(m_pBuffer, cbAvailable, &cbWritten));
+        _ASSERTE(cbWritten == cbAvailable);
+
         dwSize -= cbAvailable;
     }