Skip to content

Commit d1fa090

Browse files
committed
build: warn on potentially uninitialized reads
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.
1 parent fe466a4 commit d1fa090

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

configure.ac

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ if test "x$enable_werror" = "xyes"; then
349349
APPEND_FLAG_IF_AVAILABLE([ERROR_CXXFLAGS], [-Werror=unused-variable])
350350
APPEND_FLAG_IF_AVAILABLE([ERROR_CXXFLAGS], [-Werror=date-time])
351351
APPEND_FLAG_IF_AVAILABLE([ERROR_CXXFLAGS], [-Werror=return-type])
352+
APPEND_FLAG_IF_AVAILABLE([ERROR_CXXFLAGS], [-Werror=conditional-uninitialized])
352353
fi
353354

354355
if test "x$CXXFLAGS_overridden" = "xno"; then
@@ -363,6 +364,7 @@ if test "x$CXXFLAGS_overridden" = "xno"; then
363364
APPEND_FLAG_IF_AVAILABLE([WARN_CXXFLAGS], [-Wredundant-decls])
364365
APPEND_FLAG_IF_AVAILABLE([WARN_CXXFLAGS], [-Wunused-variable])
365366
APPEND_FLAG_IF_AVAILABLE([WARN_CXXFLAGS], [-Wdate-time])
367+
APPEND_FLAG_IF_AVAILABLE([WARN_CXXFLAGS], [-Wconditional-uninitialized])
366368

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

src/Makefile.leveldb.include

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ LEVELDB_CPPFLAGS_INT += -DLEVELDB_PLATFORM_POSIX
3636
endif
3737

3838
leveldb_libleveldb_a_CPPFLAGS = $(AM_CPPFLAGS) $(LEVELDB_CPPFLAGS_INT) $(LEVELDB_CPPFLAGS)
39-
leveldb_libleveldb_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
39+
leveldb_libleveldb_a_CXXFLAGS = $(filter-out -Wconditional-uninitialized -Werror=conditional-uninitialized, $(AM_CXXFLAGS)) $(PIE_FLAGS)
4040

4141
leveldb_libleveldb_a_SOURCES=
4242
leveldb_libleveldb_a_SOURCES += leveldb/port/port_stdcxx.h

src/random.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static uint64_t GetRdRand() noexcept
116116
// RdRand may very rarely fail. Invoke it up to 10 times in a loop to reduce this risk.
117117
#ifdef __i386__
118118
uint8_t ok;
119-
uint32_t r1, r2;
119+
uint32_t r1 = 0, r2 = 0;
120120
for (int i = 0; i < 10; ++i) {
121121
__asm__ volatile (".byte 0x0f, 0xc7, 0xf0; setc %1" : "=a"(r1), "=q"(ok) :: "cc"); // rdrand %eax
122122
if (ok) break;
@@ -128,7 +128,7 @@ static uint64_t GetRdRand() noexcept
128128
return (((uint64_t)r2) << 32) | r1;
129129
#elif defined(__x86_64__) || defined(__amd64__)
130130
uint8_t ok;
131-
uint64_t r1;
131+
uint64_t r1 = 0;
132132
for (int i = 0; i < 10; ++i) {
133133
__asm__ volatile (".byte 0x48, 0x0f, 0xc7, 0xf0; setc %1" : "=a"(r1), "=q"(ok) :: "cc"); // rdrand %rax
134134
if (ok) break;

0 commit comments

Comments
 (0)