提交 3c54ea3e 编写于 作者: J jp9000

UI/updater: Fix race condition

Fixes a race condition where a hash value could be overwritten from
different threads, causing corruption
上级 e24aaa0b
...@@ -45,11 +45,13 @@ void StringToHash(const wchar_t *in, BYTE *out) ...@@ -45,11 +45,13 @@ void StringToHash(const wchar_t *in, BYTE *out)
bool CalculateFileHash(const wchar_t *path, BYTE *hash) bool CalculateFileHash(const wchar_t *path, BYTE *hash)
{ {
static BYTE hashBuffer[1048576]; static __declspec(thread) vector<BYTE> hashBuffer;
blake2b_state blake2; blake2b_state blake2;
if (blake2b_init(&blake2, BLAKE2_HASH_LENGTH) != 0) if (blake2b_init(&blake2, BLAKE2_HASH_LENGTH) != 0)
return false; return false;
hashBuffer.resize(1048576);
WinHandle handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, WinHandle handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ,
nullptr, OPEN_EXISTING, 0, nullptr); nullptr, OPEN_EXISTING, 0, nullptr);
if (handle == INVALID_HANDLE_VALUE) if (handle == INVALID_HANDLE_VALUE)
...@@ -57,14 +59,14 @@ bool CalculateFileHash(const wchar_t *path, BYTE *hash) ...@@ -57,14 +59,14 @@ bool CalculateFileHash(const wchar_t *path, BYTE *hash)
for (;;) { for (;;) {
DWORD read = 0; DWORD read = 0;
if (!ReadFile(handle, hashBuffer, sizeof(hashBuffer), &read, if (!ReadFile(handle, &hashBuffer[0], hashBuffer.size(), &read,
nullptr)) nullptr))
return false; return false;
if (!read) if (!read)
break; break;
if (blake2b_update(&blake2, hashBuffer, read) != 0) if (blake2b_update(&blake2, &hashBuffer[0], read) != 0)
return false; return false;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册