Skip to content

Commit 55c403b

Browse files
jnewberylaanwj
authored andcommitted
Ensure -maxsigcachesize is in valid range
- If the -maxsigcachesize parameter is set to zero, setup a minimum sized sigcache (2 elements) rather than segfaulting. - Handle maxsigcachesize being negative - Handle maxsigcachesize being too large
1 parent 476cc47 commit 55c403b

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/script/sigcache.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ static CSignatureCache signatureCache;
9393
// To be called once in AppInit2/TestingSetup to initialize the signatureCache
9494
void InitSignatureCache()
9595
{
96-
size_t nMaxCacheSize = GetArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE) * ((size_t) 1 << 20);
97-
if (nMaxCacheSize <= 0) return;
96+
// nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
97+
// setup_bytes creates the minimum possible cache (2 elements).
98+
size_t nMaxCacheSize = std::min(std::max((int64_t)0, GetArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE)), MAX_MAX_SIG_CACHE_SIZE) * ((size_t) 1 << 20);
9899
size_t nElems = signatureCache.setup_bytes(nMaxCacheSize);
99100
LogPrintf("Using %zu MiB out of %zu requested for signature cache, able to store %zu elements\n",
100101
(nElems*sizeof(uint256)) >>20, nMaxCacheSize>>20, nElems);

src/script/sigcache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// systems). Due to how we count cache size, actual memory usage is slightly
1515
// more (~32.25 MB)
1616
static const unsigned int DEFAULT_MAX_SIG_CACHE_SIZE = 32;
17+
// Maximum sig cache size allowed
18+
static const int64_t MAX_MAX_SIG_CACHE_SIZE = 16384;
1719

1820
class CPubKey;
1921

0 commit comments

Comments
 (0)