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:
parent
e24aaa0b58
commit
3c54ea3eeb
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user