Merge bitcoin/bitcoin#26814: refactor: remove windows-only compat.h usage in random

621cfb7722 random: consolidate WIN32 #ifdefs (fanquake)
75ec6275e6 random: remove compat.h include (fanquake)
4dc12816ac random: use int for MAX_TRIES (fanquake)

Pull request description:

  This change is related to removing the use of `compat.h` as a miscellaneous catch-all for unclear/platform specific includes. Somewhat prompted by IWYU-related discussion here: https://github.com/bitcoin/bitcoin/pull/26763/files#r1058861693.

  The only reason `compat.h` is required in random.cpp for Windows (note the `#ifdef WIN32`), is for `ssize_t` and an "indirect" inclusion of `windows.h`. I say indirect, because `windows.h` isn't actually included in compat.h either, it's dragged in as a side-effect of other windows includes there, i.e `winsock2.h`.

  Remove this coupling by replacing `ssize_t` with int, just including `windows.h` and removing compat.h.

ACKs for top commit:
  hebasto:
    re-ACK 621cfb7722, rebased only since my [recent](https://github.com/bitcoin/bitcoin/pull/26814#pullrequestreview-1237312144) review. Verified with:
  john-moffett:
    ACK 621cfb7722

Tree-SHA512: 31e1ed2e7ff7daf6c3ee72e6a908def52f7addf8305ba371c5032f1927cbb8ef5d302785e8de42b5c04a123052f04688cc9fd80decceb04738b5d9153f3d32d7
This commit is contained in:
fanquake 2023-02-19 13:46:00 +00:00
commit 4395b7f084
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1

View file

@ -8,23 +8,22 @@
#include <compat/cpuid.h>
#include <crypto/sha256.h>
#include <crypto/sha512.h>
#include <support/cleanse.h>
#ifdef WIN32
#include <compat/compat.h>
#include <wincrypt.h>
#endif
#include <logging.h>
#include <randomenv.h>
#include <support/allocators/secure.h>
#include <span.h>
#include <sync.h> // for Mutex
#include <util/time.h> // for GetTimeMicros()
#include <support/allocators/secure.h>
#include <support/cleanse.h>
#include <sync.h>
#include <util/time.h>
#include <cmath>
#include <cstdlib>
#include <thread>
#ifndef WIN32
#ifdef WIN32
#include <windows.h>
#include <wincrypt.h>
#else
#include <fcntl.h>
#include <sys/time.h>
#endif
@ -634,7 +633,7 @@ bool Random_SanityCheck()
* GetOSRand() overwrites all 32 bytes of the output given a maximum
* number of tries.
*/
static const ssize_t MAX_TRIES = 1024;
static constexpr int MAX_TRIES{1024};
uint8_t data[NUM_OS_RANDOM_BYTES];
bool overwritten[NUM_OS_RANDOM_BYTES] = {}; /* Tracks which bytes have been overwritten at least once */
int num_overwritten;