Skip to content

Commit a8191cb

Browse files
committed
Changes related to backport bitcoin#18843
1 parent 728bcde commit a8191cb

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/random.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ static uint64_t GetRdRand() noexcept
132132
// RdRand may very rarely fail. Invoke it up to 10 times in a loop to reduce this risk.
133133
#ifdef __i386__
134134
uint8_t ok;
135-
uint32_t r1, r2;
135+
// Initialize to 0 to silence a compiler warning that r1 or r2 may be used
136+
// uninitialized. Even if rdrand fails (!ok) it will set the output to 0,
137+
// but there is no way that the compiler could know that.
138+
uint32_t r1 = 0, r2 = 0;
136139
for (int i = 0; i < 10; ++i) {
137140
__asm__ volatile (".byte 0x0f, 0xc7, 0xf0; setc %1" : "=a"(r1), "=q"(ok) :: "cc"); // rdrand %eax
138141
if (ok) break;
@@ -144,7 +147,7 @@ static uint64_t GetRdRand() noexcept
144147
return (((uint64_t)r2) << 32) | r1;
145148
#elif defined(__x86_64__) || defined(__amd64__)
146149
uint8_t ok;
147-
uint64_t r1;
150+
uint64_t r1 = 0; // See above why we initialize to 0.
148151
for (int i = 0; i < 10; ++i) {
149152
__asm__ volatile (".byte 0x48, 0x0f, 0xc7, 0xf0; setc %1" : "=a"(r1), "=q"(ok) :: "cc"); // rdrand %rax
150153
if (ok) break;

0 commit comments

Comments
 (0)