Skip to content

Commit 9c7bed4

Browse files
vasildPiRK
authored andcommitted
build: warn on potentially uninitialized reads
Summary: > Enable -Wconditional-uninitialized to warn on potentially uninitialized reads. > > Fix the sole such warning in Bitcoin Core in GetRdRand(): r1 would be > set to 0 on rdrand failure, so initializing it to 0 is a non-functional > change. > > From "Intel 64 and IA-32 ArchitecturesSoftware Developer's Manual" [1], > page 1711: "CF=1 indicates that the data in the destination is valid. > Otherwise CF=0 and the data in the destination operand will be returned > as zeros for the specified width." > > [1] https://software.intel.com/sites/default/files/managed/39/c5/325462-sdm-vol-1-2abcd-3abcd.pdf This is a backport of Core [[bitcoin/bitcoin#18843 | PR18843]] Test Plan: ``` cmake .. -GNinja \ -DCMAKE_C_COMPILER=clang \ -DCMAKE_CXX_COMPILER=clang++ \ -DENABLE_CLANG_TIDY=ON \ -DCMAKE_C_FLAGS="-Werror" ninja all check-all ``` Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D9110
1 parent b0d8c60 commit 9c7bed4

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ add_compiler_flags(
228228
-Wredundant-decls
229229
-Wunreachable-code-loop-increment
230230
-Wsign-compare
231+
-Wconditional-uninitialized
231232
)
232233
add_compiler_flag_group(-Wformat -Wformat-security)
233234
add_cxx_compiler_flags(

src/leveldb/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_compiler_flags(
1616
-Wno-shadow
1717
-Wno-sign-compare
1818
-Wno-unused-const-variable
19+
-Wno-conditional-uninitialized
1920
)
2021
add_c_compiler_flags(-Wno-strict-prototypes)
2122

src/random.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ static uint64_t GetRdRand() noexcept {
119119
// this risk.
120120
#ifdef __i386__
121121
uint8_t ok;
122-
uint32_t r1, r2;
122+
// Initialize to 0 to silence a compiler warning that r1 or r2 may be used
123+
// uninitialized. Even if rdrand fails (!ok) it will set the output to 0,
124+
// but there is no way that the compiler could know that.
125+
uint32_t r1 = 0, r2 = 0;
123126
for (int i = 0; i < 10; ++i) {
124127
// rdrand %eax
125128
__asm__ volatile(".byte 0x0f, 0xc7, 0xf0; setc %1"
@@ -139,7 +142,7 @@ static uint64_t GetRdRand() noexcept {
139142
return (uint64_t(r2) << 32) | r1;
140143
#elif defined(__x86_64__) || defined(__amd64__)
141144
uint8_t ok;
142-
uint64_t r1;
145+
uint64_t r1 = 0; // See above why we initialize to 0.
143146
for (int i = 0; i < 10; ++i) {
144147
// rdrand %rax
145148
__asm__ volatile(".byte 0x48, 0x0f, 0xc7, 0xf0; setc %1"

0 commit comments

Comments
 (0)