LCOV - code coverage report
Current view: top level - src/util - check.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 6 11 54.5 %
Date: 2023-11-10 23:46:46 Functions: 19 67 28.4 %
Branches: 11 50 22.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2019-2022 The Bitcoin Core developers
       2                 :            : // Distributed under the MIT software license, see the accompanying
       3                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :            : 
       5                 :            : #ifndef BITCOIN_UTIL_CHECK_H
       6                 :            : #define BITCOIN_UTIL_CHECK_H
       7                 :            : 
       8                 :            : #include <attributes.h>
       9                 :            : 
      10                 :            : #include <stdexcept>
      11                 :            : #include <string>
      12                 :            : #include <string_view>
      13                 :            : #include <utility>
      14                 :            : 
      15                 :            : std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func);
      16                 :            : 
      17                 :          0 : class NonFatalCheckError : public std::runtime_error
      18                 :            : {
      19                 :            : public:
      20                 :            :     NonFatalCheckError(std::string_view msg, std::string_view file, int line, std::string_view func);
      21                 :            : };
      22                 :            : 
      23                 :            : /** Helper for CHECK_NONFATAL() */
      24                 :            : template <typename T>
      25                 :      12304 : T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const char* file, int line, const char* func, const char* assertion)
      26                 :            : {
      27 [ +  - ][ #  # ]:      12304 :     if (!val) {
         [ #  # ][ #  # ]
                 [ #  # ]
      28 [ #  # ][ #  # ]:          0 :         throw NonFatalCheckError{assertion, file, line, func};
         [ #  # ][ #  # ]
                 [ #  # ]
      29                 :            :     }
      30                 :      12304 :     return std::forward<T>(val);
      31                 :          0 : }
      32                 :            : 
      33                 :            : #if defined(NDEBUG)
      34                 :            : #error "Cannot compile without assertions!"
      35                 :            : #endif
      36                 :            : 
      37                 :            : /** Helper for Assert() */
      38                 :            : void assertion_fail(std::string_view file, int line, std::string_view func, std::string_view assertion);
      39                 :            : 
      40                 :            : /** Helper for Assert()/Assume() */
      41                 :            : template <bool IS_ASSERT, typename T>
      42                 :     149976 : T&& inline_assertion_check(LIFETIMEBOUND T&& val, [[maybe_unused]] const char* file, [[maybe_unused]] int line, [[maybe_unused]] const char* func, [[maybe_unused]] const char* assertion)
      43                 :            : {
      44                 :            :     if constexpr (IS_ASSERT
      45                 :            : #ifdef ABORT_ON_FAILED_ASSUME
      46                 :            :                   || true
      47                 :            : #endif
      48                 :            :     ) {
      49 [ +  + ][ +  + ]:     149976 :         if (!val) {
         [ +  + ][ +  - ]
         [ #  # ][ +  - ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ +  - ]
                 [ +  - ]
      50                 :          0 :             assertion_fail(file, line, func, assertion);
      51                 :          0 :         }
      52                 :            :     }
      53                 :     149976 :     return std::forward<T>(val);
      54                 :            : }
      55                 :            : 
      56                 :            : // All macros may use __func__ inside a lambda, so put them under nolint.
      57                 :            : // NOLINTBEGIN(bugprone-lambda-function-name)
      58                 :            : 
      59                 :            : #define STR_INTERNAL_BUG(msg) StrFormatInternalBug((msg), __FILE__, __LINE__, __func__)
      60                 :            : 
      61                 :            : /**
      62                 :            :  * Identity function. Throw a NonFatalCheckError when the condition evaluates to false
      63                 :            :  *
      64                 :            :  * This should only be used
      65                 :            :  * - where the condition is assumed to be true, not for error handling or validating user input
      66                 :            :  * - where a failure to fulfill the condition is recoverable and does not abort the program
      67                 :            :  *
      68                 :            :  * For example in RPC code, where it is undesirable to crash the whole program, this can be generally used to replace
      69                 :            :  * asserts or recoverable logic errors. A NonFatalCheckError in RPC code is caught and passed as a string to the RPC
      70                 :            :  * caller, which can then report the issue to the developers.
      71                 :            :  */
      72                 :            : #define CHECK_NONFATAL(condition) \
      73                 :            :     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
      74                 :            : 
      75                 :            : /** Identity function. Abort if the value compares equal to zero */
      76                 :            : #define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
      77                 :            : 
      78                 :            : /**
      79                 :            :  * Assume is the identity function.
      80                 :            :  *
      81                 :            :  * - Should be used to run non-fatal checks. In debug builds it behaves like
      82                 :            :  *   Assert()/assert() to notify developers and testers about non-fatal errors.
      83                 :            :  *   In production it doesn't warn or log anything.
      84                 :            :  * - For fatal errors, use Assert().
      85                 :            :  * - For non-fatal errors in interactive sessions (e.g. RPC or command line
      86                 :            :  *   interfaces), CHECK_NONFATAL() might be more appropriate.
      87                 :            :  */
      88                 :            : #define Assume(val) inline_assertion_check<false>(val, __FILE__, __LINE__, __func__, #val)
      89                 :            : 
      90                 :            : /**
      91                 :            :  * NONFATAL_UNREACHABLE() is a macro that is used to mark unreachable code. It throws a NonFatalCheckError.
      92                 :            :  */
      93                 :            : #define NONFATAL_UNREACHABLE()                                        \
      94                 :            :     throw NonFatalCheckError(                                         \
      95                 :            :         "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__)
      96                 :            : 
      97                 :            : // NOLINTEND(bugprone-lambda-function-name)
      98                 :            : 
      99                 :            : #endif // BITCOIN_UTIL_CHECK_H

Generated by: LCOV version 1.14