refactor: Use uint16_t instead of unsigned short #19314

pull renepickhardt wants to merge 1 commits into bitcoin:master from renepickhardt:akh_uint16_t changing 9 files +28 −22
  1. renepickhardt commented at 8:21 PM on June 17, 2020: none

    I wanted to see if the up for grabs label works and looked at PR #17822 originally opend by ahook I saw it had many acks for example by jonatack and practicalswift but needed rebasing.

    So I checked out the remote branch rebased it resolved three conflicts and continued the rebase.

    Hope everything is as expected (:

  2. practicalswift commented at 8:29 PM on June 17, 2020: contributor

    Concept ACK: explicit is better than implicit.

    Warm welcome as a contributor @renepickhardt - I'm a big fan of your Lightning work etc :) Looking forward to reading Mastering the Lightning Network once it is released!

    FWIW, we are already assuming that sizeof(uint16_t) == sizeof(unsigned short) implicitly in multiple places like here …

    https://github.com/bitcoin/bitcoin/blob/35ed88f187c9bc7eedc3a1cf12193e0fbf222057/src/serialize.h#L265-L278

    … and also explicitly in assumptions.h

    https://github.com/bitcoin/bitcoin/blob/35ed88f187c9bc7eedc3a1cf12193e0fbf222057/src/compat/assumptions.h#L51

  3. renepickhardt commented at 8:29 PM on June 17, 2020: none

    btw running git grep "unsigned short" results in:

    src/secp256k1/src/tests.c:            unsigned short sh;
    

    I could make another commit to this branch where I also changed that but I am not sure if the bitcoin repo is the place to patch libsecp256k1 or if that should be done in https://github.com/bitcoin-core/secp256k1

    at least the remote file https://github.com/bitcoin-core/secp256k1/blob/master/src/tests.c has 19 contributers according to git blame where in this repo it only has contributes from @sipa and @laanwj

  4. DrahtBot added the label GUI on Jun 17, 2020
  5. DrahtBot added the label P2P on Jun 17, 2020
  6. DrahtBot added the label Refactoring on Jun 17, 2020
  7. DrahtBot added the label Tests on Jun 17, 2020
  8. DrahtBot commented at 11:11 PM on June 17, 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:

    • #19415 (net: Make DNS lookup mockable, add fuzzing harness by practicalswift)
    • #19203 (net: Add regression fuzz harness for CVE-2017-18350. Add FuzzedSocket. Add thin SOCKET wrapper. by practicalswift)
    • #19031 (Implement ADDRv2 support (part of BIP155) by vasild)
    • #18991 (Cache responses to GETADDR to prevent topology leaks by naumenkogs)
    • #18242 (Add BIP324 encrypted p2p transport de-/serializer (only used in tests) by jonasschnelli)
    • #18077 (net: Add NAT-PMP port forwarding support by hebasto)
    • #17877 (qt, refactor: Make enums in BitcoinUnits class scoped by hebasto)
    • #17428 (p2p: Try to preserve outbound block-relay-only connections during restart by hebasto)

    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 Jun 18, 2020 from issue net: Add regression fuzz harness for CVE-2017-18350. Add FuzzedSocket. by practicalswift
  10. DrahtBot cross-referenced this on Jun 18, 2020 from issue Implement ADDRv2 support (part of BIP155) by vasild
  11. DrahtBot cross-referenced this on Jun 18, 2020 from issue Cache responses to GETADDR to prevent topology leaks by naumenkogs
  12. DrahtBot cross-referenced this on Jun 18, 2020 from issue The Zero Allocations project by jb55
  13. DrahtBot cross-referenced this on Jun 18, 2020 from issue Add BIP324 encrypted p2p transport de-/serializer (only used in tests) by jonasschnelli
  14. DrahtBot cross-referenced this on Jun 18, 2020 from issue net: Add NAT-PMP port forwarding support by hebasto
  15. DrahtBot cross-referenced this on Jun 18, 2020 from issue qt, refactor: Make BitcoinUnits::Unit a scoped enum by hebasto
  16. DrahtBot cross-referenced this on Jun 18, 2020 from issue p2p: Try to preserve outbound block-relay-only connections during restart by hebasto
  17. in src/qt/optionsmodel.h:9 in d30edc4e74 outdated
       5 | @@ -6,6 +6,7 @@
       6 |  #define BITCOIN_QT_OPTIONSMODEL_H
       7 |  
       8 |  #include <amount.h>
       9 | +#include <cstdint> 
    


    adamjonas commented at 3:39 PM on June 18, 2020:

    It looks like the linter is calling out the trailing whitespace here.


    renepickhardt commented at 5:57 PM on June 18, 2020:

    fixed

  18. practicalswift commented at 9:10 PM on June 21, 2020: contributor

    @renepickhardt Would you mind squashing? I think this one should be ready for final review after squash :)

  19. refactor: Use uint16_t instead of unsigned short
    removed trailing whitespace to make linter happy
    1cabbddbca
  20. renepickhardt force-pushed on Jun 22, 2020
  21. renepickhardt commented at 10:15 AM on June 22, 2020: none

    Ok so I have squashed the two commits and rebased to current master to make merging easier (:

    if there is anything more to do let me know @practicalswift

  22. sipsorcery commented at 8:15 PM on June 23, 2020: member

    ACK 1cabbddbca615b26aa4510c75f459c28d6fe0afd.

    Nice to replace a C header with a C++ header as well.

  23. practicalswift commented at 8:49 PM on June 23, 2020: contributor

    ACK 1cabbddbca615b26aa4510c75f459c28d6fe0afd -- patch looks correct :)

    If anyone wants to follow up by checking potentially unintended/unwarranted uses of long this command might be helpful to find candidates :)

    $ git grep -E '[^a-zA-Z_."]long[^a-zA-Z_."]' -- "*.cpp" "*.h" ":(exclude)src/univalue/" ":(exclude)src/qt/" ":(exclude)src/test/" ":(exclude)src/crc32c/" ":(exclude)src/crypto/common.h" ":(exclude)src/randomenv.cpp" | grep -vE '^[^ ]*:.*[/*]' | grep -vE "(too.long|ftell|strtoul|strol|strtol|how long|really long|fseek|as long)"
    src/httpserver.cpp:    int workQueueDepth = std::max((long)gArgs.GetArg("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE), 1L);
    src/httpserver.cpp:    int rpcThreads = std::max((long)gArgs.GetArg("-rpcthreads", DEFAULT_HTTP_THREADS), 1L);
    src/rest.cpp:            if (headers.size() == (unsigned long)count)
    src/streams.h:        long nLongPos = nPos;
    src/txmempool.cpp:                if (coin.IsSpent() || (coin.IsCoinBase() && ((signed long)nMemPoolHeight) - coin.nHeight < COINBASE_MATURITY)) {
    src/txmempool.h:    unsigned long size() const
    
  24. luke-jr commented at 1:34 AM on June 24, 2020: member

    Note the (mentioned in comments only, not an issue for this commit) libsecp256k1 unsigned short does in fact need to be an unsigned short...

  25. in src/addrdb.cpp:42 in 1cabbddbca
      36 | @@ -36,7 +37,7 @@ template <typename Data>
      37 |  bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data& data)
      38 |  {
      39 |      // Generate random temporary filename
      40 | -    unsigned short randv = 0;
      41 | +    uint16_t randv = 0;
      42 |      GetRandBytes((unsigned char*)&randv, sizeof(randv));
      43 |      std::string tmpfn = strprintf("%s.%04x", prefix, randv);
    


    luke-jr commented at 1:35 AM on June 24, 2020:

    This is okay only because strprintf has some type magic.


    laanwj commented at 2:31 PM on July 9, 2020:

    Yes, this isn't a problem, strprintf is type safe. There is never a need for size specifiers.

  26. in src/net.cpp:116 in 1cabbddbca
     110 | @@ -110,9 +111,9 @@ void CConnman::AddOneShot(const std::string& strDest)
     111 |      vOneShots.push_back(strDest);
     112 |  }
     113 |  
     114 | -unsigned short GetListenPort()
     115 | +uint16_t GetListenPort()
     116 |  {
     117 | -    return (unsigned short)(gArgs.GetArg("-port", Params().GetDefaultPort()));
     118 | +    return (uint16_t)(gArgs.GetArg("-port", Params().GetDefaultPort()));
    


    luke-jr commented at 1:36 AM on June 24, 2020:

    Can we just drop the cast entirely? Not sure I see the point...


    practicalswift commented at 5:40 AM on June 24, 2020:

    The point is to make a potentially sign-changing narrowing conversion explicit.

    GetArg returns int64_t so it makes sense to make the implicit conversion to uint16_t explicit using a cast.

  27. in src/netbase.cpp:802 in 1cabbddbca
     798 | @@ -798,11 +799,11 @@ bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int
     799 |          ProxyCredentials random_auth;
     800 |          static std::atomic_int counter(0);
     801 |          random_auth.username = random_auth.password = strprintf("%i", counter++);
     802 | -        if (!Socks5(strDest, (unsigned short)port, &random_auth, hSocket)) {
     803 | +        if (!Socks5(strDest, (uint16_t)port, &random_auth, hSocket)) {
    


    luke-jr commented at 1:38 AM on June 24, 2020:

    Why not change the type of port?

  28. hebasto commented at 11:50 AM on June 29, 2020: member

    Approach ACK 1cabbddbca615b26aa4510c75f459c28d6fe0afd.

    In addition to @luke-jr's comment, could the type of the port parameter in the Socks5() function be changed from int to uint16_t?

  29. DrahtBot cross-referenced this on Jun 30, 2020 from issue net: Make DNS lookup mockable, add fuzzing harness by practicalswift
  30. fanquake removed the label GUI on Jul 9, 2020
  31. laanwj commented at 2:36 PM on July 9, 2020: member

    ACK 1cabbddbca615b26aa4510c75f459c28d6fe0afd After this, the only mention of unsigned short left is in the secp256k1 tests, which should not be changed here

    In addition to @luke-jr's comment, could the type of the port parameter in the Socks5() function be changed from int to uint16_t?

    I understand why @renepickhardt didn't do this. Right now, this is a pure refactor, only changing a type to a typedef of the same type (for, in practice, all architectures supported by bitcoin). Changing the parameter type would be an actual code change.

  32. hebasto approved
  33. hebasto commented at 2:46 PM on July 9, 2020: member

    In addition to @luke-jr's comment, could the type of the port parameter in the Socks5() function be changed from int to uint16_t?

    I understand why @renepickhardt didn't do this. Right now, this is a pure refactor, only changing a type to a typedef of the same type (for, in practice, all architectures supported by bitcoin). Changing the parameter type would be an actual code change.

    Agree.

    ACK 1cabbddbca615b26aa4510c75f459c28d6fe0afd, I have reviewed the code and it looks OK, I agree it can be merged.

  34. laanwj merged this on Jul 9, 2020
  35. laanwj closed this on Jul 9, 2020

  36. sidhujag referenced this in commit 4454ed1cb1 on Jul 9, 2020
  37. furszy cross-referenced this on Jun 7, 2021 from issue Road to Tor v3 support (BIP155) by furszy
  38. furszy cross-referenced this on Jul 15, 2021 from issue [Net] asmap to improve IP bucketing in addrman - backports by furszy
  39. random-zebra referenced this in commit 85f000ecbe on Jul 30, 2021
  40. random-zebra referenced this in commit b4751e10ce on Aug 11, 2021
  41. Fabcien referenced this in commit 6f8205e070 on Sep 1, 2021
  42. bitcoin 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: 2026-05-19 06:53 UTC