summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAnna Henningsen <sqrt@entless.org>2016-03-26 03:29:13 +0100
committerBenjamin Gruenbaum <inglor@gmail.com>2016-03-31 21:48:02 +0300
commitcf949293ba55e5d8193e26623c6e9201b14cd819 (patch)
tree2e3e3bd0dd392cb925ef20e8e541f32cd5dd81c9 /lib
parentd6c9f64e98092bbca41209bb1babe22bee64be42 (diff)
downloadandroid-node-v8-cf949293ba55e5d8193e26623c6e9201b14cd819.tar.gz
android-node-v8-cf949293ba55e5d8193e26623c6e9201b14cd819.tar.bz2
android-node-v8-cf949293ba55e5d8193e26623c6e9201b14cd819.zip
assert: Check typed array view type in deepEqual
Do not convert typed arrays to `Buffer` for deepEqual since their values may not be accurately represented by 8-bit ints. Instead perform binary comparison of underlying `ArrayBuffer`s, but only when the array types match. Never apply any kind of optimization for floating-point typed arrays since bit pattern equality is not the right kind of check for them. PR-URL: https://github.com/nodejs/node/pull/5910 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Fixes: https://github.com/nodejs/node/issues/5907
Diffstat (limited to 'lib')
-rw-r--r--lib/assert.js17
1 files changed, 13 insertions, 4 deletions
diff --git a/lib/assert.js b/lib/assert.js
index 61aba557ec..b4de551e28 100644
--- a/lib/assert.js
+++ b/lib/assert.js
@@ -29,6 +29,7 @@ const compare = process.binding('buffer').compare;
const util = require('util');
const Buffer = require('buffer').Buffer;
const pSlice = Array.prototype.slice;
+const pToString = (obj) => Object.prototype.toString.call(obj);
// 1. The assert module provides functions that throw
// AssertionError's when particular conditions are not met. The
@@ -170,10 +171,18 @@ function _deepEqual(actual, expected, strict) {
(expected === null || typeof expected !== 'object')) {
return strict ? actual === expected : actual == expected;
- // If both values are instances of typed arrays, wrap them in
- // a Buffer each to increase performance
- } else if (ArrayBuffer.isView(actual) && ArrayBuffer.isView(expected)) {
- return compare(Buffer.from(actual), Buffer.from(expected)) === 0;
+ // If both values are instances of typed arrays, wrap their underlying
+ // ArrayBuffers in a Buffer each to increase performance
+ // This optimization requires the arrays to have the same type as checked by
+ // Object.prototype.toString (aka pToString). Never perform binary
+ // comparisons for Float*Arrays, though, since e.g. +0 === -0 but their
+ // bit patterns are not identical.
+ } else if (ArrayBuffer.isView(actual) && ArrayBuffer.isView(expected) &&
+ pToString(actual) === pToString(expected) &&
+ !(actual instanceof Float32Array ||
+ actual instanceof Float64Array)) {
+ return compare(Buffer.from(actual.buffer),
+ Buffer.from(expected.buffer)) === 0;
// 7.5 For all other Object pairs, including Array objects, equivalence is
// determined by having the same number of owned properties (as verified