Address many more sign comparison warnings #1140

pull jgarzik wants to merge 6 commits into bitcoin:master from jgarzik:sign-compare changing 13 files +74 −64
  1. jgarzik commented at 6:05 PM on April 22, 2012: contributor

    This really quiets down -Wall. We are down to 3 key signed/unsigned warnings in main.h, and 2-3 other misc. warnings overall.

  2. Prefer 'unsigned int' for loop index variables tested against ::size()
    C++ STL ::size() generally returns unsigned, which implies that "int idx"
    style of loop variable will generate a signed-vs-unsigned comparison warning
    when testing the loop exit condition "idx < blah.size()"
    
    Update areas of the bitcoin code where loop variables may be more properly and
    correctly defined as unsigned.
    faf705a42a
  3. SigOp and orphan-tx constants and counts are always unsigned.
    Fixes several sign-comparison warnings.
    7bd9c3a3cf
  4. Test ScriptSigArgsExpected() for error, before accumulating return value c0a0a93d02
  5. Add casts for unavoidable signed/unsigned comparisons
    At these code sites, it is preferable to cast rather than change
    a variable's type.
    1d8c7a9557
  6. CBlock::WriteToDisk() properly checks ftell(3) for error return
    Rather than storing ftell(3)'s return value -- a long -- in an
    unsigned int, we store and check a properly typed temp.  Then, assured a
    non-negative value, we store in nBlockPosRet.
    5aa0b23825
  7. Change signed->unsigned at 3 code sites
    This resolves signed/unsigned comparison warnings.
    735a60698c
  8. in src/main.cpp:None in 3b1a096229 outdated
     194 | @@ -195,9 +195,9 @@ void static EraseOrphanTx(uint256 hash)
     195 |      mapOrphanTransactions.erase(hash);
     196 |  }
     197 |  
     198 | -int LimitOrphanTxSize(int nMaxOrphans)
     199 | +static unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
    


    gavinandresen commented at 9:12 PM on April 22, 2012:

    Making this static breaks test/DoS_tests.cpp

  9. jgarzik commented at 6:20 PM on April 23, 2012: contributor

    RE @gavinandresen:

    1. Updated and rebased, to fix LimitOrphanTxSize() issue.

    2. RE "Test ScriptSigArgsExpected() for error" -- I agree ScriptSigArgsExpected() may not return <0 given current conditions, but there are two advantages to the new code, a) the code is less fragile, in case those conditions change in the future. checking for an error that a function is pretty harmless, and trivial in terms of CPU cycles. b) the compiler's optimizer is guaranteed a non-negative value, enabling certain value-range optimizations.

  10. gavinandresen commented at 6:27 PM on April 23, 2012: contributor

    ACK.

  11. jgarzik referenced this in commit c2e8c8acd8 on Apr 24, 2012
  12. jgarzik merged this on Apr 24, 2012
  13. jgarzik closed this on Apr 24, 2012

  14. coblee referenced this in commit ec93ad8369 on Jul 17, 2012
  15. suprnurd referenced this in commit 714f9a4f0d on Dec 5, 2017
  16. lateminer referenced this in commit 70fbbf7ae8 on Jan 22, 2019
  17. xdustinface referenced this in commit 33c0e6641b on Oct 2, 2020
  18. Bushstar referenced this in commit ba3c6519cd on Oct 21, 2020
  19. 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-20 00:16 UTC

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