Skip to content

Commit

Permalink
[Fix] avoid an infinite loop in node 0.8 with Typed Arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 9, 2023
1 parent 3638470 commit f247282
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
9 changes: 8 additions & 1 deletion index.js
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -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",
Expand Down
28 changes: 28 additions & 0 deletions test/cmp.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit f247282

Please sign in to comment.