Skip to content

Commit 6621be5

Browse files
committed
Merge #18843: build: warn on potentially uninitialized reads
71f183a build: warn on potentially uninitialized reads (Vasil Dimov) Pull request description: * Enable `conditional-uninitialized` warning class to show 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. ACKs for top commit: practicalswift: ACK 71f183a laanwj: ACK 71f183a Tree-SHA512: 2c1d8caacd86424b16a9d92e5df19e0bedb51ae111eecad7e3bfa46447bc88e5fff1f32dacf6c4a28257ebb3d87e79f80f074ce2c523ce08b1a0c0a67ab44204
2 parents dd3310b + 71f183a commit 6621be5

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

configure.ac

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ if test "x$enable_werror" = "xyes"; then
344344
AX_CHECK_COMPILE_FLAG([-Werror=unused-variable],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=unused-variable"],,[[$CXXFLAG_WERROR]])
345345
AX_CHECK_COMPILE_FLAG([-Werror=date-time],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=date-time"],,[[$CXXFLAG_WERROR]])
346346
AX_CHECK_COMPILE_FLAG([-Werror=return-type],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=return-type"],,[[$CXXFLAG_WERROR]])
347+
AX_CHECK_COMPILE_FLAG([-Werror=conditional-uninitialized],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=conditional-uninitialized"],,[[$CXXFLAG_WERROR]])
347348
fi
348349

349350
if test "x$CXXFLAGS_overridden" = "xno"; then
@@ -359,6 +360,7 @@ if test "x$CXXFLAGS_overridden" = "xno"; then
359360
AX_CHECK_COMPILE_FLAG([-Wredundant-decls],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wredundant-decls"],,[[$CXXFLAG_WERROR]])
360361
AX_CHECK_COMPILE_FLAG([-Wunused-variable],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wunused-variable"],,[[$CXXFLAG_WERROR]])
361362
AX_CHECK_COMPILE_FLAG([-Wdate-time],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wdate-time"],,[[$CXXFLAG_WERROR]])
363+
AX_CHECK_COMPILE_FLAG([-Wconditional-uninitialized],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wconditional-uninitialized"],,[[$CXXFLAG_WERROR]])
362364

363365
dnl Some compilers (gcc) ignore unknown -Wno-* options, but warn about all
364366
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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ 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+
// Initialize to 0 to silence a compiler warning that r1 or r2 may be used
120+
// uninitialized. Even if rdrand fails (!ok) it will set the output to 0,
121+
// but there is no way that the compiler could know that.
122+
uint32_t r1 = 0, r2 = 0;
120123
for (int i = 0; i < 10; ++i) {
121124
__asm__ volatile (".byte 0x0f, 0xc7, 0xf0; setc %1" : "=a"(r1), "=q"(ok) :: "cc"); // rdrand %eax
122125
if (ok) break;
@@ -128,7 +131,7 @@ static uint64_t GetRdRand() noexcept
128131
return (((uint64_t)r2) << 32) | r1;
129132
#elif defined(__x86_64__) || defined(__amd64__)
130133
uint8_t ok;
131-
uint64_t r1;
134+
uint64_t r1 = 0; // See above why we initialize to 0.
132135
for (int i = 0; i < 10; ++i) {
133136
__asm__ volatile (".byte 0x48, 0x0f, 0xc7, 0xf0; setc %1" : "=a"(r1), "=q"(ok) :: "cc"); // rdrand %rax
134137
if (ok) break;

0 commit comments

Comments
 (0)