-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
Description
- Version: 6.3.1
- Platform: macOS 10.11.6
- Subsystem: assert
The following code throws an AssertionError
, when it should not.
var a = new Uint8Array([1, 2, 3, 4]).subarray(1) // slice of parent buffer
var b = new Uint8Array([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.
var c = Buffer.from([1, 2, 3, 4]).slice(1) // slice of parent buffer
var d = 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 exception
assert.deepStrictEqual(c, d) // OK -- no exception
cc @trevnorris @bnoordhuis @jasnell @addaleax
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.