Please sign in to comment.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <email@example.com> Reviewed-By: Michaël Zasso <firstname.lastname@example.org> Reviewed-By: Anna Henningsen <email@example.com> Reviewed-By: Franziska Hinkelmann <firstname.lastname@example.org> Reviewed-By: Colin Ihrig <email@example.com> Reviewed-By: Luigi Pinca <firstname.lastname@example.org> Reviewed-By: Brian White <email@example.com>
- Loading branch information
Showing with 13 additions and 4 deletions.