Skip to content

Commit

Permalink
Ensure -maxsigcachesize is in valid range
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
jnewbery authored and laanwj committed Feb 17, 2017
1 parent 476cc47 commit 55c403b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/script/sigcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ static CSignatureCache signatureCache;
// To be called once in AppInit2/TestingSetup to initialize the signatureCache
void InitSignatureCache()
{
size_t nMaxCacheSize = GetArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE) * ((size_t) 1 << 20);
if (nMaxCacheSize <= 0) return;
// nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
// setup_bytes creates the minimum possible cache (2 elements).
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);
size_t nElems = signatureCache.setup_bytes(nMaxCacheSize);
LogPrintf("Using %zu MiB out of %zu requested for signature cache, able to store %zu elements\n",
(nElems*sizeof(uint256)) >>20, nMaxCacheSize>>20, nElems);
Expand Down
2 changes: 2 additions & 0 deletions src/script/sigcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// systems). Due to how we count cache size, actual memory usage is slightly
// more (~32.25 MB)
static const unsigned int DEFAULT_MAX_SIG_CACHE_SIZE = 32;
// Maximum sig cache size allowed
static const int64_t MAX_MAX_SIG_CACHE_SIZE = 16384;

class CPubKey;

Expand Down

0 comments on commit 55c403b

Please sign in to comment.