Skip to content

Commit 2a26b11

Browse files
committed
bitcoin#18843: warn on potentially uninitialized reads
1 parent 1038970 commit 2a26b11

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

configure.ac

+2
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ if test "x$enable_werror" = "xyes"; then
367367
AX_CHECK_COMPILE_FLAG([-Werror=unused-variable],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=unused-variable"],,[[$CXXFLAG_WERROR]])
368368
AX_CHECK_COMPILE_FLAG([-Werror=date-time],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=date-time"],,[[$CXXFLAG_WERROR]])
369369
AX_CHECK_COMPILE_FLAG([-Werror=return-type],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=return-type"],,[[$CXXFLAG_WERROR]])
370+
AX_CHECK_COMPILE_FLAG([-Werror=conditional-uninitialized],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=conditional-uninitialized"],,[[$CXXFLAG_WERROR]])
370371
fi
371372

372373
if test "x$CXXFLAGS_overridden" = "xno"; then
@@ -381,6 +382,7 @@ if test "x$CXXFLAGS_overridden" = "xno"; then
381382
AX_CHECK_COMPILE_FLAG([-Wredundant-decls],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wredundant-decls"],,[[$CXXFLAG_WERROR]])
382383
AX_CHECK_COMPILE_FLAG([-Wunused-variable],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wunused-variable"],,[[$CXXFLAG_WERROR]])
383384
AX_CHECK_COMPILE_FLAG([-Wdate-time],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wdate-time"],,[[$CXXFLAG_WERROR]])
385+
AX_CHECK_COMPILE_FLAG([-Wconditional-uninitialized],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wconditional-uninitialized"],,[[$CXXFLAG_WERROR]])
384386

385387
## Some compilers (gcc) ignore unknown -Wno-* options, but warn about all
386388
## unknown options if any other warning is produced. Test the -Wfoo case, and

src/Makefile.leveldb.include

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ LEVELDB_CPPFLAGS_INT += -DLEVELDB_PLATFORM_POSIX
3030
endif
3131

3232
leveldb_libleveldb_a_CPPFLAGS = $(AM_CPPFLAGS) $(LEVELDB_CPPFLAGS_INT) $(LEVELDB_CPPFLAGS)
33-
leveldb_libleveldb_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
33+
leveldb_libleveldb_a_CXXFLAGS = $(filter-out -Wconditional-uninitialized -Werror=conditional-uninitialized, $(AM_CXXFLAGS)) $(PIE_FLAGS)
3434

3535
leveldb_libleveldb_a_SOURCES=
3636
leveldb_libleveldb_a_SOURCES += leveldb/port/atomic_pointer.h

src/random.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ static bool GetHWRand(unsigned char* ent32) {
100100
// Not all assemblers support the rdrand instruction, write it in hex.
101101
#ifdef __i386__
102102
for (int iter = 0; iter < 4; ++iter) {
103-
uint32_t r1, r2;
103+
// Initialize to 0 to silence a compiler warning that r1 or r2 may be used
104+
// uninitialized. Even if rdrand fails (!ok) it will set the output to 0,
105+
// but there is no way that the compiler could know that.
106+
uint32_t r1 = 0, r2 = 0;
104107
__asm__ volatile (".byte 0x0f, 0xc7, 0xf0;" // rdrand %eax
105108
".byte 0x0f, 0xc7, 0xf2;" // rdrand %edx
106109
"setc %2" :
@@ -110,7 +113,7 @@ static bool GetHWRand(unsigned char* ent32) {
110113
WriteLE32(ent32 + 8 * iter + 4, r2);
111114
}
112115
#else
113-
uint64_t r1, r2, r3, r4;
116+
uint64_t r1 = 0, r2 = 0, r3 = 0, r4 = 0; // See above why we initialize to 0.
114117
__asm__ volatile (".byte 0x48, 0x0f, 0xc7, 0xf0, " // rdrand %rax
115118
"0x48, 0x0f, 0xc7, 0xf3, " // rdrand %rbx
116119
"0x48, 0x0f, 0xc7, 0xf1, " // rdrand %rcx

0 commit comments

Comments
 (0)