You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
assert: deepEqual/deepStrictEqual throws on equivalent typed arrays #8001
The following code throws an AssertionError, when it should not.
vara=newUint8Array([1,2,3,4]).subarray(1)// slice of parent buffervarb=newUint8Array([2,3,4])console.log(a)// Uint8Array [ 2, 3, 4 ]console.log(b)// Uint8Array [ 2, 3, 4 ]assert.deepEqual(a,b)// NOT OK -- throws!assert.deepStrictEqual(a,b)// NOT OK -- throws!
The error thrown is AssertionError: Uint8Array [ 2, 3, 4 ] deepEqual Uint8Array [ 2, 3, 4 ].
This bug is caused by the optimization introduced in step 7.4 of the _deepEqual algorithm where typed arrays are converted to Buffers for performance reasons. The error is that the typed array's underlying ArrayBuffer is used without respecting it's .byteOffset or .byteLength (i.e. position within the parent ArrayBuffer).
When Buffer is used instead of typed arrays, both assert.deepEqual and assert.deepStrictEqual work as expected without throwing an exception.
varc=Buffer.from([1,2,3,4]).slice(1)// slice of parent buffervard=Buffer.from([2,3,4])console.log(c)// <Buffer 02 03 04>console.log(d)// <Buffer 02 03 04>assert.deepEqual(c,d)// OK -- no exceptionassert.deepStrictEqual(c,d)// OK -- no exception
Step 7.1 of the _deepEqual algorithm has a special case for objects that are instanceof Buffer so this issue hasn't manifested for the Buffer tests in core, even though they're also typed arrays. However, this bug causes issues in the browser buffer test suite, because our Buffer class is not considered instanceof Buffer by the assert package, since it's not a Node Buffer.
Activity
feross commentedon Aug 6, 2016
Here's an attempt at a PR to fix the issue: #8002
HACK: test: add terrible hack to work around node.js assert bug
assert: fix deepEqual/deepStrictEqual on equivalent typed arrays
assert: fix deepEqual/deepStrictEqual on equivalent typed arrays
assert: fix deepEqual/deepStrictEqual on equivalent typed arrays
assert: fix deepEqual/deepStrictEqual on equivalent typed arrays