From e25c344c7833581f559a5a678a5b4259d0de3ec7 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sat, 24 Aug 2024 09:40:58 +0200 Subject: [PATCH] buffer: allow invalid encoding in from Looks like a bug to me but the change should probably done in a semver majpr. PR-URL: https://github.com/nodejs/node/pull/54533 --- lib/buffer.js | 2 +- test/parallel/test-buffer-from.js | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/buffer.js b/lib/buffer.js index 05b57275f03dca..4467a555c7180e 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') { + if (!encoding || encoding === 'utf8' || typeof encoding !== 'string') { ops = encodingOps.utf8; } else { ops = getEncodingOps(encoding); diff --git a/test/parallel/test-buffer-from.js b/test/parallel/test-buffer-from.js index 284c63e7d02e8e..416a3b3a3105b5 100644 --- a/test/parallel/test-buffer-from.js +++ b/test/parallel/test-buffer-from.js @@ -139,3 +139,6 @@ throws(() => { code: 'ERR_OUT_OF_RANGE', }) ); + +// Invalid encoding is allowed +Buffer.from('asd', 1);