From afd8c1eb4f10e9afcd74e1f77a2f68fe47a32dc6 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Tue, 27 Aug 2024 06:01:30 +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 Reviewed-By: Matteo Collina Reviewed-By: Benjamin Gruenbaum Reviewed-By: Jake Yuesong Li Reviewed-By: Rafael Gonzaga --- 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);