Skip to content

Commit a27a35b

Browse files
seishunaddaleax
authored andcommitted
src: fix decoding base64 with whitespace
`max_i` should also include the characters that were just read by `base64_decode_group_slow()`. PR-URL: #13660 Fixes: #13636 Fixes: #13657 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Alexey Orlenko <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent c046a21 commit a27a35b

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/base64.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,9 @@ size_t base64_decode_fast(char* const dst, const size_t dstlen,
9999
unbase64(src[i + 3]);
100100
// If MSB is set, input contains whitespace or is not valid base64.
101101
if (v & 0x80808080) {
102-
const size_t old_i = i;
103102
if (!base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k))
104103
return k;
105-
max_i = old_i + (srclen - i) / 4 * 4; // Align max_i again.
104+
max_i = i + (srclen - i) / 4 * 4; // Align max_i again.
106105
} else {
107106
dst[k + 0] = ((v >> 22) & 0xFC) | ((v >> 20) & 0x03);
108107
dst[k + 1] = ((v >> 12) & 0xF0) | ((v >> 10) & 0x0F);

test/parallel/test-buffer-alloc.js

+4
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,10 @@ assert.strictEqual(Buffer.from('=bad'.repeat(1e4), 'base64').length, 0);
468468
assert.deepStrictEqual(Buffer.from('w0 ', 'base64'),
469469
Buffer.from('w0', 'base64'));
470470

471+
// Regression test for https://github.com/nodejs/node/issues/13657.
472+
assert.deepStrictEqual(Buffer.from(' YWJvcnVtLg', 'base64'),
473+
Buffer.from('YWJvcnVtLg', 'base64'));
474+
471475
{
472476
// Creating buffers larger than pool size.
473477
const l = Buffer.poolSize + 5;

0 commit comments

Comments
 (0)