]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common: fix the macros for malformed_input::what() 4521/head
authorKefu Chai <kchai@redhat.com>
Fri, 1 May 2015 11:52:25 +0000 (04:52 -0700)
committerKefu Chai <kchai@redhat.com>
Wed, 13 May 2015 02:08:10 +0000 (10:08 +0800)
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 <kchai@redhat.com>
src/include/buffer.h
src/include/encoding.h
src/test/encoding.cc

index 109a6b26768e13b16eaa3c7dd4ee29706ff1e834..6b1da01d4e09b4633d271caf90404bdcbbb728c7 100644 (file)
@@ -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;
index b190111d1c0bb423789c8109ba0c15b27ab83a11..aaa843b544da691b6635faef0981c2aca351ff3e 100644 (file)
@@ -807,13 +807,13 @@ inline void decode(std::deque<T>& 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
index 2662c240eaaf77bccd0a8cd939162aaaaa3cbab7..a047b61a3015f3c2390cda423664c5d2d70eccac 100644 (file)
@@ -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()));
+    }
+  }
+}