summaryrefslogtreecommitdiff
path: root/test/parallel/test-buffer-slice.js
diff options
context:
space:
mode:
authorSakthipriyan Vairamani (thefourtheye) <thechargingvolcano@gmail.com>2016-10-15 00:51:46 +0530
committerJames M Snell <jasnell@gmail.com>2016-10-18 15:51:44 -0700
commitee1469050385186168dc2ecaf816646e34c98000 (patch)
tree3a811b43db19b52c361f5b88be7e2ab49440cf5e /test/parallel/test-buffer-slice.js
parent4e1d6d0d6888d91e7ace077654c9e54fcf245adf (diff)
downloadandroid-node-v8-ee1469050385186168dc2ecaf816646e34c98000.tar.gz
android-node-v8-ee1469050385186168dc2ecaf816646e34c98000.tar.bz2
android-node-v8-ee1469050385186168dc2ecaf816646e34c98000.zip
buffer: coerce slice parameters consistently
As shown in https://github.com/nodejs/node/issues/9096, the offset and end value of the `slice` call are coerced to numbers and then passed to `FastBuffer`, which internally truncates the mantissa part if the number is actually a floating point number. This actually affects the new length of the slice calculation. For example, > const original = Buffer.from('abcd'); undefined > original.slice(original.length / 3).toString() 'bc' This happens because, starting value of the slice is 4 / 3, which is 1.33 (approximately). Now, the length of the slice is calculated as the difference between the actual length of the buffer and the starting offset. So, it becomes 2.67 (4 - 1.33). Now, a new `FastBuffer` is constructed, with the following values as parameters, 1. actual buffer object, 2. starting value, which is 1.33 and 3. the length 2.67. The underlying C++ code truncates the numbers and they become 1 and 2. That is why the result is just `bc`. This patch makes sure that all the offsets are coerced to integers before any calculations are done. Fixes: https://github.com/nodejs/node/issues/9096 PR-URL: https://github.com/nodejs/node/pull/9101 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaƫl Zasso <targos@protonmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
Diffstat (limited to 'test/parallel/test-buffer-slice.js')
-rw-r--r--test/parallel/test-buffer-slice.js10
1 files changed, 10 insertions, 0 deletions
diff --git a/test/parallel/test-buffer-slice.js b/test/parallel/test-buffer-slice.js
index 133d056b17..e5b598e625 100644
--- a/test/parallel/test-buffer-slice.js
+++ b/test/parallel/test-buffer-slice.js
@@ -62,3 +62,13 @@ assert.strictEqual(Buffer.alloc(0).slice(0, 1).length, 0);
// slice(0,0).length === 0
assert.strictEqual(0, Buffer.from('hello').slice(0, 0).length);
+
+{
+ // Regression tests for https://github.com/nodejs/node/issues/9096
+ const buf = Buffer.from('abcd');
+ assert.strictEqual(buf.slice(buf.length / 3).toString(), 'bcd');
+ assert.strictEqual(
+ buf.slice(buf.length / 3, buf.length).toString(),
+ 'bcd'
+ );
+}