>From d0d1a5a7a3f00e4ba418545fe9e151fb1cb357ed Mon Sep 17 00:00:00 2001 From: Gijs van Tulder Date: Wed, 10 Aug 2011 15:13:12 +0200 Subject: [PATCH 1/2] base64_decode_alloc_ctx would sometimes miss the final byte. If you called base64_decode_alloc_ctx with a partial byte left over from a previous call, the last character would not be decoded because the output buffer was too small. (This bug was noted in the test, stangely, but not fixed.) --- ChangeLog | 7 +++++++ lib/base64.c | 2 +- tests/test-base64.c | 5 ++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 701f07b..2d165ae 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2011-08-10 Gijs van Tulder + + base64: Fix bug in base64_decode_alloc_ctx, which sometimes would + not decode the final character. + * lib/base64.c: Fixed bug. + * lib/base64.h: Updated test. + 2011-08-09 Bruno Haible More tests for 'fseeko'. diff --git a/lib/base64.c b/lib/base64.c index 99fcc57..96d2cfd 100644 --- a/lib/base64.c +++ b/lib/base64.c @@ -555,7 +555,7 @@ base64_decode_alloc_ctx (struct base64_decode_context *ctx, The exact size is 3 * inlen / 4, minus 1 if the input ends with "=" and minus another 1 if the input ends with "==". Dividing before multiplying avoids the possibility of overflow. */ - size_t needlen = 3 * (inlen / 4) + 2; + size_t needlen = 3 * (inlen / 4) + 3; *out = malloc (needlen); if (!*out) diff --git a/tests/test-base64.c b/tests/test-base64.c index c7cad2f..b1979b4 100644 --- a/tests/test-base64.c +++ b/tests/test-base64.c @@ -184,9 +184,8 @@ main (void) ok = base64_decode_alloc_ctx (&ctx, "hp", 2, &p, &len); ASSERT (ok); - ASSERT (len == 2); - /* Actually this looks buggy. Shouldn't output be 'ghi'? */ - ASSERT (memcmp (p, "gh", len) == 0); + ASSERT (len == 3); + ASSERT (memcmp (p, "ghi", len) == 0); ok = base64_decode_alloc_ctx (&ctx, "", 0, &p, &len); ASSERT (ok); } -- 1.7.4.1