Merge pull request #5104 from andir/refscan-race

Fix potential race-condition in reference scanning code
This commit is contained in:
Eelco Dolstra 2021-08-09 14:49:24 +02:00 committed by GitHub
commit 4c5ce2a345
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,6 +5,7 @@
#include <map> #include <map>
#include <cstdlib> #include <cstdlib>
#include <mutex>
namespace nix { namespace nix {
@ -16,14 +17,13 @@ static unsigned int refLength = 32; /* characters */
static void search(const unsigned char * s, size_t len, static void search(const unsigned char * s, size_t len,
StringSet & hashes, StringSet & seen) StringSet & hashes, StringSet & seen)
{ {
static bool initialised = false; static std::once_flag initialised;
static bool isBase32[256]; static bool isBase32[256];
if (!initialised) { std::call_once(initialised, [](){
for (unsigned int i = 0; i < 256; ++i) isBase32[i] = false; for (unsigned int i = 0; i < 256; ++i) isBase32[i] = false;
for (unsigned int i = 0; i < base32Chars.size(); ++i) for (unsigned int i = 0; i < base32Chars.size(); ++i)
isBase32[(unsigned char) base32Chars[i]] = true; isBase32[(unsigned char) base32Chars[i]] = true;
initialised = true; });
}
for (size_t i = 0; i + refLength <= len; ) { for (size_t i = 0; i + refLength <= len; ) {
int j; int j;