bench: Assert that division by zero is unreachable #9547

pull practicalswift wants to merge 1 commits into bitcoin:master from practicalswift:avoid-potential-division-by-zero-in-benchmark-state-keeprunning changing 1 files +2 −0
  1. practicalswift commented at 9:00 PM on January 13, 2017: contributor

    No description provided.

  2. in src/bench/bench.cpp:None in 6f96b2e518 outdated
      87 | @@ -88,7 +88,7 @@ bool benchmark::State::KeepRunning()
      88 |      lastCycles = nowCycles;
      89 |      ++count;
      90 |  
      91 | -    if (now - beginTime < maxElapsed) return true; // Keep going
      92 | +    if (now - beginTime < maxElapsed || count == 1) return true; // Keep going
    


    gmaxwell commented at 9:56 PM on January 13, 2017:

    because of the ++count above I don't believe this can be 1 here.


    practicalswift commented at 9:59 PM on January 13, 2017:

    @gmaxwell But there is a --count on the line below :-)


    gmaxwell commented at 10:00 PM on January 13, 2017:

    yes, my read it can't be 1 or 0, it must be >1 and then the decrements reduces it to 1. If it is possible to be 0 at the division then I believe there is a bug elsewhere (in addition).


    practicalswift commented at 10:16 PM on January 13, 2017:

    If count would be guaranteed to be different from zero, then the check on line 50 (if (count == 0) {) wouldn't be needed, right? :-)

    If the branch if (count == 0) { is taken then the relevant parts of the remaining code can be reduced to something like:

    count = 0; // implicit
    ++count;
    --count;
    double average = (now-beginTime)/count;
    

    Assuming I'm reading it correctly :-)

    Let me know if I'm missing something obvious – this is one of my first contributions so please bear with me :-)


    instagibbs commented at 6:05 PM on January 19, 2017:

    Looks like if maxElapsed is > 0 then the logic here is redundant for that code path. count=0 means now and beginTime are both 0.


    isle2983 commented at 9:34 PM on January 20, 2017:

    It might be better to fix the issue in the output at the bottom of the function like so:

         // Output results
    -    double average = (now-beginTime)/count;
    -    int64_t averageCycles = (nowCycles-beginCycles)/count;
    +    double average = (count > 0) ? (now-beginTime)/count : 0.0;
    +    int64_t averageCycles = (count > 0) ? (nowCycles-beginCycles)/count : 0;
         std::cout << std::fixed << std::setprecision(15) << name << "," << count << "," << minTime << "," << maxTime << "," << average << ","
                   << minCycles << "," << maxCycles << "," << averageCycles << "\n";
    

    I would say that this reads ok in isolation as just dumb output code and avoids having to understand the branching in function body above.

  3. fanquake added the label Tests on Jan 14, 2017
  4. practicalswift commented at 4:35 PM on January 18, 2017: contributor

    If we're guaranteed that count cannot be 0 in this context, should I remove the then redundant if (count == 0) {-check on line 50 instead?

    As said, please let me know if I'm missing something obvious here and I'll close the PR :-)

  5. MarcoFalke commented at 2:24 AM on January 22, 2017: member

    Agree with @instagibbs that the code is redundant. Maybe just add a comment or assert that says the count is never zero?

  6. practicalswift force-pushed on Jan 22, 2017
  7. practicalswift force-pushed on Jan 22, 2017
  8. practicalswift force-pushed on Jan 22, 2017
  9. practicalswift commented at 9:24 AM on January 22, 2017: contributor

    @MarcoFalke Good point! Updated, squashed and pushed - looks good? :-)

  10. jtimon commented at 12:07 AM on February 1, 2017: contributor

    utACK e91074b

  11. laanwj commented at 9:07 AM on February 2, 2017: member

    We shouldn't be using an assertion for error handling.

    When can this happen? What should be the result (instead of dividing by zero)?

  12. laanwj commented at 9:08 AM on February 2, 2017: member

    Okay, just re-read the post above, so this can never happen. That wasn't clear to me from the code change: please add a comment specifying that.

  13. Assert that what might look like a possible division by zero is actually unreachable db07f91899
  14. practicalswift force-pushed on Feb 2, 2017
  15. practicalswift commented at 9:46 AM on February 2, 2017: contributor

    @laanwj Good point! Updated and pushed. Looks good? :-)

  16. practicalswift commented at 10:19 PM on February 27, 2017: contributor

    Any changes needed before merge? :-)

  17. laanwj commented at 11:54 AM on February 28, 2017: member

    I think you should change the pull title first - it is incorrect to say that this is a potential division by zero, because it's avoided by previous code.

  18. practicalswift renamed this:
    Avoid potential division by zero in benchmark::State::KeepRunning()
    Assert that what might look like a possible division by zero is actually unreachable
    on Feb 28, 2017
  19. practicalswift commented at 12:28 PM on February 28, 2017: contributor

    @laanwj Changed. Looks good? :-)

  20. MarcoFalke renamed this:
    Assert that what might look like a possible division by zero is actually unreachable
    bench: Assert that division by zero is unreachable
    on Feb 28, 2017
  21. laanwj merged this on Mar 6, 2017
  22. laanwj closed this on Mar 6, 2017

  23. laanwj referenced this in commit d32581cc29 on Mar 6, 2017
  24. PastaPastaPasta referenced this in commit 39a55851a4 on Dec 30, 2018
  25. PastaPastaPasta referenced this in commit 0d9532d945 on Dec 31, 2018
  26. PastaPastaPasta referenced this in commit b11c70ca1b on Dec 31, 2018
  27. PastaPastaPasta referenced this in commit c070945512 on Dec 31, 2018
  28. PastaPastaPasta referenced this in commit f185b6187c on Jan 2, 2019
  29. PastaPastaPasta referenced this in commit 6f1882e077 on Jan 2, 2019
  30. PastaPastaPasta referenced this in commit 2b3b710699 on Jan 3, 2019
  31. PastaPastaPasta referenced this in commit d5e90b7723 on Jan 21, 2019
  32. PastaPastaPasta referenced this in commit 4bcd83ffe8 on Jan 25, 2019
  33. PastaPastaPasta referenced this in commit f23ea2307c on Jan 29, 2019
  34. PastaPastaPasta referenced this in commit 9ed9b73a5e on Feb 1, 2019
  35. PastaPastaPasta referenced this in commit 56d01657c1 on Feb 1, 2019
  36. PastaPastaPasta referenced this in commit 99739e72ae on Feb 1, 2019
  37. PastaPastaPasta referenced this in commit a61b747a29 on Feb 1, 2019
  38. zkbot referenced this in commit aa225ebb0b on Jan 24, 2020
  39. zkbot referenced this in commit 74ff73abab on Jan 24, 2020
  40. furszy referenced this in commit 4ed15cc69d on Jun 8, 2020
  41. practicalswift deleted the branch on Apr 10, 2021
  42. DrahtBot locked this on Aug 16, 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-15 18:15 UTC

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