From f247282e66060087c51221f77b9170fa0e2e461b Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 9 Jul 2023 15:22:16 -0700 Subject: [PATCH] [Fix] avoid an infinite loop in node 0.8 with Typed Arrays --- index.js | 9 ++++++++- package.json | 2 ++ test/cmp.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index dccfe2b..14cf7a8 100644 --- a/index.js +++ b/index.js @@ -313,9 +313,16 @@ function objEquiv(a, b, opts, channel) { var aWhich = whichTypedArray(a); var bWhich = whichTypedArray(b); - if ((aWhich || bWhich) && aWhich !== bWhich) { + if (aWhich !== bWhich) { return false; } + if (aWhich || bWhich) { // && would work too, because both are true or both false here + if (a.length !== b.length) { return false; } + for (i = 0; i < a.length; i++) { + if (a[i] !== b[i]) { return false; } + } + return true; + } var aIsBuffer = isBuffer(a); var bIsBuffer = isBuffer(b); diff --git a/package.json b/package.json index e5e0ee0..86785b6 100755 --- a/package.json +++ b/package.json @@ -54,7 +54,9 @@ "@ljharb/eslint-config": "^21.1.0", "aud": "^2.0.3", "auto-changelog": "^2.4.0", + "available-typed-arrays": "^1.0.5", "eslint": "=8.8.0", + "for-each": "^0.3.3", "has-symbols": "^1.0.3", "has-typed-arrays": "^1.0.1", "in-publish": "^2.0.1", diff --git a/test/cmp.js b/test/cmp.js index 47fb557..9673b8d 100644 --- a/test/cmp.js +++ b/test/cmp.js @@ -8,6 +8,8 @@ var hasSymbols = require('has-symbols')(); var hasTypedArrays = require('has-typed-arrays')(); var semver = require('semver'); var keys = require('object-keys'); +var availableTypedArrays = require('available-typed-arrays')(); +var forEach = require('for-each'); var safeBuffer = typeof Buffer === 'function' ? Buffer.from && Buffer.from.length > 1 ? Buffer.from : Buffer : null; var buffersAreTypedArrays = typeof Buffer === 'function' && new Buffer(0) instanceof Uint8Array; @@ -1171,6 +1173,32 @@ test('TypedArrays', { skip: !hasTypedArrays }, function (t) { st.end(); }); + forEach(availableTypedArrays, function (name) { + t.test(name + 's', function (st) { + var TA = global[name]; + var isBigInt = name.slice(0, 3) === 'Big'; + var Z = isBigInt ? BigInt : Number; + + st.deepEqualTest( + new TA([Z(1), Z(2), Z(3)]), + new TA([Z(1), Z(2), Z(3)]), + 'two ' + name + 's with the same contents', + true, + true + ); + + st.deepEqualTest( + new TA([Z(1), Z(2), Z(3)]), + new TA([Z(1), Z(2), Z(4)]), + 'two ' + name + 's with different contents', + false, + false + ); + + st.end(); + }); + }); + t.test('one TypedArray faking as another', { skip: !hasDunderProto }, function (st) { var a = new Uint8Array(10); var b = tag(new Int8Array(10), 'Uint8Array');