Skip to content

Commit c00ea01

Browse files
authored
buffer: allow invalid encoding in from
Looks like a bug to me but the change should probably done in a semver majpr. PR-URL: nodejs#54533 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Jake Yuesong Li <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]>
1 parent b39fad6 commit c00ea01

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

lib/buffer.js

Lines changed: 1 addition & 1 deletion
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') {
479+
if (!encoding || encoding === 'utf8' || typeof encoding !== 'string') {
480480
ops = encodingOps.utf8;
481481
} else {
482482
ops = getEncodingOps(encoding);

test/parallel/test-buffer-from.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,6 @@ throws(() => {
139139
code: 'ERR_OUT_OF_RANGE',
140140
})
141141
);
142+
143+
// Invalid encoding is allowed
144+
Buffer.from('asd', 1);

0 commit comments

Comments
 (0)