From: Tommi Virtanen Date: Wed, 2 Feb 2011 19:02:14 +0000 (-0800) Subject: Fix base64-decoding when input ends in newline. X-Git-Tag: v0.25~231^2~17 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=2b9cb24e0acbda318f6427c891c5fc97bdd9555a;p=ceph.git Fix base64-decoding when input ends in newline. 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. --- diff --git a/src/common/armor.c b/src/common/armor.c index 067ed7041403..dce1fed96154 100644 --- a/src/common/armor.c +++ b/src/common/armor.c @@ -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]); diff --git a/src/test/base64.cc b/src/test/base64.cc index 0ff580c51b0f..831fad86e563 100644 --- a/src/test/base64.cc +++ b/src/test/base64.cc @@ -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";