Uh oh!
There was an error while loading. Please reload this page.
buffer: coerce offset using Math.trunc() - #9341
Conversation
Fishrock123
commented
Oct 28, 2016
Does |
cjihrig
commented
Oct 28, 2016
Did you mean |
Fishrock123
commented
Oct 28, 2016
@cjihrig no, I mean a signed shift. It does similar to what |
There was a problem hiding this comment.
Wouldn't it be better if we had Number.isInteger here and drop the Math.floor in the previous line?
There was a problem hiding this comment.
So, for example, an offset of 3.3 would become 0? That seems like it would be a surprising breaking change.
There was a problem hiding this comment.
Since slice() supports negative numbers Math.round() must be used. A simple test for that would be
constbuf=Buffer.from('abc');assert.strictEqual(buf.slice(-0.5).toString(),buf.toString();cjihrig
commented
Oct 28, 2016
It looks like it has the same problems. Using the example from #9101: |
I just noticed that this PR actually fixes another bug (that has existed since v6.3.0 or ac8e1bf) not covered by the regression test in this PR. Here is a test case to add: constbuf=Buffer.from([1,29,0,0,1,143,216,162,92,254,248,63,0,0,0,18,184,6,0,175,29,0,8,11,1,0,0]);constchunk1=Buffer.from([1,29,0,0,1,143,216,162,92,254,248,63,0]);constchunk2=Buffer.from([0,0,18,184,6,0,175,29,0,8,11,1,0,0]);constmiddle=buf.length/2;assert.deepStrictEqual(buf.slice(0,middle),chunk1);assert.deepStrictEqual(buf.slice(middle),chunk2); |
cjihrig
commented
Oct 29, 2016
@cjihrig It doesn't pass on v6.x though. Will both commits get backported to v6.x? |
cjihrig
commented
Oct 29, 2016
I think it would be simplest to backport both together. |
rvagg
commented
Oct 29, 2016
Might be good to add that new test case to this PR even if it passes on master. Moar regression tests! This will be good to expedite into the next v6 patch release but it'd be preferable to get it into a v7 asap. |
trevnorris
commented
Oct 30, 2016
@cjihrig My bad. I meant to say to use |
cjihrig
commented
Oct 30, 2016
Using |
trevnorris
commented
Nov 1, 2016
@cjihrig thanks for the correction. |
cjihrig
commented
Nov 2, 2016
OK, moved from |
There was a problem hiding this comment.
Is the + here still necessary? It seems Math.trunc() handles conversions already?
There was a problem hiding this comment.
I think you're right. Removed the +
mscdex
commented
Nov 2, 2016
cjihrig
commented
Nov 2, 2016
jasnell
commented
Nov 2, 2016
given that use of |
cjihrig
commented
Nov 3, 2016
@jasnell I added a comment. Does it work for you? |
cjihrig
commented
Nov 3, 2016
Looks like there were two simultaneous CI runs today. One was green. The other had a single Jenkins issue. |
jasnell
commented
Nov 3, 2016
Yep! Thank you! |
cjihrig
commented
Nov 3, 2016
@trevnorris@mscdex@rvagg are any of you interested in signing off on this? |
mscdex
commented
Nov 3, 2016
LGTM |
trevnorris
commented
Nov 4, 2016
LGTM @jasnell we use binary conversions in several places we shouldn't. Should probably be fixed at some point. |
jasnell
commented
Nov 4, 2016
Agreed On Friday, November 4, 2016, Trevor Norris notifications@github.com wrote:
|
This is a partial revert of 14d1a8a, which coerced the offset of Buffer#slice() using the | operator. This causes some edge cases to be handled incorrectly. This commit restores the old behavior, but converts offsets to integers using Math.trunc(). This commit does not revert any tests, and adds an additional regression test. Refs: nodejs#9096 Refs: nodejs#9101 PR-URL: nodejs#9341 Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
This is a partial revert of 14d1a8a, which coerced the offset of Buffer#slice() using the | operator. This causes some edge cases to be handled incorrectly. This commit restores the old behavior, but converts offsets to integers using Math.trunc(). This commit does not revert any tests, and adds an additional regression test. Refs: #9096 Refs: #9101 PR-URL: #9341 Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
This is a partial revert of 14d1a8a, which coerced the offset of Buffer#slice() using the | operator. This causes some edge cases to be handled incorrectly. This commit restores the old behavior, but converts offsets to integers using Math.trunc(). This commit does not revert any tests, and adds an additional regression test. Refs: #9096 Refs: #9101 PR-URL: #9341 Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
This is a partial revert of 14d1a8a, which coerced the offset of Buffer#slice() using the | operator. This causes some edge cases to be handled incorrectly. This commit restores the old behavior, but converts offsets to integers using Math.trunc(). This commit does not revert any tests, and adds an additional regression test. Refs: #9096 Refs: #9101 PR-URL: #9341 Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Checklist
make -j8 test(UNIX), orvcbuild test nosign(Windows) passesAffected core subsystem(s)
buffer
Description of change
This is a partial revert of 14d1a8a, which coerced the
offsetofBuffer#slice()using the|operator. This causes some edge cases to be handled incorrectly. This commit restores the old behavior, but converts offsets to integers usingMath.trunc(). This commit does not revert any tests, and adds an additionalregression test.
Refs: #9101
Refs: #9096
cc: @nodejs/buffer