From c6e7f224c119f47af250a9e0c5b185cb98b30c4c Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Thu, 1 Dec 2022 12:20:45 -0500 Subject: [PATCH 1/2] util: Add StrFormatInternalBug and STR_INTERNAL_BUG --- src/util/check.cpp | 7 +++++-- src/util/check.h | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/util/check.cpp b/src/util/check.cpp index e50d6087d0b..c4982ff4adc 100644 --- a/src/util/check.cpp +++ b/src/util/check.cpp @@ -14,10 +14,13 @@ #include #include +std::string StrFormatInternalBug(const char* msg, const char* file, int line, const char* func) +{ + return strprintf("Internal bug detected: \"%s\"\n%s:%d (%s)\nPlease report this issue here: %s\n", msg, file, line, func, PACKAGE_BUGREPORT); +} NonFatalCheckError::NonFatalCheckError(const char* msg, const char* file, int line, const char* func) - : std::runtime_error{ - strprintf("Internal bug detected: \"%s\"\n%s:%d (%s)\nPlease report this issue here: %s\n", msg, file, line, func, PACKAGE_BUGREPORT)} + : std::runtime_error{StrFormatInternalBug(msg, file, line, func)} { } diff --git a/src/util/check.h b/src/util/check.h index b6c03bed2ac..b7919445025 100644 --- a/src/util/check.h +++ b/src/util/check.h @@ -10,12 +10,16 @@ #include #include +std::string StrFormatInternalBug(const char* msg, const char* file, int line, const char* func); + class NonFatalCheckError : public std::runtime_error { public: NonFatalCheckError(const char* msg, const char* file, int line, const char* func); }; +#define STR_INTERNAL_BUG(msg) StrFormatInternalBug((msg), __FILE__, __LINE__, __func__) + /** Helper for CHECK_NONFATAL() */ template T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const char* file, int line, const char* func, const char* assertion) From 3eb041f014870954db564369a4be4bd0dea48fbe Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Wed, 30 Nov 2022 10:48:05 -0500 Subject: [PATCH 2/2] wallet: Change coin selection fee assert to error Returning an error instead of asserting for the low fee check will be better as it does not crash the node and instructs users to report the bug. --- src/wallet/spend.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/wallet/spend.cpp b/src/wallet/spend.cpp index 8c0d56a1cb0..178b5ff395a 100644 --- a/src/wallet/spend.cpp +++ b/src/wallet/spend.cpp @@ -940,7 +940,9 @@ static util::Result CreateTransactionInternal( // The only time that fee_needed should be less than the amount available for fees is when // we are subtracting the fee from the outputs. If this occurs at any other time, it is a bug. - assert(coin_selection_params.m_subtract_fee_outputs || fee_needed <= nFeeRet); + if (!coin_selection_params.m_subtract_fee_outputs && fee_needed > nFeeRet) { + return util::Error{Untranslated(STR_INTERNAL_BUG("Fee needed > fee paid"))}; + } // If there is a change output and we overpay the fees then increase the change to match the fee needed if (nChangePosInOut != -1 && fee_needed < nFeeRet) {