Skip to content

Commit

Permalink
buffer: throw on invalid encoding in Buffer.from
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Aug 27, 2024
1 parent c00ea01 commit 9d335ca
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ function createFromString(string, ops, length = ops.byteLength(string)) {

function fromString(string, encoding) {
let ops;
if (!encoding || encoding === 'utf8' || typeof encoding !== 'string') {
if (encoding == null || encoding === 'utf8') {
ops = encodingOps.utf8;
} else {
ops = getEncodingOps(encoding);
Expand Down
7 changes: 5 additions & 2 deletions test/parallel/test-buffer-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,8 @@ throws(() => {
})
);

// Invalid encoding is allowed
Buffer.from('asd', 1);
throws(() => {
Buffer.from('asd', 1);
}, {
code: 'ERR_UNKNOWN_ENCODING'
});

0 comments on commit 9d335ca

Please sign in to comment.