From 260f87a9f1c4e32ff3a596ea341ad07a901f44a9 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Tue, 10 Oct 2017 20:32:08 +0800 Subject: [PATCH] mon/PGMap: let pg_string_state() return boost::optional<> better to be specific, so we don't run into the same problem even we are using the MSB of uint64_t for a pg state bit in future. we could, however use uint64_t(-1) to indicate the pg_string_state()'s failure to parse the state string, because pg_string_state() only translate a single state a time. but it's always better to be explicit than implicit. Fixes: http://tracker.ceph.com/issues/21609 Signed-off-by: Kefu Chai --- src/mon/PGMap.cc | 6 +++--- src/osd/osd_types.cc | 6 +++--- src/osd/osd_types.h | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/mon/PGMap.cc b/src/mon/PGMap.cc index b3ec334a728da..0b6d3480497e7 100644 --- a/src/mon/PGMap.cc +++ b/src/mon/PGMap.cc @@ -2959,13 +2959,13 @@ int process_pg_map_command( state = -1; break; } else { - int64_t filter = pg_string_state(state_str); - if (filter < 0) { + auto filter = pg_string_state(state_str); + if (!filter) { *ss << "'" << state_str << "' is not a valid pg state," << " available choices: " << pg_state_string(0xFFFFFFFF); return -EINVAL; } - state |= filter; + state |= *filter; } states.pop_back(); diff --git a/src/osd/osd_types.cc b/src/osd/osd_types.cc index 5f32dc7ea4458..ceced3cb40b7e 100644 --- a/src/osd/osd_types.cc +++ b/src/osd/osd_types.cc @@ -853,9 +853,9 @@ std::string pg_state_string(uint64_t state) return ret; } -int64_t pg_string_state(const std::string& state) +boost::optional pg_string_state(const std::string& state) { - int64_t type; + boost::optional type; if (state == "active") type = PG_STATE_ACTIVE; else if (state == "clean") @@ -909,7 +909,7 @@ int64_t pg_string_state(const std::string& state) else if (state == "snaptrim_error") type = PG_STATE_SNAPTRIM_ERROR; else - type = -1; + type = boost::none; return type; } diff --git a/src/osd/osd_types.h b/src/osd/osd_types.h index 57acf95900ca5..33583e2163938 100644 --- a/src/osd/osd_types.h +++ b/src/osd/osd_types.h @@ -1008,7 +1008,7 @@ inline ostream& operator<<(ostream& out, const osd_stat_t& s) { std::string pg_state_string(uint64_t state); std::string pg_vector_string(const vector &a); -int64_t pg_string_state(const std::string& state); +boost::optional pg_string_state(const std::string& state); /* -- 2.39.5