Skip to content

Commit 9d335ca

Browse files
committed
buffer: throw on invalid encoding in Buffer.from
Refs: #54533 PR-URL: #54586
1 parent c00ea01 commit 9d335ca

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

lib/buffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ function createFromString(string, ops, length = ops.byteLength(string)) {
476476

477477
function fromString(string, encoding) {
478478
let ops;
479-
if (!encoding || encoding === 'utf8' || typeof encoding !== 'string') {
479+
if (encoding == null || encoding === 'utf8') {
480480
ops = encodingOps.utf8;
481481
} else {
482482
ops = getEncodingOps(encoding);

test/parallel/test-buffer-from.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,8 @@ throws(() => {
140140
})
141141
);
142142

143-
// Invalid encoding is allowed
144-
Buffer.from('asd', 1);
143+
throws(() => {
144+
Buffer.from('asd', 1);
145+
}, {
146+
code: 'ERR_UNKNOWN_ENCODING'
147+
});

0 commit comments

Comments
 (0)