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:

    0src/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: member

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

    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. 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
  10. 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 :)
  11. refactor: Use uint16_t instead of unsigned short
    removed trailing whitespace to make linter happy
    1cabbddbca
  12. renepickhardt force-pushed on Jun 22, 2020
  13. 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

  14. 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.

  15. 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 :)

    0$ 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)"
    1src/httpserver.cpp:    int workQueueDepth = std::max((long)gArgs.GetArg("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE), 1L);
    2src/httpserver.cpp:    int rpcThreads = std::max((long)gArgs.GetArg("-rpcthreads", DEFAULT_HTTP_THREADS), 1L);
    3src/rest.cpp:            if (headers.size() == (unsigned long)count)
    4src/streams.h:        long nLongPos = nPos;
    5src/txmempool.cpp:                if (coin.IsSpent() || (coin.IsCoinBase() && ((signed long)nMemPoolHeight) - coin.nHeight < COINBASE_MATURITY)) {
    6src/txmempool.h:    unsigned long size() const
    
  16. 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…
  17. 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.
  18. 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.

  19. 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?
  20. 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?

  21. fanquake removed the label GUI on Jul 9, 2020
  22. 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.

  23. hebasto approved
  24. 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.

  25. laanwj merged this on Jul 9, 2020
  26. laanwj closed this on Jul 9, 2020

  27. sidhujag referenced this in commit 4454ed1cb1 on Jul 9, 2020
  28. random-zebra referenced this in commit 85f000ecbe on Jul 30, 2021
  29. random-zebra referenced this in commit b4751e10ce on Aug 11, 2021
  30. Fabcien referenced this in commit 6f8205e070 on Sep 1, 2021
  31. DrahtBot 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: 2024-11-17 12:12 UTC

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