-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
buffer: coerce offset using Math.trunc() #9341
Conversation
Does |
Did you mean |
@cjihrig no, I mean a signed shift. It does similar to what |
offset |= 0; | ||
if (offset === 0) { | ||
offset = Math.floor(+offset); | ||
if (offset === 0 || Number.isNaN(offset)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since slice()
supports negative numbers Math.round()
must be used. A simple test for that would be
const buf = Buffer.from('abc');
assert.strictEqual(buf.slice(-0.5).toString(), buf.toString();
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: const buf = 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
]);
const chunk1 = Buffer.from([
1, 29, 0, 0, 1, 143, 216, 162, 92, 254, 248, 63, 0
]);
const chunk2 = Buffer.from([
0, 0, 18, 184, 6, 0, 175, 29, 0, 8, 11, 1, 0, 0
]);
const middle = buf.length / 2;
assert.deepStrictEqual(buf.slice(0, middle), chunk1);
assert.deepStrictEqual(buf.slice(middle), chunk2); |
@cjihrig It doesn't pass on v6.x though. Will both commits get backported to v6.x? |
I think it would be simplest to backport both together. |
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. |
@cjihrig My bad. I meant to say to use |
Using |
@cjihrig thanks for the correction. |
OK, moved from |
@@ -807,8 +807,8 @@ Buffer.prototype.toJSON = function() { | |||
|
|||
|
|||
function adjustOffset(offset, length) { | |||
offset |= 0; | |||
if (offset === 0) { | |||
offset = Math.trunc(+offset); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the +
here still necessary? It seems Math.trunc()
handles conversions already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're right. Removed the +
given that use of |
@jasnell I added a comment. Does it work for you? |
Looks like there were two simultaneous CI runs today. One was green. The other had a single Jenkins issue. |
Yep! Thank you! |
@trevnorris @mscdex @rvagg are any of you interested in signing off on this? |
LGTM |
LGTM @jasnell we use binary conversions in several places we shouldn't. Should probably be fixed at some point. |
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
offset
ofBuffer#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