tests: Don't assert(...) with side effects #14088

pull practicalswift wants to merge 2 commits into bitcoin:master from practicalswift:assertions-with-side-effects changing 2 files +27 −2
  1. practicalswift commented at 8:25 AM on August 28, 2018: contributor

    Don't assert(...) with side effects.

    From the developer notes:

    Assertions should not have side-effects

    Rationale: Even though the source code is set to refuse to compile with assertions disabled, having side-effects in assertions is unexpected and makes the code harder to understand

    These assertions were introduced quite recently (in #14069 which was merged two days ago) and since this is a recurring thing (see #13534 – "Don't assert(foo()) where foo() has side effects" from May) I added a simple regression test for the most obvious common side effect.

  2. Don't assert(...) with side effects 4c3c9c3869
  3. practicalswift renamed this:
    tests: Don't `assert(...)` with side effects
    tests: Don't assert(...) with side effects
    on Aug 28, 2018
  4. fanquake added the label Tests on Aug 28, 2018
  5. ken2812221 commented at 9:20 AM on August 28, 2018: contributor

    utACK 1916b4e

  6. skeees commented at 12:12 PM on August 28, 2018: contributor

    Thanks :) utACK 1916b4e I don't know how I feel about the linter though - it is too narrowly scoped to ++|-- and won't catch anything inside of a BOOST_* style assert either.

    If you're worried about somebody trying to run a node with NDEBUG - maybe add a more explanatory warning message https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp#L51 about why this could dangerous - instead of the very vague one that's there now?

  7. practicalswift commented at 12:22 PM on August 28, 2018: contributor

    @skeees Yes, the regression test will only catch this specific type of side effect. The regression test is not meant to be exhaustive (regression tests seldom are! :-)).

    FWIW, this is the third time this specific type of side effect is fixed during the past few months so having a check for this is better than no checking :-)

  8. Add regression test: Don't assert(...) with side effects ca1a093127
  9. practicalswift force-pushed on Aug 28, 2018
  10. practicalswift commented at 12:31 PM on August 28, 2018: contributor

    @skeees Updated version based on your feedback with improved comments. Now catching also accidental assignment operations in assertions :-)

    From PRE31-C (SEI CERT C Coding Standard):

    Assertions should not contain assignments, increment, or decrement operators.

    Now catching all three.

  11. practicalswift commented at 12:34 PM on August 28, 2018: contributor

    @skeees Regarding BOOST_ASSERT(...) – we're not using it are we? :-)

  12. skeees commented at 12:53 PM on August 28, 2018: contributor

    Cool - utACK - I think this change makes things strictly better. Regarding BOOST_CHECK_* stuff - used in unit tests but not production code I believe

    I do think its worth changing the #error warning message (or adding a comment nearby) to something that explains why bitcoin cannot be compiled without assertions. Right now it just states so without any reason. Perhaps:

    In addition to providing important sanity checks on the internal state of the system, is possible that assert statements throughout bitcoin may contain side-effecting code that is consensus critical, therefore bitcoin may not be compiled without assertions

  13. practicalswift commented at 1:08 PM on August 28, 2018: contributor

    @skeees BOOST_CHECK_* is ever used outside of the tests:

    $ git grep BOOST_CHECK_ ":(exclude)*/test/*"
    $
    

    And I don't see any scenario where BOOST_CHECK_* could be removed at run-time like assert(…) under NDEBUG. Do you? :-)

  14. laanwj commented at 1:00 PM on August 31, 2018: member

    utACK ca1a093127c11bb2aea10bf96c38dbfb40f8d170

    And I don't see any scenario where BOOST_CHECK_* could be removed at run-time like assert(…) under NDEBUG. Do you? :-)

    No—the problem does not exist for BOOST_CHECK_*, leave those alone please.

  15. laanwj merged this on Aug 31, 2018
  16. laanwj closed this on Aug 31, 2018

  17. laanwj referenced this in commit 709a15b0a6 on Aug 31, 2018
  18. practicalswift deleted the branch on Apr 10, 2021
  19. PastaPastaPasta referenced this in commit 969de22347 on Jun 27, 2021
  20. PastaPastaPasta referenced this in commit b5a3283124 on Jun 28, 2021
  21. PastaPastaPasta referenced this in commit 5303574f90 on Jun 29, 2021
  22. PastaPastaPasta referenced this in commit 60c722151f on Jun 29, 2021
  23. PastaPastaPasta referenced this in commit 689230b978 on Jun 29, 2021
  24. PastaPastaPasta referenced this in commit 658d86e0d4 on Jun 29, 2021
  25. PastaPastaPasta referenced this in commit 989e01887d on Jun 29, 2021
  26. PastaPastaPasta referenced this in commit cd5a39c8af on Jul 1, 2021
  27. DrahtBot locked this on Aug 18, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-16 15:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me