Skip to content

Commit 14bb6f9

Browse files
benjamingrdanielleadams
authored andcommitted
buffer: fix atob/btoa no-arg case
PR-URL: #41478 Fixes: #41450 Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 44b6927 commit 14bb6f9

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

lib/buffer.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ const {
9696
ERR_INVALID_ARG_VALUE,
9797
ERR_INVALID_BUFFER_SIZE,
9898
ERR_OUT_OF_RANGE,
99+
ERR_MISSING_ARGS,
99100
ERR_UNKNOWN_ENCODING
100101
},
101102
hideStackFrames
@@ -1214,6 +1215,9 @@ function btoa(input) {
12141215
// The implementation here has not been performance optimized in any way and
12151216
// should not be.
12161217
// Refs: https://github.com/nodejs/node/pull/38433#issuecomment-828426932
1218+
if (arguments.length === 0) {
1219+
throw new ERR_MISSING_ARGS('input');
1220+
}
12171221
input = `${input}`;
12181222
for (let n = 0; n < input.length; n++) {
12191223
if (input[n].charCodeAt(0) > 0xff)
@@ -1230,6 +1234,9 @@ function atob(input) {
12301234
// The implementation here has not been performance optimized in any way and
12311235
// should not be.
12321236
// Refs: https://github.com/nodejs/node/pull/38433#issuecomment-828426932
1237+
if (arguments.length === 0) {
1238+
throw new ERR_MISSING_ARGS('input');
1239+
}
12331240
input = `${input}`;
12341241
for (let n = 0; n < input.length; n++) {
12351242
if (!kBase64Digits.includes(input[n]))

test/parallel/test-btoa-atob-global.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/parallel/test-btoa-atob.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const { strictEqual, throws } = require('assert');
6+
const buffer = require('buffer');
7+
8+
// Exported on the global object
9+
strictEqual(globalThis.atob, buffer.atob);
10+
strictEqual(globalThis.btoa, buffer.btoa);
11+
12+
// Throws type error on no argument passed
13+
throws(() => buffer.atob(), /TypeError/);
14+
throws(() => buffer.btoa(), /TypeError/);

0 commit comments

Comments
 (0)