Skip to content

Commit 254fd89

Browse files
committed
Merge bitcoin/bitcoin#31863: random: Initialize variables in hardware RNG functions
99755e0 random: Initialize variables in hardware RNG functions (Eval EXEC) Pull request description: See: bitcoin/bitcoin#31826 (comment) , So this PR want to prevent potential uninitialized value issues and improve code clarity. ACKs for top commit: sipa: utACK 99755e0 achow101: ACK 99755e0 Tree-SHA512: 4cf9c214617769cf051b4f36453275b407e37d96315b6a206102d17019375b3834ba07e2ccb28c7650c90ff8e1f1034522fccafaa33e136dfe63cc68396a1f6e
2 parents 75f8396 + 99755e0 commit 254fd89

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/random.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ uint64_t GetRdRand() noexcept
126126
{
127127
// RdRand may very rarely fail. Invoke it up to 10 times in a loop to reduce this risk.
128128
#ifdef __i386__
129-
uint8_t ok;
129+
uint8_t ok = 0;
130130
// Initialize to 0 to silence a compiler warning that r1 or r2 may be used
131131
// uninitialized. Even if rdrand fails (!ok) it will set the output to 0,
132132
// but there is no way that the compiler could know that.
@@ -141,7 +141,7 @@ uint64_t GetRdRand() noexcept
141141
}
142142
return (((uint64_t)r2) << 32) | r1;
143143
#elif defined(__x86_64__) || defined(__amd64__)
144-
uint8_t ok;
144+
uint8_t ok = 0;
145145
uint64_t r1 = 0; // See above why we initialize to 0.
146146
for (int i = 0; i < 10; ++i) {
147147
__asm__ volatile (".byte 0x48, 0x0f, 0xc7, 0xf0; setc %1" : "=a"(r1), "=q"(ok) :: "cc"); // rdrand %rax
@@ -162,7 +162,7 @@ uint64_t GetRdSeed() noexcept
162162
// RdSeed may fail when the HW RNG is overloaded. Loop indefinitely until enough entropy is gathered,
163163
// but pause after every failure.
164164
#ifdef __i386__
165-
uint8_t ok;
165+
uint8_t ok = 0;
166166
uint32_t r1, r2;
167167
do {
168168
__asm__ volatile (".byte 0x0f, 0xc7, 0xf8; setc %1" : "=a"(r1), "=q"(ok) :: "cc"); // rdseed %eax
@@ -215,7 +215,7 @@ void ReportHardwareRand()
215215
*/
216216
uint64_t GetRNDR() noexcept
217217
{
218-
uint8_t ok;
218+
uint8_t ok = 0;
219219
uint64_t r1;
220220
do {
221221
// https://developer.arm.com/documentation/ddi0601/2022-12/AArch64-Registers/RNDR--Random-Number
@@ -233,7 +233,7 @@ uint64_t GetRNDR() noexcept
233233
*/
234234
uint64_t GetRNDRRS() noexcept
235235
{
236-
uint8_t ok;
236+
uint8_t ok = 0;
237237
uint64_t r1;
238238
do {
239239
// https://developer.arm.com/documentation/ddi0601/2022-12/AArch64-Registers/RNDRRS--Reseeded-Random-Number

0 commit comments

Comments
 (0)