Skip to content

Fix Clang -Wconditional-uninitialized warning#59

Open
hebasto wants to merge 1 commit intobitcoin-core:bitcoin-forkfrom
hebasto:260203-uninitialized
Open

Fix Clang -Wconditional-uninitialized warning#59
hebasto wants to merge 1 commit intobitcoin-core:bitcoin-forkfrom
hebasto:260203-uninitialized

Conversation

@hebasto
Copy link
Copy Markdown
Member

@hebasto hebasto commented Feb 3, 2026

Steps to reproduce the warning on Ubuntu 25.10 using Clang 22.0:

  1. Apply a diff to workaround a build issue:
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -12,7 +12,7 @@ set(CMAKE_C_STANDARD_REQUIRED OFF)
 set(CMAKE_C_EXTENSIONS OFF)
 
 # This project requires C++11.
-set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_CXX_STANDARD 20)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
 set(CMAKE_CXX_EXTENSIONS OFF)
 
  1. Configure:
$ cmake -B build -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_CXX_FLAGS=-Wconditional-uninitialized -DLEVELDB_BUILD_BENCHMARKS=ON
  1. Build:
$ cmake --build build
[23/161] Building CXX object CMakeFiles/leveldb.dir/db/version_set.cc.o
FAILED: CMakeFiles/leveldb.dir/db/version_set.cc.o 
/usr/bin/clang++ -DLEVELDB_COMPILE_LIBRARY -DLEVELDB_PLATFORM_POSIX=1 -I/home/hebasto/dev/leveldb/build/include -I/home/hebasto/dev/leveldb/. -I/home/hebasto/dev/leveldb/include -Wconditional-uninitialized -fno-exceptions -fno-rtti -std=c++20 -Werror -Wthread-safety -MD -MT CMakeFiles/leveldb.dir/db/version_set.cc.o -MF CMakeFiles/leveldb.dir/db/version_set.cc.o.d -o CMakeFiles/leveldb.dir/db/version_set.cc.o -c /home/hebasto/dev/leveldb/db/version_set.cc
/home/hebasto/dev/leveldb/db/version_set.cc:1016:55: error: variable 'manifest_size' may be uninitialized when used here [-Werror,-Wconditional-uninitialized]
 1016 |   descriptor_log_ = new log::Writer(descriptor_file_, manifest_size);
      |                                                       ^~~~~~~~~~~~~
/home/hebasto/dev/leveldb/db/version_set.cc:997:25: note: initialize the variable 'manifest_size' to silence this warning
  997 |   uint64_t manifest_size;
      |                         ^
      |                          = 0
1 error generated.
[40/161] Building CXX object CMakeFiles/leveldb.dir/util/env_posix.cc.o
ninja: build stopped: subcommand failed.
hebasto@TS-P340:~/dev/leveldb

This eliminates the need for the following code:

else()
  try_append_cxx_flags("-Wconditional-uninitialized" TARGET nowarn_leveldb_interface SKIP_LINK
    IF_CHECK_PASSED "-Wno-conditional-uninitialized"
  )

Copy link
Copy Markdown

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Comment thread db/corruption_test.cc Outdated
@maflcko
Copy link
Copy Markdown

maflcko commented Feb 6, 2026

Unrelated: Google is using clang to build their projects, so I wonder why they haven't fixed it yet upstream or if they are also using a workaround 🤔

reuse_logs isn't set by Bitcoin Core, so this is just refactoring dead code?

Seems fine, if the motivation is to remove build code.

@hebasto
Copy link
Copy Markdown
Member Author

hebasto commented Feb 6, 2026

reuse_logs isn't set by Bitcoin Core, so this is just refactoring dead code?

From the build log of the Bitcoin Core patched to enable the warning:

[55/626] Building CXX object src/CMakeFiles/leveldb.dir/leveldb/db/version_set.cc.o
../src/leveldb/db/version_set.cc:1016:55: warning: variable 'manifest_size' may be uninitialized when used here [-Wconditional-uninitialized]
 1016 |   descriptor_log_ = new log::Writer(descriptor_file_, manifest_size);
      |                                                       ^~~~~~~~~~~~~
../src/leveldb/db/version_set.cc:997:25: note: initialize the variable 'manifest_size' to silence this warning
  997 |   uint64_t manifest_size;
      |                         ^
      |                          = 0
1 warning generated.

@fanquake
Copy link
Copy Markdown
Member

fanquake commented Feb 6, 2026

Yes the compiler warns, but Core doesn't set reuse_logs (which defaults to false), so the code you are changing here, can never be hit, because we always return early out of ReuseManifest.

@hebasto hebasto force-pushed the 260203-uninitialized branch from 8c70878 to 0711e6d Compare February 6, 2026 15:37
@maflcko
Copy link
Copy Markdown

maflcko commented Feb 6, 2026

lgtm ACK 0711e6d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants