src: fix decoding base64 with whitespace

`max_i` should also include the characters that were just read by
`base64_decode_group_slow()`.

PR-URL: https://github.com/nodejs/node/pull/13660
Fixes: https://github.com/nodejs/node/issues/13636
Fixes: https://github.com/nodejs/node/issues/13657
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Nikolai Vavilov 2017-06-13 18:18:37 +03:00
parent 00aac57df9
commit 64812f5728
2 changed files with 5 additions and 2 deletions

View File

@ -99,10 +99,9 @@ size_t base64_decode_fast(char* const dst, const size_t dstlen,
unbase64(src[i + 3]);
// If MSB is set, input contains whitespace or is not valid base64.
if (v & 0x80808080) {
const size_t old_i = i;
if (!base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k))
return k;
max_i = old_i + (srclen - i) / 4 * 4; // Align max_i again.
max_i = i + (srclen - i) / 4 * 4; // Align max_i again.
} else {
dst[k + 0] = ((v >> 22) & 0xFC) | ((v >> 20) & 0x03);
dst[k + 1] = ((v >> 12) & 0xF0) | ((v >> 10) & 0x0F);

View File

@ -468,6 +468,10 @@ assert.strictEqual(Buffer.from('=bad'.repeat(1e4), 'base64').length, 0);
assert.deepStrictEqual(Buffer.from('w0 ', 'base64'),
Buffer.from('w0', 'base64'));
// Regression test for https://github.com/nodejs/node/issues/13657.
assert.deepStrictEqual(Buffer.from(' YWJvcnVtLg', 'base64'),
Buffer.from('YWJvcnVtLg', 'base64'));
{
// Creating buffers larger than pool size.
const l = Buffer.poolSize + 5;