Skip to content

Conversation

@cjihrig
Copy link
Contributor

@cjihrigcjihrig commented Oct 28, 2016

Checklist
  • make -j8 test (UNIX), or vcbuild test nosign (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

buffer

Description of change

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: #9101
Refs: #9096

cc: @nodejs/buffer

@nodejs-github-botnodejs-github-bot added the buffer Issues and PRs related to the buffer subsystem. label Oct 28, 2016
@Fishrock123
Copy link
Contributor

Does offset >> 0 fix these bugs rather than Math.floor()?

@cjihrig
Copy link
ContributorAuthor

Did you mean offset >>> 0? If so, that would create an uint32, and slice() supports negative numbers.

@Fishrock123
Copy link
Contributor

@cjihrig no, I mean a signed shift. It does similar to what | 0 does but perhaps with less caveats.

lib/buffer.js Outdated
Copy link
Contributor

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?

Copy link
ContributorAuthor

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.

Copy link
Contributor

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

constbuf=Buffer.from('abc');assert.strictEqual(buf.slice(-0.5).toString(),buf.toString();

@cjihrig
Copy link
ContributorAuthor

no, I mean a signed shift.

It looks like it has the same problems. Using the example from #9101:

> (-(-1 >>> 0) + 1) | 0 2 > (-(-1 >>> 0) + 1) >> 0 2 

@mscdex
Copy link
Contributor

mscdex commented Oct 28, 2016

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
Copy link
ContributorAuthor

@mscdex that code is passing for me on master already, probably because of 14d1a8a.

@mscdex
Copy link
Contributor

mscdex commented Oct 29, 2016

@cjihrig It doesn't pass on v6.x though. Will both commits get backported to v6.x?

@cjihrig
Copy link
ContributorAuthor

I think it would be simplest to backport both together.

@rvagg
Copy link
Member

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
Copy link
Contributor

@cjihrig My bad. I meant to say to use round() in my PR review.

@cjihrig
Copy link
ContributorAuthor

Using round() seems like it could lead to some confusion. What about Math.trunc() to just drop everything after the decimal point?

@trevnorris
Copy link
Contributor

@cjihrig thanks for the correction. Math.trunc() is the correct solution.

@cjihrigcjihrig changed the title buffer: coerce offset using Math.floor()buffer: coerce offset using Math.trunc()Nov 2, 2016
@cjihrig
Copy link
ContributorAuthor

OK, moved from Math.floor() to Math.trunc(), and added the code from #9341 (comment) and #9341 (comment) as regression tests.

lib/buffer.js Outdated
Copy link
Contributor

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?

Copy link
ContributorAuthor

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 +

@mscdex
Copy link
Contributor

@cjihrig
Copy link
ContributorAuthor

@jasnell
Copy link
Member

given that use of | is pretty common already, would you mind adding a code comment indicating why Math.trunc() is used so that someone later on doesn't come along and change it? Otherwise LGTM

@cjihrig
Copy link
ContributorAuthor

@jasnell I added a comment. Does it work for you?

@cjihrig
Copy link
ContributorAuthor

Looks like there were two simultaneous CI runs today. One was green. The other had a single Jenkins issue.

@jasnell
Copy link
Member

Yep! Thank you!

@cjihrig
Copy link
ContributorAuthor

@trevnorris@mscdex@rvagg are any of you interested in signing off on this?

@mscdex
Copy link
Contributor

LGTM

@trevnorris
Copy link
Contributor

LGTM

@jasnell we use binary conversions in several places we shouldn't. Should probably be fixed at some point.

@jasnell
Copy link
Member

Agreed

On Friday, November 4, 2016, Trevor Norris [email protected] wrote:

LGTM

@jasnellhttps://github.com/jasnell we use binary conversions in
several places we shouldn't. Should probably be fixed at some point.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#9341 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAa2eazC9UGhivwqia5zjnVbztBtippKks5q63DugaJpZM4Kjgw5
.

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 <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Brian White <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
@cjihrigcjihrig merged commit a02b13f into nodejs:masterNov 5, 2016
@cjihrigcjihrig deleted the buf branch November 5, 2016 00:45
evanlucas pushed a commit that referenced this pull request Nov 7, 2016
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 <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Brian White <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
@evanlucasevanlucas mentioned this pull request Nov 7, 2016
MylesBorins pushed a commit that referenced this pull request Nov 18, 2016
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 <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Brian White <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
MylesBorins pushed a commit that referenced this pull request Nov 19, 2016
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 <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Brian White <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
@MylesBorinsMylesBorins mentioned this pull request Nov 22, 2016
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bufferIssues and PRs related to the buffer subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants

@cjihrig@Fishrock123@mscdex@rvagg@trevnorris@jasnell@thefourtheye@MylesBorins@nodejs-github-bot