From 07fcc14726628e9976564457008284c04e859e7f Mon Sep 17 00:00:00 2001 From: Nikolai Vavilov Date: Tue, 13 Jun 2017 18:18:37 +0300 Subject: [PATCH] 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 Reviewed-By: Alexey Orlenko Reviewed-By: Colin Ihrig --- src/base64.h | 3 +-- test/parallel/test-buffer-alloc.js | 4 ++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/base64.h b/src/base64.h index 2e0f8e3858209e..89c6251ef8fea6 100644 --- a/src/base64.h +++ b/src/base64.h @@ -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); diff --git a/test/parallel/test-buffer-alloc.js b/test/parallel/test-buffer-alloc.js index ca68a27a90fe7a..7016d07f7adef5 100644 --- a/test/parallel/test-buffer-alloc.js +++ b/test/parallel/test-buffer-alloc.js @@ -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;