From 11fe4ff48a91f2b8716596fb84e44b3b147148f5 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Tue, 27 Aug 2024 07:33:30 +0200 Subject: [PATCH] buffer: throw on invalid encoding in Buffer.from Refs: https://github.com/nodejs/node/pull/54533 PR-URL: https://github.com/nodejs/node/pull/54586 --- lib/buffer.js | 2 +- test/parallel/test-buffer-from.js | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 4467a555c7180e..05b57275f03dca 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -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 || encoding === 'utf8') { ops = encodingOps.utf8; } else { ops = getEncodingOps(encoding); diff --git a/test/parallel/test-buffer-from.js b/test/parallel/test-buffer-from.js index 416a3b3a3105b5..d8d0eefa23f0dc 100644 --- a/test/parallel/test-buffer-from.js +++ b/test/parallel/test-buffer-from.js @@ -140,5 +140,8 @@ throws(() => { }) ); -// Invalid encoding is allowed -Buffer.from('asd', 1); +throws(() => { + Buffer.from('asd', 1); +}, { + code: 'ERR_UNKNOWN_ENCODING' +})