test, build: Enable -Werror=sign-compare #18216

pull Empact wants to merge 3 commits into bitcoin:master from Empact:2020-02-sign-compare changing 15 files +81 −76
  1. Empact commented at 11:43 PM on February 27, 2020: member

    Disallowing sign-comparison mismatches can help to prevent the introduction of overflow and interpretation bugs.

    In this case, ~all~ most existing violations are in the tests, and most simply required annotating the literal as unsigned for comparison.

    This was previously prevented by violations in leveldb which were fixed upstream and merged in #17398. You can test that by building this branch against: 22d11187ee3c7abfe9d43c9eb68f102498cc2b9a vs 75fb37ce68289eb7e00e2ccdd2ef7f9271332545

  2. fanquake added the label Build system on Feb 27, 2020
  3. fanquake added the label Tests on Feb 27, 2020
  4. fanquake commented at 12:44 AM on February 28, 2020: member

    This is failing on the ARM64 Travis build:

      CXX      util/libbitcoin_util_a-asmap.o
      CXX      util/libbitcoin_util_a-bip32.o
    util/asmap.cpp: In function ‘uint32_t Interpret(const std::vector<bool>&, const std::vector<bool>&)’:
    util/asmap.cpp:82:26: error: comparison of integer expressions of different signedness: ‘uint32_t’ {aka ‘unsigned int’} and ‘std::ptrdiff_t’ {aka ‘int’} [-Werror=sign-compare]
                     if (jump >= endpos - pos) break;
                         ~~~~~^~~~~~~~~~~~~~~
    
  5. Empact force-pushed on Feb 28, 2020
  6. Empact force-pushed on Feb 28, 2020
  7. Empact commented at 3:07 AM on February 28, 2020: member
  8. DrahtBot commented at 5:06 AM on February 28, 2020: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #18857 (build: avoid repetitions when enabling warnings in configure.ac by vasild)
    • #18468 (Span improvements by sipa)
    • #16710 (build: Enable -Wsuggest-override if available by hebasto)
    • #15719 (Drop Chain::requestMempoolTransactions method by ryanofsky)
    • #13062 (Make script interpreter independent from storage type CScript by sipa)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  9. DrahtBot cross-referenced this on Feb 28, 2020 from issue build: add --enable-isystem and change --enable-werror to enable -Werror by vasild
  10. fanquake requested review from practicalswift on Feb 28, 2020
  11. DrahtBot cross-referenced this on Feb 28, 2020 from issue build: Optionally enable -Wzero-as-null-pointer-constant by Empact
  12. DrahtBot cross-referenced this on Feb 28, 2020 from issue Build: enable -Wdocumentation via isystem by Empact
  13. practicalswift commented at 8:12 PM on February 29, 2020: contributor

    Thanks for doing this! I'm confident that adding -Werror=sign-compare to --enable-werror will prevent a non-zero number of bugs hitting master in the future: strong concept ACK! :)

    I've reviewed the code and my only suggestion is to add -Wsign-compare as part of our set of default diagnostics. That way developers will have a chance of resolving any sign-compare issues before PR submission (and thus --enable-werror scrunity in Travis).

    Tested ACK 416f3be736faabddd06945557e726a28df9688ca modulo above suggestion

  14. practicalswift commented at 8:16 PM on February 29, 2020: contributor

    I'm confident that adding -Werror=sign-compare to --enable-werror will prevent a non-zero number of bugs hitting master in the future: strong concept ACK! :)

    FWIW -- CVE-2017-18350 was signedness related:

    "CVE-2017-18350 is a buffer overflow vulnerability which allows a malicious SOCKS proxy server to overwrite the program stack on systems with a signed char type (including common 32-bit and 64-bit x86 PCs)."

  15. Empact force-pushed on Feb 29, 2020
  16. practicalswift approved
  17. practicalswift commented at 10:17 PM on February 29, 2020: contributor

    ACK acee61494d50d2821003dde6c888b9ed3ed1ddce

  18. DrahtBot cross-referenced this on Mar 1, 2020 from issue build: Enable -Wsuggest-override if available by hebasto
  19. hebasto commented at 3:23 PM on March 10, 2020: member

    Concept ACK.

  20. in src/util/asmap.cpp:80 in f35268db83 outdated
      76 | @@ -77,9 +77,10 @@ uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip)
      77 |              return DecodeASN(pos, endpos);
      78 |          } else if (opcode == 1) {
      79 |              jump = DecodeJump(pos, endpos);
      80 | +            assert(pos + jump > pos);
    


    hebasto commented at 9:34 PM on March 10, 2020:

    Why this assert is needed here?


    Empact commented at 5:57 PM on March 11, 2020:

    I mean for that to protect against the unlikely case of overflow of pos + jump overflowing - if an overflow occurs, pos + jump will be less than pos.


    practicalswift commented at 5:59 PM on March 11, 2020:

    Is the failure condition possible (pos + jump <= pos)? If it is possible to reach I'm not certain aborting is the right thing to do :)


    laanwj commented at 1:04 PM on April 22, 2020:

    I don't think this should be an assert. It can be caused by incorrect input (an corrupt asmap file), not just developer errors. We shouldn't use assert for error handling. Don't we have other ways of reporting an error here?


    Empact commented at 2:12 AM on April 23, 2020:

    Updated to break which results in returning 0, which is not a valid ASN.

  21. hebasto commented at 10:28 PM on March 10, 2020: member

    Tested on macOS 10.15.3:

    % clang --version | head -1  
    Apple clang version 11.0.0 (clang-1100.0.33.17)
    % brew info boost | head -1  
    boost: stable 1.72.0 (bottled), HEAD
    % make > /dev/null           
    /Library/Developer/CommandLineTools/usr/bin/ranlib: file: libbitcoin_util.a(libbitcoin_util_a-strnlen.o) has no symbols
    /Library/Developer/CommandLineTools/usr/bin/ranlib: file: libbitcoin_util.a(libbitcoin_util_a-sync.o) has no symbols
    /Library/Developer/CommandLineTools/usr/bin/ranlib: file: libbitcoin_util.a(libbitcoin_util_a-string.o) has no symbols
    /Library/Developer/CommandLineTools/usr/bin/ranlib: file: libbitcoin_util.a(libbitcoin_util_a-strnlen.o) has no symbols
    /Library/Developer/CommandLineTools/usr/bin/ranlib: file: libbitcoin_util.a(libbitcoin_util_a-sync.o) has no symbols
    /Library/Developer/CommandLineTools/usr/bin/ranlib: file: libbitcoin_util.a(libbitcoin_util_a-string.o) has no symbols
    
  22. hebasto approved
  23. hebasto commented at 10:49 PM on March 10, 2020: member

    ACK acee61494d50d2821003dde6c888b9ed3ed1ddce

  24. practicalswift commented at 6:45 AM on March 11, 2020: contributor

    ACK acee61494d50d2821003dde6c888b9ed3ed1ddce modulo assert - see @hebasto's question above which needs to be addressed :)

  25. jonatack commented at 5:39 PM on March 16, 2020: contributor

    ACK acee6149

  26. DrahtBot cross-referenced this on Mar 30, 2020 from issue Span improvements by sipa
  27. DrahtBot cross-referenced this on Mar 30, 2020 from issue Make script interpreter independent from storage type CScript by sipa
  28. DrahtBot cross-referenced this on Apr 3, 2020 from issue Improve asmap checks and add sanity check by sipa
  29. DrahtBot cross-referenced this on Apr 9, 2020 from issue [RFC] bitcoin-asmap utility by sipa
  30. MarcoFalke commented at 8:11 PM on April 22, 2020: member

    Can remove some of the lines in test/sanitizer_suppressions/ubsan now?

  31. MarcoFalke commented at 9:25 PM on April 22, 2020: member

    Could also split up the test changes to merge them in the fast track? The Core changes seem controversial for now.

  32. Empact force-pushed on Apr 23, 2020
  33. practicalswift commented at 10:42 AM on April 23, 2020: contributor

    ACK b8a6a2de910aed711026a8114be5391135beedb3 -- patch looks correct

  34. in configure.ac:387 in b8a6a2de91 outdated
     345 | @@ -345,6 +346,7 @@ if test "x$CXXFLAGS_overridden" = "xno"; then
     346 |    AX_CHECK_COMPILE_FLAG([-Wredundant-decls],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wredundant-decls"],,[[$CXXFLAG_WERROR]])
     347 |    AX_CHECK_COMPILE_FLAG([-Wunused-variable],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wunused-variable"],,[[$CXXFLAG_WERROR]])
     348 |    AX_CHECK_COMPILE_FLAG([-Wdate-time],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wdate-time"],,[[$CXXFLAG_WERROR]])
     349 | +  AX_CHECK_COMPILE_FLAG([-Wsign-compare],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wsign-compare"],,[[$CXXFLAG_WERROR]])
    


    vasild commented at 11:24 AM on April 23, 2020:

    It is not necessary to add -Wsign-compare because it is enabled by -Wall in gcc and by -Wextra in clang. Just a few lines above we enable both -Wall and -Wextra.

    https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wsign-compare https://clang.llvm.org/docs/DiagnosticsReference.html#wextra

    This was explicitly requested by @practicalswift, but maybe he did not realize that it is already enabled in both gcc and clang?


    Empact commented at 6:32 PM on April 23, 2020:

    Leaving as it seems harmless to me.

  35. in src/util/asmap.cpp:100 in b8a6a2de91 outdated
      83 |              if (bits == 0) break;
      84 |              if (ip[ip.size() - bits]) {
      85 | -                if (jump >= endpos - pos) break;
      86 | +                if (pos + jump >= endpos) break;
      87 |                  pos += jump;
      88 |              }
    


    vasild commented at 11:41 AM on April 23, 2020:

    Shouldn't the added check for overflow be inside if (ip[ip.size() - bits])? If we don't get inside that if then we don't care whether it overflows because we would not do the arithmetic that would overflow.

    nit: only 2 spaces used for indentation, the rest of the code uses 4.


    Empact commented at 6:31 PM on April 23, 2020:

    Fixed, thanks.

  36. fjahr commented at 11:47 AM on April 23, 2020: contributor

    tACK b8a6a2de910aed711026a8114be5391135beedb3

    Thanks for doing this, compiling on macOS will be without warnings again thanks to this (see https://traviszci.org/github/bitcoin/bitcoin/jobs/678210021#L2026 for example). Not sure why these only showed up on macOS and not on Linux previously.

  37. vasild approved
  38. vasild commented at 11:57 AM on April 23, 2020: contributor

    utACK b8a6a2d

    Some comments below, feel free to ignore.

  39. Empact force-pushed on Apr 23, 2020
  40. vasild commented at 6:42 PM on April 23, 2020: contributor

    utACK ba1b64a

    It is good to expand the reach of -Werror=

  41. DrahtBot cross-referenced this on May 1, 2020 from issue build: warn on potentially uninitialized reads by vasild
  42. DrahtBot cross-referenced this on May 2, 2020 from issue The Zero Allocations project by jb55
  43. DrahtBot cross-referenced this on May 3, 2020 from issue build: avoid repetitions when enabling warnings in configure.ac by vasild
  44. DrahtBot added the label Needs rebase on May 6, 2020
  45. test: Fix outstanding -Wsign-compare errors df37377e30
  46. Empact force-pushed on May 8, 2020
  47. Empact commented at 6:22 PM on May 8, 2020: member

    Rebased for #18512

  48. DrahtBot removed the label Needs rebase on May 8, 2020
  49. practicalswift commented at 8:06 PM on May 8, 2020: contributor

    @Empact Looks good, but this one needs to be fixed for the ARM job to succeed :)

    util/asmap.cpp: In function ‘bool SanityCheckASMap(const std::vector<bool>&, int)’:
    util/asmap.cpp:159:22: error: comparison of integer expressions of different signedness: ‘uint32_t’ {aka ‘unsigned int’} and ‘std::ptrdiff_t’ {aka ‘int’} [-Werror=sign-compare]
                 if (jump > endpos - pos) return false; // Jump out of range
                     ~~~~~^~~~~~~~~~~~~~
    
  50. hebasto commented at 3:35 AM on May 9, 2020: member

    @Empact Looks good, but this one needs to be fixed for the ARM job to succeed :)

    util/asmap.cpp: In function ‘bool SanityCheckASMap(const std::vector<bool>&, int)’:
    util/asmap.cpp:159:22: error: comparison of integer expressions of different signedness: ‘uint32_t’ {aka ‘unsigned int’} and ‘std::ptrdiff_t’ {aka ‘int’} [-Werror=sign-compare]
                 if (jump > endpos - pos) return false; // Jump out of range
                     ~~~~~^~~~~~~~~~~~~~
    

    Also #18686.

  51. DrahtBot cross-referenced this on May 9, 2020 from issue Wallet passive startup by ryanofsky
  52. Empact force-pushed on May 9, 2020
  53. refactor: Rework asmap Interpret to avoid ptrdiff_t eac6a3080d
  54. build: Enable -Werror=sign-compare
    Explicitly add -Wsign-compare as well - not required for all compilers, as GCC activates it
    under -Wall, but may impact clang, etc.
    68537275bd
  55. Empact force-pushed on May 9, 2020
  56. practicalswift commented at 10:03 AM on May 9, 2020: contributor

    ACK 68537275bd91d1dc14a69609ae443f955bfdbd64

    As noted in earlier comments: I'm confident this change will help us catch a non-zero amount of bugs hitting master going forward :)

  57. fjahr commented at 1:49 PM on May 10, 2020: contributor

    re-ACK 68537275bd91d1dc14a69609ae443f955bfdbd64

  58. fanquake merged this on May 11, 2020
  59. fanquake closed this on May 11, 2020

  60. fanquake referenced this in commit b4037ae814 on May 11, 2020
  61. LarryRuane referenced this in commit ee42af0f80 on Jul 13, 2020
  62. Fabcien referenced this in commit 942d91965b on Feb 10, 2021
  63. Empact deleted the branch on Apr 13, 2021
  64. MarcoFalke commented at 6:56 AM on April 29, 2021: member

    I went ahead and reverted the asmap changes, because they rely on UB: #21802

  65. PastaPastaPasta referenced this in commit 72bc72136d on Oct 6, 2021
  66. PastaPastaPasta referenced this in commit ebe48e688c on Oct 6, 2021
  67. PastaPastaPasta referenced this in commit 5392f8ec22 on Oct 10, 2021
  68. PastaPastaPasta referenced this in commit de864fa149 on Oct 12, 2021
  69. PastaPastaPasta referenced this in commit 9eb4e09ee4 on Dec 12, 2021
  70. PastaPastaPasta referenced this in commit 6204c0288d on Dec 12, 2021
  71. bitcoin 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-05-20 06:53 UTC

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