From fc9d902f3d4e45c087f8f33d3882550edb2dcce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Mon, 1 Sep 2025 13:26:53 -0700 Subject: [PATCH 1/2] Fix C++11 compatibility --- util/env_posix.cc | 3 ++- util/no_destructor.h | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/util/env_posix.cc b/util/env_posix.cc index 86571059b..0ec1f85d1 100644 --- a/util/env_posix.cc +++ b/util/env_posix.cc @@ -854,7 +854,8 @@ class SingletonEnv { #endif // !defined(NDEBUG) static_assert(sizeof(env_storage_) >= sizeof(EnvType), "env_storage_ will not fit the Env"); - static_assert(std::is_standard_layout_v>); + static_assert(std::is_standard_layout>::value, + "SingletonEnv must have standard layout"); static_assert( offsetof(SingletonEnv, env_storage_) % alignof(EnvType) == 0, "env_storage_ does not meet the Env's alignment needs"); diff --git a/util/no_destructor.h b/util/no_destructor.h index c28a10731..926ccb51f 100644 --- a/util/no_destructor.h +++ b/util/no_destructor.h @@ -21,7 +21,8 @@ class NoDestructor { explicit NoDestructor(ConstructorArgTypes&&... constructor_args) { static_assert(sizeof(instance_storage_) >= sizeof(InstanceType), "instance_storage_ is not large enough to hold the instance"); - static_assert(std::is_standard_layout_v>); + static_assert(std::is_standard_layout>::value, + "NoDestructor must have standard layout"); static_assert( offsetof(NoDestructor, instance_storage_) % alignof(InstanceType) == 0, "instance_storage_ does not meet the instance's alignment requirement"); From 2e6c041f7824c8bbe221413532862db8d62a6f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Mon, 1 Sep 2025 13:28:27 -0700 Subject: [PATCH 2/2] Remove uninitialized `bytes_written` statistics from `CompactionStats` The `bytes_written` field from `CompactionStats` was reading uninitialized memory, causing undefined behavior detected by UBSana, as reported in https://github.com/bitcoin/bitcoin/pull/28359#issuecomment-1698694748 Co-authored-by: MarcoFalke --- db/db_impl.cc | 9 ++------- db/db_impl.h | 4 +--- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index 65e31724b..5db9e6aac 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -537,7 +537,6 @@ Status DBImpl::WriteLevel0Table(MemTable* mem, VersionEdit* edit, CompactionStats stats; stats.micros = env_->NowMicros() - start_micros; - stats.bytes_written = meta.file_size; stats_[level].Add(stats); return s; } @@ -1028,9 +1027,6 @@ Status DBImpl::DoCompactionWork(CompactionState* compact) { stats.bytes_read += compact->compaction->input(which, i)->file_size; } } - for (size_t i = 0; i < compact->outputs.size(); i++) { - stats.bytes_written += compact->outputs[i].file_size; - } mutex_.Lock(); stats_[compact->compaction->level() + 1].Add(stats); @@ -1412,11 +1408,10 @@ bool DBImpl::GetProperty(const Slice& property, std::string* value) { for (int level = 0; level < config::kNumLevels; level++) { int files = versions_->NumLevelFiles(level); if (stats_[level].micros > 0 || files > 0) { - snprintf(buf, sizeof(buf), "%3d %8d %8.0f %9.0f %8.0f %9.0f\n", level, + snprintf(buf, sizeof(buf), "%3d %8d %8.0f %9.0f %8.0f\n", level, files, versions_->NumLevelBytes(level) / 1048576.0, stats_[level].micros / 1e6, - stats_[level].bytes_read / 1048576.0, - stats_[level].bytes_written / 1048576.0); + stats_[level].bytes_read / 1048576.0); value->append(buf); } } diff --git a/db/db_impl.h b/db/db_impl.h index 685735c73..8816a30a3 100644 --- a/db/db_impl.h +++ b/db/db_impl.h @@ -88,17 +88,15 @@ class DBImpl : public DB { // Per level compaction stats. stats_[level] stores the stats for // compactions that produced data for the specified "level". struct CompactionStats { - CompactionStats() : micros(0), bytes_read(0), bytes_written(0) {} + CompactionStats() : micros(0), bytes_read(0) {} void Add(const CompactionStats& c) { this->micros += c.micros; this->bytes_read += c.bytes_read; - this->bytes_written += c.bytes_written; } int64_t micros; int64_t bytes_read; - int64_t bytes_written; }; Iterator* NewInternalIterator(const ReadOptions&,