Skip to content

Add an optional extra level of checking: ASSUME(...) - an opt-in side-effect safe assert(...)

As suggested by sipa in the open issue #4576 (closed) (2014) -- "[discussion] Dealing with assertions and optional consistency checking" -- as an alternative to assert(…) in situations where assert(…) is not appropriate:

… What I want is more of these checks, more as a way for the programmer to say "this is what I assume here", more than "if this doesn't hold here, we're in BIG trouble". It makes the code clearer, and simultaneously verifies that such assumptions hold. But only in cases where we're not at risk of hurting the network by dying. …

ASSUME(expression) works like this:

  • If compiled with -DABORT_ON_FAILED_ASSUME (set by --enable-debug and/or --enable-fuzz): Evaluate expression and abort if expression is false.
  • If compiled without -DABORT_ON_FAILED_ASSUME: Evaluate expression and continue execution.

Example:

int main(void) {
    ASSUME(IsFoo());
     ...
}

If !IsFoo() and -DABORT_ON_FAILED_ASSUME, then:

    filename.cpp:123: main: ASSUME(IsFoo()) failed.
    Aborted

Otherwise the execution continues.

Resolves #4576 (closed).

Merge request reports

Loading