Avoid static analyzer warnings regarding uninitialized arguments #10735

pull practicalswift wants to merge 1 commits into bitcoin:master from practicalswift:std-array changing 2 files +23 −14
  1. practicalswift commented at 3:50 PM on July 3, 2017: contributor

    Avoid static analyzer warnings regarding "Function call argument is a pointer to uninitialized value" in cases where we are intentionally using such arguments.

    This is achieved by using f(b.begin(), b.end()) (std::array<char, N>) instead of f(b, b + N) (char b[N]).

    Rationale:

    • Reduce false positives by guiding static analyzers regarding our intentions.

    Before this commit:

    $ clang-tidy-3.5 -checks=* src/bench/base58.cpp
    bench/base58.cpp:23:9: warning: Function call argument is a pointer to uninitialized value [clang-analyzer-core.CallAndMessage]
            EncodeBase58(b, b + 32);
            ^
    $ clang-tidy-3.5 -checks=* src/bench/verify_script.cpp
    bench/verify_script.cpp:59:5: warning: Function call argument is a pointer to uninitialized value [clang-analyzer-core.CallAndMessage]
        key.Set(vchKey, vchKey + 32, false);
        ^
    $
    

    After this commit:

    $ clang-tidy-3.5 -checks=* src/bench/base58.cpp
    $ clang-tidy-3.5 -checks=* src/bench/verify_script.cpp
    $
    
  2. in src/bench/base58.cpp:16 in 397af13a14 outdated
      12 | @@ -13,28 +13,30 @@
      13 |  
      14 |  static void Base58Encode(benchmark::State& state)
      15 |  {
      16 | -    unsigned char buff[32] = {
      17 | -        17, 79, 8, 99, 150, 189, 208, 162, 22, 23, 203, 163, 36, 58, 147,
      18 | -        227, 139, 2, 215, 100, 91, 38, 11, 141, 253, 40, 117, 21, 16, 90,
      19 | -        200, 24
      20 | +    std::array<unsigned char, 32> buff = {
    


    sipa commented at 3:56 PM on July 3, 2017:

    While you're at it, make these static const ?


    practicalswift commented at 8:10 PM on July 3, 2017:

    @sipa Good point! Fixed! :-)


    TheBlueMatt commented at 7:18 PM on July 14, 2017:

    tiny nit: I might prefer this if it kept the benchmarked-against data on stack instead of heap, but it doesnt matter much.


    sipa commented at 7:20 PM on July 14, 2017:

    std::array stores on the stack


    TheBlueMatt commented at 10:48 PM on July 14, 2017:

    static implies not-on-stack, no?


    sipa commented at 1:41 AM on July 15, 2017:

    @TheBlueMatt static const is just statically allocated by the binary, not even on the heap.


    TheBlueMatt commented at 1:57 AM on July 15, 2017:

    Yes, indeed, my point was to prefer stack over binary or other allocations. It shouldnt matter cause the memory usage of these benchmarks should be trivial, so whatever, it doesnt matter.

  3. practicalswift force-pushed on Jul 3, 2017
  4. fanquake added the label Refactoring on Jul 5, 2017
  5. jonasschnelli commented at 7:27 AM on July 13, 2017: contributor

    utACK dcc0e0f2c93888f904001703a7ef6be11f61a9e4

  6. paveljanik commented at 8:36 AM on July 13, 2017: contributor

    OS X, Apple LLVM version 7.0.2:

      CXX      bench/bench_bench_bitcoin-verify_script.o
    bench/verify_script.cpp:58:48: error: implicit instantiation of undefined template 'std::__1::array<unsigned char, 32>'
        static const std::array<unsigned char, 32> vchKey = {
                                                   ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__tuple:95:65: note: template is declared here
    template <class _Tp, size_t _Size> struct _LIBCPP_TYPE_VIS_ONLY array;
                                                                    ^
    1 error generated.
    
  7. Avoid static analyzer warnings regarding uninitialized arguments
    Avoid static analyzer warnings regarding "Function call argument
    is a pointer to uninitialized value" in cases where we are
    intentionally using such arguments.
    
    This is achieved by using ...
    
    `f(b.begin(), b.end())` (`std::array<char, N>`)
    
    ... instead of ...
    
    `f(b, b + N)` (`char b[N]`)
    
    Rationale:
    * Reduce false positives by guiding static analyzers regarding our
      intentions.
    
    Before this commit:
    
    ```
    $ clang-tidy-3.5 -checks=* src/bench/base58.cpp
    bench/base58.cpp:23:9: warning: Function call argument is a pointer to uninitialized value [clang-analyzer-core.CallAndMessage]
            EncodeBase58(b, b + 32);
            ^
    $ clang-tidy-3.5 -checks=* src/bench/verify_script.cpp
    bench/verify_script.cpp:59:5: warning: Function call argument is a pointer to uninitialized value [clang-analyzer-core.CallAndMessage]
        key.Set(vchKey, vchKey + 32, false);
        ^
    $
    ```
    
    After this commit:
    
    ```
    $ clang-tidy-3.5 -checks=* src/bench/base58.cpp
    $ clang-tidy-3.5 -checks=* src/bench/verify_script.cpp
    $
    ```
    6835cb0ab2
  8. practicalswift force-pushed on Jul 15, 2017
  9. practicalswift commented at 12:27 PM on July 15, 2017: contributor

    @paveljanik Missing #include <array> added. Thanks for reviewing! :-)

  10. sipa commented at 6:02 PM on July 15, 2017: member

    utACK 6835cb0ab26c913423cc2307c989579d05aabdcb

  11. TheBlueMatt commented at 3:19 PM on July 16, 2017: member

    utACK 6835cb0ab26c913423cc2307c989579d05aabdcb

  12. sipa merged this on Jul 16, 2017
  13. sipa closed this on Jul 16, 2017

  14. sipa referenced this in commit 565494619d on Jul 16, 2017
  15. PastaPastaPasta referenced this in commit 1f6222d778 on Jul 6, 2019
  16. PastaPastaPasta referenced this in commit d8e2ec1a6c on Jul 8, 2019
  17. PastaPastaPasta referenced this in commit 89bdc6b1f8 on Jul 9, 2019
  18. PastaPastaPasta referenced this in commit 17ff25e6a0 on Jul 11, 2019
  19. PastaPastaPasta referenced this in commit 24ad679155 on Jul 13, 2019
  20. PastaPastaPasta referenced this in commit dd1a8cce37 on Jul 13, 2019
  21. PastaPastaPasta referenced this in commit ef3331f4ae on Jul 17, 2019
  22. PastaPastaPasta referenced this in commit 2a6e46794d on Jul 17, 2019
  23. PastaPastaPasta referenced this in commit 188f4a7522 on Jul 18, 2019
  24. barrystyle referenced this in commit f8241ca5ca on Jan 22, 2020
  25. zkbot referenced this in commit aa225ebb0b on Jan 24, 2020
  26. zkbot referenced this in commit 74ff73abab on Jan 24, 2020
  27. practicalswift deleted the branch on Apr 10, 2021
  28. DrahtBot 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-04-16 15:15 UTC

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