Skip to content

Commit f21b122

Browse files
jnewberyfurszy
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 464b922 commit f21b122

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
@@ -94,8 +94,9 @@ static CSignatureCache signatureCache;
9494
// To be called once in AppInit2/TestingSetup to initialize the signatureCache
9595
void InitSignatureCache()
9696
{
97-
size_t nMaxCacheSize = GetArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE) * ((size_t) 1 << 20);
98-
if (nMaxCacheSize <= 0) return;
97+
// nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
98+
// setup_bytes creates the minimum possible cache (2 elements).
99+
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);
99100
size_t nElems = signatureCache.setup_bytes(nMaxCacheSize);
100101
LogPrintf("Using %zu MiB out of %zu requested for signature cache, able to store %zu elements\n",
101102
(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)