Skip to content

Commit

Permalink
src: fix base64 decoding
Browse files Browse the repository at this point in the history
Make sure trailing garbage is not treated as a valid base64 character.

Fixes: #11987
PR-URL: #11995
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
seishun authored and MylesBorins committed Apr 19, 2017
1 parent 7a1920d commit eb393f9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/string_bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ size_t base64_decode_slow(char* dst, size_t dstlen,
size_t k = 0;
for (;;) {
#define V(expr) \
while (i < srclen) { \
for (;;) { \
const uint8_t c = src[i]; \
lo = unbase64(c); \
i += 1; \
if (lo < 64) \
break; /* Legal character. */ \
if (c == '=') \
if (c == '=' || i >= srclen) \
return k; \
} \
expr; \
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,10 @@ assert.equal(dot.toString('base64'), '//4uAA==');
// Regression test for https://github.com/nodejs/node/issues/3496.
assert.equal(Buffer.from('=bad'.repeat(1e4), 'base64').length, 0);

// Regression test for https://github.com/nodejs/node/issues/11987.
assert.deepStrictEqual(Buffer.from('w0 ', 'base64'),
Buffer.from('w0', 'base64'));

{
// Creating buffers larger than pool size.
const l = Buffer.poolSize + 5;
Expand Down

0 comments on commit eb393f9

Please sign in to comment.