aboutsummaryrefslogtreecommitdiff
path: root/lib/internal/util.js
diff options
context:
space:
mode:
authorSakthipriyan Vairamani (thefourtheye) <thechargingvolcano@gmail.com>2016-11-06 19:01:19 +0530
committerSakthipriyan Vairamani (thefourtheye) <thechargingvolcano@gmail.com>2016-11-26 11:15:34 +0530
commitca37fa527f174b547893817fe8c67a3befa02317 (patch)
tree97d7f74cb902b7c0074acdb8baa794d697b8c671 /lib/internal/util.js
parent561eade31726348ddc377f69fdadc5d1ce7db687 (diff)
downloadandroid-node-v8-ca37fa527f174b547893817fe8c67a3befa02317.tar.gz
android-node-v8-ca37fa527f174b547893817fe8c67a3befa02317.tar.bz2
android-node-v8-ca37fa527f174b547893817fe8c67a3befa02317.zip
buffer: convert offset & length to int properly
As per ecma-262 2015's #sec-%typedarray%-buffer-byteoffset-length, `offset` would be an integer, not a 32 bit unsigned integer. Also, `length` would be an integer with the maximum value of 2^53 - 1, not a 32 bit unsigned integer. This would be a problem because, if we create a buffer from an arraybuffer, from an offset which is greater than 2^32, it would be actually pointing to a different location in arraybuffer. For example, if we use 2^40 as offset, then the actual value used will be 0, because `byteOffset >>>= 0` will convert `byteOffset` to a 32 bit unsigned int, which is based on 2^32 modulo. PR-URL: https://github.com/nodejs/node/pull/9492 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Diffstat (limited to 'lib/internal/util.js')
-rw-r--r--lib/internal/util.js18
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/internal/util.js b/lib/internal/util.js
index 4ada8dd0cc..ae8b1e0b64 100644
--- a/lib/internal/util.js
+++ b/lib/internal/util.js
@@ -161,3 +161,21 @@ exports.cachedResult = function cachedResult(fn) {
return result;
};
};
+
+/*
+ * Implementation of ToInteger as per ECMAScript Specification
+ * Refer: http://www.ecma-international.org/ecma-262/6.0/#sec-tointeger
+ */
+const toInteger = exports.toInteger = function toInteger(argument) {
+ const number = +argument;
+ return Number.isNaN(number) ? 0 : Math.trunc(number);
+};
+
+/*
+ * Implementation of ToLength as per ECMAScript Specification
+ * Refer: http://www.ecma-international.org/ecma-262/6.0/#sec-tolength
+ */
+exports.toLength = function toLength(argument) {
+ const len = toInteger(argument);
+ return len <= 0 ? 0 : Math.min(len, Number.MAX_SAFE_INTEGER);
+};