From 5b748b9e68d03bd310c7abfe054173a776a81bf3 Mon Sep 17 00:00:00 2001 From: Andrew Kryczka Date: Sat, 16 Jan 2021 04:23:12 -0800 Subject: [PATCH] Cover all status codes in `Status::ToString()` (#7872) Summary: - Completed the switch statement for all possible `Code` values (the only one missing was `kCompactionTooLarge`). - Removed the default case so compiler can alert us if a new value is added to `Code` without handling it in `Status::ToString()`. Pull Request resolved: https://github.com/facebook/rocksdb/pull/7872 Test Plan: verified the log message for this scenario looks right ``` 2021/01/15-17:26:34.564450 7fa6845fe700 [ERROR] [/db_impl/db_impl_compaction_flush.cc:2621] Waiting after background compaction error: Compaction too large: , Accumulated background error counts: 1 ``` Reviewed By: ramvadiv Differential Revision: D25934539 Pulled By: ajkr fbshipit-source-id: 2e0b3c0d993e356a4987276d6f8a163f0ee8be7a --- util/status.cc | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/util/status.cc b/util/status.cc index ad948f017..616c1a746 100644 --- a/util/status.cc +++ b/util/status.cc @@ -80,8 +80,7 @@ std::string Status::ToString() const { #ifdef ROCKSDB_ASSERT_STATUS_CHECKED checked_ = true; #endif // ROCKSDB_ASSERT_STATUS_CHECKED - char tmp[30]; - const char* type; + const char* type = nullptr; switch (code_) { case kOk: return "OK"; @@ -124,15 +123,25 @@ std::string Status::ToString() const { case kTryAgain: type = "Operation failed. Try again.: "; break; + case kCompactionTooLarge: + type = "Compaction too large: "; + break; case kColumnFamilyDropped: type = "Column family dropped: "; break; - default: - snprintf(tmp, sizeof(tmp), "Unknown code(%d): ", - static_cast(code())); - type = tmp; + case kMaxCode: + assert(false); break; } + char tmp[30]; + if (type == nullptr) { + // This should not happen since `code_` should be a valid non-`kMaxCode` + // member of the `Code` enum. The above switch-statement should have had a + // case assigning `type` to a corresponding string. + assert(false); + snprintf(tmp, sizeof(tmp), "Unknown code(%d): ", static_cast(code())); + type = tmp; + } std::string result(type); if (subcode_ != kNone) { uint32_t index = static_cast(subcode_); -- GitLab