Skip to content

Commit 5654946

Browse files
committed
Merge #10735: Avoid static analyzer warnings regarding uninitialized arguments
6835cb0 Avoid static analyzer warnings regarding uninitialized arguments (practicalswift) Pull request description: 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: ```shell $ 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: ```shell $ clang-tidy-3.5 -checks=* src/bench/base58.cpp $ clang-tidy-3.5 -checks=* src/bench/verify_script.cpp $ ``` Tree-SHA512: 5814a320ca8b959d0954bb64393424bcad73f942d2e988de1cd6788f39153b93900325532f2e340de02d740a3953385d212ae08e7ec72bb4c394a40475f251df
2 parents ad6fce6 + 6835cb0 commit 5654946

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/bench/base58.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,37 @@
77
#include "validation.h"
88
#include "base58.h"
99

10+
#include <array>
1011
#include <vector>
1112
#include <string>
1213

1314

1415
static void Base58Encode(benchmark::State& state)
1516
{
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
17+
static const std::array<unsigned char, 32> buff = {
18+
{
19+
17, 79, 8, 99, 150, 189, 208, 162, 22, 23, 203, 163, 36, 58, 147,
20+
227, 139, 2, 215, 100, 91, 38, 11, 141, 253, 40, 117, 21, 16, 90,
21+
200, 24
22+
}
2023
};
21-
unsigned char* b = buff;
2224
while (state.KeepRunning()) {
23-
EncodeBase58(b, b + 32);
25+
EncodeBase58(buff.begin(), buff.end());
2426
}
2527
}
2628

2729

2830
static void Base58CheckEncode(benchmark::State& state)
2931
{
30-
unsigned char buff[32] = {
31-
17, 79, 8, 99, 150, 189, 208, 162, 22, 23, 203, 163, 36, 58, 147,
32-
227, 139, 2, 215, 100, 91, 38, 11, 141, 253, 40, 117, 21, 16, 90,
33-
200, 24
32+
static const std::array<unsigned char, 32> buff = {
33+
{
34+
17, 79, 8, 99, 150, 189, 208, 162, 22, 23, 203, 163, 36, 58, 147,
35+
227, 139, 2, 215, 100, 91, 38, 11, 141, 253, 40, 117, 21, 16, 90,
36+
200, 24
37+
}
3438
};
35-
unsigned char* b = buff;
3639
std::vector<unsigned char> vch;
37-
vch.assign(b, b + 32);
40+
vch.assign(buff.begin(), buff.end());
3841
while (state.KeepRunning()) {
3942
EncodeBase58Check(vch);
4043
}

src/bench/verify_script.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "script/sign.h"
1212
#include "streams.h"
1313

14+
#include <array>
15+
1416
// FIXME: Dedup with BuildCreditingTransaction in test/script_tests.cpp.
1517
static CMutableTransaction BuildCreditingTransaction(const CScript& scriptPubKey)
1618
{
@@ -55,8 +57,12 @@ static void VerifyScriptBench(benchmark::State& state)
5557

5658
// Keypair.
5759
CKey key;
58-
const unsigned char vchKey[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
59-
key.Set(vchKey, vchKey + 32, false);
60+
static const std::array<unsigned char, 32> vchKey = {
61+
{
62+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
63+
}
64+
};
65+
key.Set(vchKey.begin(), vchKey.end(), false);
6066
CPubKey pubkey = key.GetPubKey();
6167
uint160 pubkeyHash;
6268
CHash160().Write(pubkey.begin(), pubkey.size()).Finalize(pubkeyHash.begin());

0 commit comments

Comments
 (0)