[bugfix] random: fixes read buffer to use min rather than max #20082

pull EthanHeilman wants to merge 1 commits into bitcoin:master from EthanHeilman:perfmon changing 1 files +3 −2
  1. EthanHeilman commented at 1:41 am on October 5, 2020: contributor

    As shown below when resizing the read buffer vData std::max((vData.size() * 3) / 2, nMaxSize) is used. This means that the buffer size immediately jumps to nMaxSize. I believe the intend of this code is to grow the buffer size through several steps rather than immediately resize it to the max size.

     0    std::vector<unsigned char> vData(250000, 0);
     1    long ret = 0;
     2    unsigned long nSize = 0;
     3    const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data
     4    while (true) {
     5        nSize = vData.size();
     6        ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", nullptr, nullptr, vData.data(), &nSize);
     7        if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
     8            break;
     9        vData.resize(std::max((vData.size() * 3) / 2, nMaxSize)); // Grow size of buffer exponentially
    10    }
    

    vData always starts at size 250,000 and nMaxSize is always 10,000,000 so the first time this line is reached:

    0vData.resize(std::max((vData.size() * 3) / 2, nMaxSize));
    

    the effect will always be to resize vData to nMaxSize. Then because the loop terminates when vData.size >= 10,000,000 only one resize operation will take place.

    To fix this issue we replace std::min with std::max

    This PR also adds a comment clarifying the behavior of this function the first time it is called.

  2. fanquake added the label Utils/log/libs on Oct 5, 2020
  3. EthanHeilman force-pushed on Oct 5, 2020
  4. EthanHeilman force-pushed on Oct 5, 2020
  5. EthanHeilman force-pushed on Oct 6, 2020
  6. EthanHeilman renamed this:
    refactor: Minor refactor of RandAddSeedPerfmon + comments
    [WIP] refactor: Minor refactor of RandAddSeedPerfmon + comments
    on Oct 6, 2020
  7. random: fixes read buffer resizing in RandAddSeedPerfmon
    + Replaces std::max with std::min to resize buffer in RandAddSeedPerfmon
    + Documents behavior of RandAddSeedPerfmon
    bd5215103e
  8. EthanHeilman force-pushed on Oct 8, 2020
  9. EthanHeilman renamed this:
    [WIP] refactor: Minor refactor of RandAddSeedPerfmon + comments
    [bugfix] random: fixes read buffer to use min rather than max
    on Oct 8, 2020
  10. practicalswift commented at 8:41 am on October 9, 2020: contributor
    Concept ACK: nice find! @laanwj As the author of 8ae973c00cfb02161bb2c2a6c839e510cd409278 would you mind reviewing to make sure that the logic @EthanHeilman suggests corresponds to what was originally intended? :)
  11. theStack commented at 5:42 pm on October 11, 2020: member
    Concept ACK Looking at the code, it seems pretty obvious that jumping immediately from 250k to 10M is not desired – changing std::max to std::min leads to the intended behaviour.
  12. fanquake approved
  13. fanquake commented at 5:59 am on October 19, 2020: member
    ACK bd5215103eb3985c1622eddea45a040e6173829c - thanks for taking a look at this Ethan. Swapping from std::max to std::min here certainly seems correct.
  14. fanquake merged this on Oct 19, 2020
  15. fanquake closed this on Oct 19, 2020

  16. EthanHeilman deleted the branch on Oct 19, 2020
  17. sidhujag referenced this in commit 2eaf9f8ee6 on Oct 19, 2020
  18. fanquake referenced this in commit 1f67a30e83 on Oct 23, 2020
  19. fanquake commented at 1:06 am on October 23, 2020: member
    Added this for backport to 0.20 in #20166.
  20. MarcoFalke referenced this in commit a2fa11f9de on Nov 18, 2020
  21. MarkLTZ referenced this in commit 4b518ff15f on Nov 21, 2020
  22. MarkLTZ referenced this in commit c61e643b75 on Nov 21, 2020
  23. Fabcien referenced this in commit da2d0950fb on Nov 25, 2021
  24. DrahtBot locked this on Feb 15, 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: 2024-12-18 18:12 UTC

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