How to reproduce -Werror=sign-compare failures locally? #19123

issue maflcko opened this issue on May 31, 2020
  1. maflcko commented at 1:10 PM on May 31, 2020: member

    I tried gcc and clang, but those failures won't show for me. See #18637 (comment)

    While I like the warning and error, if developers can't see them on their boxes, it makes it tedious to fix those issues up.

  2. maflcko added the label Feature on May 31, 2020
  3. maflcko added the label Questions and Help on May 31, 2020
  4. maflcko removed the label Feature on May 31, 2020
  5. ajtowns commented at 2:53 PM on July 1, 2020: contributor
    #include <cstddef>
    
    int main(void)
    {
        int x = -2;
        size_t y = 3;
        if (x == y*y) {
            return 1;
        }
        return 0;
    }
    

    gives me errors for both gcc (v9.3) and clang (v9.0):

    $ g++ -Wall -W -Werror -o testsign test.cc
    test.cc: In function ‘int main()’:
    test.cc:7:11: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
        7 |     if (x == y*y) {
          |         ~~^~~~~~
    cc1plus: all warnings being treated as errors
    
    $ clang++ -Wall -W -Werror -o testsign test.cc
    test.cc:7:11: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]
        if (x == y*y) {
            ~ ^  ~~~
    1 error generated.
    

    If I say if (x*x == y*y), g++ stops complaining though.

  6. maflcko commented at 1:41 PM on July 2, 2020: member

    Will reopen the next time I run into this again

  7. maflcko closed this on Jul 2, 2020

  8. maflcko commented at 1:30 PM on July 11, 2020: member

    Steps to reproduce on commit 5f96bce9b7f38c687817d58e8b54a5b7ebfe91b3

    Clang

    Options used to compile and link:
      multiprocess  = no
      with wallet   = yes
      with gui / qt = yes
        with qr     = yes
      with zmq      = yes
      with test     = yes
        with fuzz   = no
      with bench    = yes
      with upnp     = no
      use asm       = yes
      sanitizers    = 
      debug enabled = no
      gprof enabled = no
      werror        = no
    
      target os     = linux
      build os      = linux-gnu
    
      CC            = /usr/bin/ccache clang
      CFLAGS        = -g -O2
      CPPFLAGS      =   -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2  -DHAVE_BUILD_INFO -D__STDC_FORMAT_MACROS
      CXX           = /usr/bin/ccache clang++ -std=c++17
      CXXFLAGS      =   -Wstack-protector -fstack-protector-all  -Wall -Wextra -Wgnu -Wformat -Wvla -Wshadow-field -Wswitch -Wformat-security -Wthread-safety -Wrange-loop-analysis -Wredundant-decls -Wunused-variable -Wdate-time -Wconditional-uninitialized -Wsign-compare -Wunreachable-code-loop-increment  -Wno-unused-parameter -Wno-self-assign -Wno-unused-local-typedef -Wno-deprecated-register -Wno-implicit-fallthrough -Wno-deprecated-copy   -g -O2
      LDFLAGS       = -pthread  -Wl,-z,relro -Wl,-z,now -pie  
      ARFLAGS       = cr
    

    GCC

    Options used to compile and link:
      multiprocess  = no
      with wallet   = yes
      with gui / qt = yes
        with qr     = yes
      with zmq      = yes
      with test     = yes
        with fuzz   = no
      with bench    = yes
      with upnp     = no
      use asm       = yes
      sanitizers    = 
      debug enabled = no
      gprof enabled = no
      werror        = no
    
      target os     = linux
      build os      = linux-gnu
    
      CC            = /usr/bin/ccache gcc
      CFLAGS        = -g -O2
      CPPFLAGS      =   -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2  -DHAVE_BUILD_INFO -D__STDC_FORMAT_MACROS
      CXX           = /usr/bin/ccache g++ -std=c++11
      CXXFLAGS      =   -fstack-reuse=none -Wstack-protector -fstack-protector-all  -Wall -Wextra -Wformat -Wvla -Wswitch -Wredundant-decls -Wunused-variable -Wdate-time -Wsign-compare -Wsuggest-override  -Wno-unused-parameter -Wno-implicit-fallthrough -Wno-deprecated-copy   -g -O2 -fno-extended-identifiers
      LDFLAGS       = -pthread  -Wl,-z,relro -Wl,-z,now -pie  
      ARFLAGS       = cr
    
    
  9. maflcko reopened this on Jul 11, 2020

  10. ajtowns commented at 2:48 AM on July 13, 2020: contributor

    If I checkout 5f96bce9b7f38c687817d58e8b54a5b7ebfe91b3 and apply

    $ git diff
    diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp
    index 0f7c3e5b39..54a2b53182 100644
    --- a/src/wallet/test/wallet_tests.cpp
    +++ b/src/wallet/test/wallet_tests.cpp
    @@ -812,6 +812,11 @@ BOOST_FIXTURE_TEST_CASE(ZapSelectTx, TestChain100Setup)
     
             LOCK(wallet->cs_wallet);
             BOOST_CHECK(wallet->HasWalletSpend(prev_hash));
    +        int x = 1;
    +        if (wallet->mapWallet.count(block_hash) == 1) (void)0;
    +        if (wallet->mapWallet.count(block_hash) == (int)1) (void)0;
    +        if (wallet->mapWallet.count(block_hash) == x) (void)0;
    +        if (wallet->mapWallet.count(block_hash) == -1) (void)0;
             BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_hash), 1);
     
             std::vector<uint256> vHashIn{ block_hash }, vHashOut;
    

    Then ./configure CC=clang CXX=clang++ --with-incompatible-bdb --enable-zmq --without-miniupnpc --enable-c++17 (clang 9.0.1) I get the following errors:

    wallet/test/wallet_tests.cpp:818:49: warning: comparison of integers of different signs: 'std::map<uint256, CWalletTx, std::less<uint256>, std::allocator<std::pair<const uint256, CWalletTx> > >::size_type' (aka 'unsigned long') and 'int' [-Wsign-compare]
            if (wallet->mapWallet.count(block_hash) == x) (void)0;
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~
    wallet/test/wallet_tests.cpp:819:49: warning: comparison of integers of different signs: 'std::map<uint256, CWalletTx, std::less<uint256>, std::allocator<std::pair<const uint256, CWalletTx> > >::size_type' (aka 'unsigned long') and 'int' [-Wsign-compare]
            if (wallet->mapWallet.count(block_hash) == -1) (void)0;
    

    same errors with gcc (9.3.0), give or take some wording. The two travis reports reportedly have clang version 7.0.0, and Apple clang 11.0.0, both of which I think are Xcode versions rather than llvm versions. Wikipedia claims xcode clang 11.0.0 corresponds to llvm clang 8.0.0, but trying clang 8 and clang 7 in Debian, and clang 6 in bionic gives me the same collection of errors as above, so I think this must be some Xcode specific thing, where other compilers are able to figure out the value is a constant that's always in range, and just emit correct code, rather than a warning?

  11. Empact commented at 4:24 PM on June 9, 2021: member

    I'm currently running Mac OS 10.15.7 (19H1217)

    $ /usr/local/bin/ccache gcc --version
    Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/c++/4.2.1
    Apple clang version 12.0.0 (clang-1200.0.32.29)
    Target: x86_64-apple-darwin19.6.0
    Thread model: posix
    InstalledDir: /Library/Developer/CommandLineTools/usr/bin
    
    Options used to compile and link:
      external signer = no
      multiprocess    = no
      with libs       = yes
      with wallet     = yes
        with sqlite   = yes
        with bdb      = yes
      with gui / qt   = yes
        with qr       = yes
      with zmq        = no
      with test       = yes
      with bench      = yes
      with upnp       = yes
      with natpmp     = no
      use asm         = yes
      ebpf tracing    = yes
      sanitizers      = 
      debug enabled   = no
      gprof enabled   = no
      werror          = no
    
      target os       = darwin
      build os        = darwin19.6.0
    
      CC              = /usr/local/bin/ccache gcc
      CFLAGS          = -pthread -g -O2
      CPPFLAGS        =  -fmacro-prefix-map=$(abs_srcdir)=.  -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2  -DHAVE_BUILD_INFO -D__STDC_FORMAT_MACROS -DMAC_OSX -DOBJC_OLD_DISPATCH_PROTOTYPES=0 -DPROVIDE_FUZZ_MAIN_FUNCTION
      CXX             = /usr/local/bin/ccache g++ -std=c++17
      CXXFLAGS        =  -fdebug-prefix-map=$(abs_srcdir)=.  -Wstack-protector -fstack-protector-all -fcf-protection=full  -Wall -Wextra -Wgnu -Wformat -Wformat-security -Wvla -Wshadow-field -Wswitch -Wthread-safety -Wrange-loop-analysis -Wredundant-decls -Wunused-variable -Wunused-member-function -Wdate-time -Wconditional-uninitialized -Wsign-compare -Woverloaded-virtual -Wunreachable-code-loop-increment  -Wno-unused-parameter -Wno-self-assign -Wno-unused-local-typedef -Wno-implicit-fallthrough   -g -O2
      LDFLAGS         = -lpthread  -Wl,-bind_at_load   -Wl,-headerpad_max_install_names -Wl,-dead_strip -Wl,-dead_strip_dylibs
      ARFLAGS         = cr
    

    The above setting show warnings when building 93e38d5c06d9b32326585121b46bb59041d7cfed.

  12. maflcko commented at 6:26 AM on July 9, 2021: member

    According to #22176 (comment) this can be reproduced on OpenBSD 6.9, clang 10.0.1

  13. maflcko closed this on Jan 17, 2022

  14. bitcoin locked this on Jan 17, 2023
  15. bitcoin unlocked this on Jun 15, 2023
  16. maflcko commented at 3:57 PM on June 15, 2023: member

    Another way to reproduce (at least for 32-bit) would be:

    ( cd depends && make HOST=i686-pc-linux-gnu CC='clang -m32' CXX='clang++ -m32 -Wsign-compare' NO_QT=1 NO_WALLET=1 NO_ZMQ=1 NO_UPNP=1 NO_NATPMP=1 -j $(nproc) ) && ./autogen.sh && CONFIG_SITE="$PWD/depends/i686-pc-linux-gnu/share/config.site" ./configure && make -j $(nproc) src/bitcoind

  17. bitcoin locked this on Jun 14, 2024

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-24 09:16 UTC

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