From 14bb6f97f09d2a273cc7cf9f839029c8e8d9473c Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Thu, 3 Feb 2022 14:30:27 +0200 Subject: [PATCH] buffer: fix atob/btoa no-arg case MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/41478 Fixes: https://github.com/nodejs/node/issues/41450 Reviewed-By: Tobias Nießen Reviewed-By: Filip Skokan Reviewed-By: Michaël Zasso --- lib/buffer.js | 7 +++++++ test/parallel/test-btoa-atob-global.js | 9 --------- test/parallel/test-btoa-atob.js | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 9 deletions(-) delete mode 100644 test/parallel/test-btoa-atob-global.js create mode 100644 test/parallel/test-btoa-atob.js diff --git a/lib/buffer.js b/lib/buffer.js index 1f4f0a2e89deb8..2d0057544395bc 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -96,6 +96,7 @@ const { ERR_INVALID_ARG_VALUE, ERR_INVALID_BUFFER_SIZE, ERR_OUT_OF_RANGE, + ERR_MISSING_ARGS, ERR_UNKNOWN_ENCODING }, hideStackFrames @@ -1214,6 +1215,9 @@ function btoa(input) { // The implementation here has not been performance optimized in any way and // should not be. // Refs: https://github.com/nodejs/node/pull/38433#issuecomment-828426932 + if (arguments.length === 0) { + throw new ERR_MISSING_ARGS('input'); + } input = `${input}`; for (let n = 0; n < input.length; n++) { if (input[n].charCodeAt(0) > 0xff) @@ -1230,6 +1234,9 @@ function atob(input) { // The implementation here has not been performance optimized in any way and // should not be. // Refs: https://github.com/nodejs/node/pull/38433#issuecomment-828426932 + if (arguments.length === 0) { + throw new ERR_MISSING_ARGS('input'); + } input = `${input}`; for (let n = 0; n < input.length; n++) { if (!kBase64Digits.includes(input[n])) diff --git a/test/parallel/test-btoa-atob-global.js b/test/parallel/test-btoa-atob-global.js deleted file mode 100644 index 06a6d3ed28f3f1..00000000000000 --- a/test/parallel/test-btoa-atob-global.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -require('../common'); - -const { strictEqual } = require('assert'); -const buffer = require('buffer'); - -strictEqual(globalThis.atob, buffer.atob); -strictEqual(globalThis.btoa, buffer.btoa); diff --git a/test/parallel/test-btoa-atob.js b/test/parallel/test-btoa-atob.js new file mode 100644 index 00000000000000..162406dd9f6b50 --- /dev/null +++ b/test/parallel/test-btoa-atob.js @@ -0,0 +1,14 @@ +'use strict'; + +require('../common'); + +const { strictEqual, throws } = require('assert'); +const buffer = require('buffer'); + +// Exported on the global object +strictEqual(globalThis.atob, buffer.atob); +strictEqual(globalThis.btoa, buffer.btoa); + +// Throws type error on no argument passed +throws(() => buffer.atob(), /TypeError/); +throws(() => buffer.btoa(), /TypeError/);