Don't attempt to resize vector to negative size. #2705

pull robbak wants to merge 1 commits into bitcoin:master from robbak:crash_zero_size_peers.dat changing 1 files +2 −0
  1. robbak commented at 2:22 AM on May 29, 2013: contributor

    When my system crashes, the peers.dat file often ends up zero-sized. When this happens, vector vchData.resize(dataSize) is handed a negative dataSize. It takes it as an unsigned int and attempts to resize it to 18 hexillion bytes, crashing with a std::bad_alloc, which is presented as "bitcoin in Runaway exception"

    This pull simply adds a 'if ( dataSize < 0 ) dataSize = 0;' check before running resize(). Another option would be to make the resize() call itself conditional on dataSize being positive.

  2. Don't attempt to resize vector to negative size. 98ab2b5a26
  3. BitcoinPullTester commented at 3:19 AM on May 29, 2013: none

    Automatic sanity-testing: PASSED, see http://jenkins.bluematt.me/pull-tester/98ab2b5a265e4794d6a9d22212d992bfd1f9c1f1 for binaries and test log. This test script verifies pulls every time they are updated. It, however, dies sometimes and fails to test properly. If you are waiting on a test, please check timestamps to verify that the test.log is moving at http://jenkins.bluematt.me/pull-tester/current/ Contact BlueMatt on freenode if something looks broken.

  4. sipa commented at 5:49 PM on May 29, 2013: member

    This seems harmless.

  5. in src/db.cpp:None in 98ab2b5a26
     540 | @@ -541,6 +541,8 @@ bool CAddrDB::Read(CAddrMan& addr)
     541 |      // use file size to size memory buffer
     542 |      int fileSize = GetFilesize(filein);
     543 |      int dataSize = fileSize - sizeof(uint256);
     544 | +    //Don't try to resize to a negative number if file is small
     545 | +    if ( dataSize < 0 ) dataSize = 0;
    


    laanwj commented at 12:16 PM on May 30, 2013:

    May be handle the problem immediately, ie do an return error("...") here (though I suppose when the file is too short, it will fail later anyway when doing filein >> hashIn, so it may not matter apart from clarity)

  6. jgarzik referenced this in commit 87ec1929cf on May 30, 2013
  7. jgarzik merged this on May 30, 2013
  8. jgarzik closed this on May 30, 2013

  9. DrahtBot locked this on Sep 8, 2021

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-29 03:16 UTC

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