From 56d878c4650cc46ce54d1d79db054ac44b486605 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Tue, 2 Jan 2024 13:24:02 +0100 Subject: [PATCH] fuzz: avoid underflow in coins_view target --- src/test/fuzz/coins_view.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/fuzz/coins_view.cpp b/src/test/fuzz/coins_view.cpp index 9c6aa6e7a1e..e6361098314 100644 --- a/src/test/fuzz/coins_view.cpp +++ b/src/test/fuzz/coins_view.cpp @@ -69,6 +69,12 @@ FUZZ_TARGET(coins_view, .init = initialize_coins_view) if (e.what() == std::string{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"}) { assert(!possible_overwrite); expected_code_path = true; + // AddCoin() decreases cachedCoinsUsage by the memory usage of the old coin at the beginning and + // increases it by the value of the new coin at the end. If it throws in the process, the value + // of cachedCoinsUsage would have been incorrectly decreased, leading to an underflow later on. + // To avoid this, use Flush() to reset the value of cachedCoinsUsage in sync with the cacheCoins + // mapping. + (void)coins_view_cache.Flush(); } } assert(expected_code_path);