From d1f14e8d54f94bf42c733e7afb9825b899741059 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Wed, 5 May 2021 12:41:11 +0800 Subject: [PATCH] buffer: refactor `byteLength` to remove outdated optimizations The third argument `mustMatch` is now outdated, so remove it. Fixes: https://github.com/nodejs/node/issues/38536 --- lib/buffer.js | 17 ++++++++--------- test/parallel/test-buffer-bytelength.js | 2 -- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 57d6cddbaa2e6b..8e29ac1822af82 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -738,17 +738,16 @@ function byteLength(string, encoding) { } const len = string.length; - const mustMatch = (arguments.length > 2 && arguments[2] === true); - if (!mustMatch && len === 0) + if (len === 0) return 0; - if (!encoding) - return (mustMatch ? -1 : byteLengthUtf8(string)); - - const ops = getEncodingOps(encoding); - if (ops === undefined) - return (mustMatch ? -1 : byteLengthUtf8(string)); - return ops.byteLength(string); + if (encoding) { + const ops = getEncodingOps(encoding); + if (ops) { + return ops.byteLength(string); + } + } + return byteLengthUtf8(string); } Buffer.byteLength = byteLength; diff --git a/test/parallel/test-buffer-bytelength.js b/test/parallel/test-buffer-bytelength.js index 33cbb3268440a1..95d54d425bc252 100644 --- a/test/parallel/test-buffer-bytelength.js +++ b/test/parallel/test-buffer-bytelength.js @@ -23,8 +23,6 @@ const vm = require('vm'); ); }); -assert.strictEqual(Buffer.byteLength('', undefined, true), -1); - assert(ArrayBuffer.isView(new Buffer(10))); assert(ArrayBuffer.isView(new SlowBuffer(10))); assert(ArrayBuffer.isView(Buffer.alloc(10)));