UI/updater: Fix race condition

Fixes a race condition where a hash value could be overwritten from
different threads, causing corruption
This commit is contained in:
jp9000 2020-09-29 04:03:37 -07:00
parent e24aaa0b58
commit 3c54ea3eeb

View File

@ -45,11 +45,13 @@ void StringToHash(const wchar_t *in, BYTE *out)
bool CalculateFileHash(const wchar_t *path, BYTE *hash)
{
static BYTE hashBuffer[1048576];
static __declspec(thread) vector<BYTE> hashBuffer;
blake2b_state blake2;
if (blake2b_init(&blake2, BLAKE2_HASH_LENGTH) != 0)
return false;
hashBuffer.resize(1048576);
WinHandle handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ,
nullptr, OPEN_EXISTING, 0, nullptr);
if (handle == INVALID_HANDLE_VALUE)
@ -57,14 +59,14 @@ bool CalculateFileHash(const wchar_t *path, BYTE *hash)
for (;;) {
DWORD read = 0;
if (!ReadFile(handle, hashBuffer, sizeof(hashBuffer), &read,
if (!ReadFile(handle, &hashBuffer[0], hashBuffer.size(), &read,
nullptr))
return false;
if (!read)
break;
if (blake2b_update(&blake2, hashBuffer, read) != 0)
if (blake2b_update(&blake2, &hashBuffer[0], read) != 0)
return false;
}