Please sign in to comment.
buffer: coerce slice parameters consistently
As shown in #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: #9096 PR-URL: #9101 Reviewed-By: James M Snell <firstname.lastname@example.org> Reviewed-By: Michaël Zasso <email@example.com> Reviewed-By: Anna Henningsen <firstname.lastname@example.org> Reviewed-By: Franziska Hinkelmann <email@example.com> Reviewed-By: Colin Ihrig <firstname.lastname@example.org> Reviewed-By: Luigi Pinca <email@example.com> Reviewed-By: Brian White <firstname.lastname@example.org>
- Loading branch information...
Showing with 13 additions and 4 deletions.