]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common: fix buffer::list::decode_base64
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Thu, 13 Jan 2011 19:19:27 +0000 (11:19 -0800)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Thu, 13 Jan 2011 16:58:07 +0000 (08:58 -0800)
buffer::list::decode_base64 needs to check for decode failures.

Signed-off-by: Colin McCabe <colinm@hq.newdream.net>
src/common/buffer.cc
src/test/base64.cc
src/test/cli/cauthtool/add-key-segv.t

index 1b3d4b731bd0d5fde35e25113a0da0919f322a60..f2d785b918da57f6b6e82ddfc523eac7a342a210 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <errno.h>
 #include <fstream>
+#include <sstream>
 #include <sys/uio.h>
 #include <limits.h>
 
@@ -39,8 +40,14 @@ void buffer::list::encode_base64(buffer::list& o)
 
 void buffer::list::decode_base64(buffer::list& e)
 {
-  bufferptr bp(e.length() * 3 / 4 + 4);
+  bufferptr bp(4 + ((e.length() * 3) / 4));
   int l = ceph_unarmor(bp.c_str(), bp.c_str() + bp.length(), e.c_str(), e.c_str() + e.length());
+  if (l < 0) {
+    std::ostringstream oss;
+    oss << "decode_base64: decoding failed:\n";
+    hexdump(oss);
+    throw buffer::malformed_input(oss.str().c_str());
+  }
   assert(l <= (int)bp.length());
   bp.set_length(l);
   push_back(bp);
index 2cf6d71fed8d30f42dc97ce227811445056939fb..dc091221eb344d5d9e17edf2bb461edffa462415 100644 (file)
@@ -19,7 +19,7 @@
 
 #include "gtest/gtest.h"
 
-TEST(CorrectBase64Encoding, StringSimple) {
+TEST(CorrectBase64RoundTrip, StringSimple) {
   static const int OUT_LEN = 4096;
   const char * const original = "abracadabra";
   const char * const correctly_encoded = "YWJyYWNhZGFicmE=";
@@ -34,7 +34,7 @@ TEST(CorrectBase64Encoding, StringSimple) {
   ASSERT_STREQ(original, out2);
 }
 
-TEST(IncorrectBase64Encoding, StringSimple) {
+TEST(IncorrectBase64Decoding, StringSimple) {
   static const int OUT_LEN = 4096;
   const char * const bad_encoded = "FAKEBASE64 foo";
   char out[OUT_LEN];
@@ -42,3 +42,20 @@ TEST(IncorrectBase64Encoding, StringSimple) {
   int alen = ceph_unarmor(out, out + OUT_LEN, bad_encoded, bad_encoded + strlen(bad_encoded));
   ASSERT_LT(alen, 0);
 }
+
+TEST(IncorrectBase64Decoding2, StringSimple) {
+  string str("FAKEBASE64 foo");
+  bool failed = false;
+  try {
+    bufferlist bl;
+    bl.append(str);
+
+    bufferlist cl;
+    cl.decode_base64(bl);
+    cl.hexdump(std::cerr);
+  }
+  catch (const buffer::error &err) {
+    failed = true;
+  }
+  ASSERT_EQ(failed, true);
+}
index 370dbabdf04cf7eaf5370e3290d1dd9bf3c4673d..696a80a574f84f9b553fca8f2d97c20331cc21c9 100644 (file)
@@ -1,18 +1,6 @@
   $ cauthtool kring --create-keyring
   creating kring
 
-# TODO fix me
   $ cauthtool kring --add-key 'FAKEBASE64 foo'
-  *** Caught signal (Segmentation fault) ***
-  in thread [0-9a-f]{12} (re)
-   ceph version .* (re)
-   1: .* (re)
-   2: .* (re)
-   3: .* (re)
-   4: .* (re)
-   5: .* (re)
-   6: .* (re)
-   7: .* (re)
-   8: .* (re)
-  Segmentation fault
-  [139]
+  can't decode key 'FAKEBASE64 foo'
+  [1]