Skip to content

Unsigned integer wraparound in CCoinsViewCache::SpendCoin makes CCoinsViewCache::DynamicMemoryUsage() return bogus values #21001

@practicalswift

Description

@practicalswift

When running the fuzz tests under -fsanitize=integer I stumbled upon this:

coins.cpp:114:22: runtime error: unsigned integer overflow: 0 - 96 cannot be represented in type 'unsigned long'

bitcoin/src/coins.cpp

Lines 111 to 114 in e31af10

bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
CCoinsMap::iterator it = FetchCoin(outpoint);
if (it == cacheCoins.end()) return false;
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();

Note that cachedCoinsUsage is decreased despite being zero which causes it to wrap around. This in turn makes CCoinsViewCache::DynamicMemoryUsage start returning bogus values.

Nothing high priority of course, but perhaps worth fixing? :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions