From f66011de2f68846a090c07c77416c56b8fb3de20 Mon Sep 17 00:00:00 2001 From: rude Date: Tue, 12 Jul 2016 21:25:23 +0200 Subject: [PATCH] Return correct size from b64_decode. The current implementation does not take into account padding (and yet it does actually depend on padding), or skipped whitespace/gibberish characters. The actual size can be computed by subtracting the padding and gibberish bytes, but in this case it's easier to just return the actual number of bytes written. --- src/common/b64.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/common/b64.cpp b/src/common/b64.cpp index aa2f6f5df..bc3bd6f62 100644 --- a/src/common/b64.cpp +++ b/src/common/b64.cpp @@ -34,9 +34,11 @@ static void b64_decode_block(char in[4], char out[3]) char *b64_decode(const char *src, int slen, int &size) { - size = (slen / 4) * 3; + // Actual output may be smaller due to padding and/or whitespace in the + // base64-encoded string. + int max_size = (slen / 4) * 3; - char *dst = new char[size]; + char *dst = new char[max_size]; char *d = dst; char in[4] = {0}, out[3], v; @@ -70,10 +72,12 @@ char *b64_decode(const char *src, int slen, int &size) { b64_decode_block(in, out); for (i = 0; i < len - 1; i++) - *(d++) = out[i]; + *(d++) = out[i]; } } + size = int(d - dst); + return dst; }