diff options
author | Matt Loring <mattloring@google.com> | 2015-12-07 16:52:53 -0700 |
---|---|---|
committer | Trevor Norris <trev.norris@gmail.com> | 2015-12-07 16:52:53 -0700 |
commit | ec836547c486394505bc3ea6e195dbd2f3137cf6 (patch) | |
tree | 3d1faf85728444c42bf7fa804cc14b8be9a039e3 /lib | |
parent | d2c8ba5248d68a6b152ea8443a1c50feab35a162 (diff) | |
download | node-new-ec836547c486394505bc3ea6e195dbd2f3137cf6.tar.gz |
buffer: fix range checking for slowToString
If `start` is not a valid number in the range, then the default value
zero will be used. Same way, if `end` is not a valid number in the
accepted range, then, by default, the length of the buffer is assumed.
Fixes: https://github.com/nodejs/node/issues/2668
Ref: https://github.com/nodejs/node/pull/2919
PR-URL: https://github.com/nodejs/node/pull/4019
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/buffer.js | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/lib/buffer.js b/lib/buffer.js index 41f1f4b391..7221f42c5a 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -327,13 +327,34 @@ Object.defineProperty(Buffer.prototype, 'offset', { function slowToString(encoding, start, end) { var loweredCase = false; - start = start >>> 0; - end = end === undefined || end === Infinity ? this.length : end >>> 0; + // No need to verify that "this.length <= MAX_UINT32" since it's a read-only + // property of a typed array. + + // This behaves neither like String nor Uint8Array in that we set start/end + // to their upper/lower bounds if the value passed is out of range. + // undefined is handled specially as per ECMA-262 6th Edition, + // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization. + if (start === undefined || start < 0) + start = 0; + // Return early if start > this.length. Done here to prevent potential uint32 + // coercion fail below. + if (start > this.length) + return ''; + + if (end === undefined || end > this.length) + end = this.length; + + if (end <= 0) + return ''; + + // Force coersion to uint32. This will also coerce falsey/NaN values to 0. + end >>>= 0; + start >>>= 0; + + if (end <= start) + return ''; if (!encoding) encoding = 'utf8'; - if (start < 0) start = 0; - if (end > this.length) end = this.length; - if (end <= start) return ''; while (true) { switch (encoding) { |