From 3c54ea3eebff170a241be773c1051cf244519cd0 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Tue, 29 Sep 2020 04:03:37 -0700 Subject: [PATCH] UI/updater: Fix race condition Fixes a race condition where a hash value could be overwritten from different threads, causing corruption --- UI/win-update/updater/hash.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/UI/win-update/updater/hash.cpp b/UI/win-update/updater/hash.cpp index a081b2a9c..baa2ba58c 100644 --- a/UI/win-update/updater/hash.cpp +++ b/UI/win-update/updater/hash.cpp @@ -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 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; } -- GitLab