]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
Fix base64-decoding when input ends in newline.
authorTommi Virtanen <tv@hq.newdream.net>
Wed, 2 Feb 2011 19:02:14 +0000 (11:02 -0800)
committerTommi Virtanen <tv@hq.newdream.net>
Wed, 2 Feb 2011 19:02:17 +0000 (11:02 -0800)
It used to return -EINVAL because it thought the end was not aligned
to 4 bytes.

Clean up superfluous src < end test in if, the while itself guarantees
that.

src/common/armor.c
src/test/base64.cc

index 067ed70414033ec0d1a140827cc3117d42838761..dce1fed96154a9546c4ec6716a4c0923dea3928d 100644 (file)
@@ -92,8 +92,11 @@ int ceph_unarmor(char *dst, const char *dst_end, const char *src, const char *en
        while (src < end) {
                int a, b, c, d;
 
-               if (src < end && src[0] == '\n')
+               if (src[0] == '\n') {
                        src++;
+                       continue;
+               }
+
                if (src + 4 > end)
                        return -EINVAL;
                a = decode_bits(src[0]);
index 0ff580c51b0f8a52c79d3d24d04c0c00aef69265..831fad86e563eba9984c2412c5c64e881902b7cf 100644 (file)
@@ -61,6 +61,18 @@ TEST(RoundTrip, RandomRoundTrips) {
   }
 }
 
+TEST(EdgeCase, EndsInNewline) {
+  static const int OUT_MAX = 4096;
+
+  char b64[] =
+    "aaaa\n";
+
+    char decoded[OUT_MAX];
+    memset(decoded, 0, sizeof(decoded));
+    int blen = ceph_unarmor(decoded, decoded + OUT_MAX, b64, b64 + sizeof(b64)-1);
+    ASSERT_GE(blen, 0);
+}
+
 TEST(FuzzEncoding, BadDecode1) {
   static const int OUT_LEN = 4096;
   const char * const bad_encoded = "FAKEBASE64 foo";