From 54fd4e054fe7407f9c23c0f2d9832ebe5fd273e6 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 1 May 2015 04:52:25 -0700 Subject: [PATCH] common: fix the macros for malformed_input::what() the thrown exception of malformed_input should carry the function name in which it was thrown. but what we have now is something like: "buffer::malformed_input: __PRETTY_FUNCTION__ unknown encoding version > 100" because __PRETTY_FUNCTION__ is not a macro any more. see - https://gcc.gnu.org/onlinedocs/gcc-3.1/gcc/Function-Names.html - https://gcc.gnu.org/onlinedocs/gcc/Function-Names.html and it is not a string literal, so neither can we can concat it with the literal err message. Signed-off-by: Kefu Chai --- src/include/buffer.h | 4 ++-- src/include/encoding.h | 6 +++--- src/test/encoding.cc | 27 +++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/include/buffer.h b/src/include/buffer.h index 109a6b26768e1..6b1da01d4e09b 100644 --- a/src/include/buffer.h +++ b/src/include/buffer.h @@ -87,8 +87,8 @@ public: } }; struct malformed_input : public error { - explicit malformed_input(const char *w) { - snprintf(buf, sizeof(buf), "buffer::malformed_input: %s", w); + explicit malformed_input(const std::string& w) { + snprintf(buf, sizeof(buf), "buffer::malformed_input: %s", w.c_str()); } const char *what() const throw () { return buf; diff --git a/src/include/encoding.h b/src/include/encoding.h index b190111d1c0bb..aaa843b544da6 100644 --- a/src/include/encoding.h +++ b/src/include/encoding.h @@ -807,13 +807,13 @@ inline void decode(std::deque& ls, bufferlist::iterator& p) #define ENCODE_FINISH(bl) ENCODE_FINISH_NEW_COMPAT(bl, 0) #define DECODE_ERR_VERSION(func, v) \ - "" #func " unknown encoding version > " #v + (std::string(func) + " unknown encoding version > " #v) #define DECODE_ERR_OLDVERSION(func, v) \ - "" #func " no longer understand old encoding version < " #v + (std::string(func) + " no longer understand old encoding version < " #v) #define DECODE_ERR_PAST(func) \ - "" #func " decode past end of struct encoding" + (std::string(func) + " decode past end of struct encoding") /** * check for very old encoding diff --git a/src/test/encoding.cc b/src/test/encoding.cc index 2662c240eaaf7..a047b61a3015f 100644 --- a/src/test/encoding.cc +++ b/src/test/encoding.cc @@ -196,3 +196,30 @@ TEST(EncodingRoundTrip, MultimapConstructorCounter) { EXPECT_EQ(my_val_t::get_copy_ctor(), 10); EXPECT_EQ(my_val_t::get_assigns(), 0); } + +const char* expected_what[] = { + "buffer::malformed_input: void lame_decoder(int) unknown encoding version > 100", + "buffer::malformed_input: void lame_decoder(int) no longer understand old encoding version < 100", + "buffer::malformed_input: void lame_decoder(int) decode past end of struct encoding", +}; + +void lame_decoder(int which) { + switch (which) { + case 0: + throw buffer::malformed_input(DECODE_ERR_VERSION(__PRETTY_FUNCTION__, 100)); + case 1: + throw buffer::malformed_input(DECODE_ERR_OLDVERSION(__PRETTY_FUNCTION__, 100)); + case 2: + throw buffer::malformed_input(DECODE_ERR_PAST(__PRETTY_FUNCTION__)); + } +} + +TEST(EncodingException, Macros) { + for (unsigned i = 0; i < sizeof(expected_what)/sizeof(expected_what[0]); i++) { + try { + lame_decoder(i); + } catch (const exception& e) { + ASSERT_EQ(string(expected_what[i]), string(e.what())); + } + } +} -- 2.39.5