-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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 slice parameters consistenly #9101
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -807,11 +807,10 @@ Buffer.prototype.toJSON = function() { | |
|
||
|
||
function adjustOffset(offset, length) { | ||
offset = +offset; | ||
if (offset === 0 || Number.isNaN(offset)) { | ||
offset |= 0; | ||
if (offset === 0) { | ||
return 0; | ||
} | ||
if (offset < 0) { | ||
} else if (offset < 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's where the problem lies. There's no range check before the int32 coercion. A large number could fail this. |
||
offset += length; | ||
return offset > 0 ? offset : 0; | ||
} else { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,3 +62,13 @@ assert.strictEqual(Buffer.alloc(0).slice(0, 1).length, 0); | |
|
||
// slice(0,0).length === 0 | ||
assert.strictEqual(0, Buffer.from('hello').slice(0, 0).length); | ||
|
||
{ | ||
// Regression tests for https://github.com/nodejs/node/issues/9096 | ||
const buf = Buffer.from('abcd'); | ||
assert.strictEqual(buf.slice(buf.length / 3).toString(), 'bcd'); | ||
assert.strictEqual( | ||
buf.slice(buf.length / 3, buf.length).toString(), | ||
'bcd' | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're making the change to operate like const b = Buffer.from('abcdefg');
assert.strictEqual(b.slice(-(-1 >>> 0) + 1).toString(), b.toString()); |
||
} |
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.
If you want the floor then please use
Math.floor()
. Using|
allows wrap around. One reason why+
was used before.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.
@thefourtheye
This fails edge cases. For example:
But as this currently stands
offset
will be coerced to2
with the above example.