Only check GetLastError() if ReadFile returns false (#13076)

Blue committed Jun 10, 2025 at 11:07 UTC 94ecb860678d1ec34b9517f211137f545d85fa97
1 file changed +7 -3
src/windows/common/wslutil.cpp
+7 -3
@@ -1107,13 +1107,17 @@ std::vector<BYTE> wsl::windows::common::wslutil::HashFile(HANDLE file, DWORD Alg
1107 std::vector<char> buffer(bufferSize);
1108
1109 DWORD readBytes{};
1110 - while (ReadFile(file, buffer.data(), bufferSize, &readBytes, nullptr) && readBytes > 0)
1110 + while (true)
1111 {
1112 + THROW_IF_WIN32_BOOL_FALSE(ReadFile(file, buffer.data(), bufferSize, &readBytes, nullptr));
1113 + if (readBytes == 0)
1114 + {
1115 + break;
1116 + }
1117 +
1118 THROW_IF_WIN32_BOOL_FALSE(CryptHashData(hash.get(), reinterpret_cast<const BYTE*>(buffer.data()), readBytes, 0));
1119 }
1120
1115 - THROW_IF_WIN32_ERROR(GetLastError());
1116 -
1121 std::vector<BYTE> fileHash(32);
1122 DWORD hashSize = static_cast<DWORD>(fileHash.size());
1123