-
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: prevent abort on bad proto #2012
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const Buffer = require('buffer').Buffer; | ||
const Bp = Buffer.prototype; | ||
|
||
function FakeBuffer() { } | ||
FakeBuffer.__proto__ = Buffer; | ||
FakeBuffer.prototype.__proto__ = Buffer.prototype; | ||
|
||
const fb = new FakeBuffer(); | ||
|
||
assert.throws(function() { | ||
new Buffer(fb); | ||
}, TypeError); | ||
|
||
assert.throws(function() { | ||
+Buffer.prototype; | ||
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. I don't think this should fail, but rather coerce as usual with > '' + net.Socket.prototype
'[object Object]'
> '' + Buffer.prototype
TypeError: blah blah blah 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. This operation calls the toString method on the object. Which is a custom implementation. It seems standard to throw in such cases. 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. I disagree and think that regardless of how the operation works, we shouldn't throw an error on simple coercion. 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. V8 throws the
This type of behavior is defined by the ES spec: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-symbol.prototype.tostring I think it would be smart for us to follow the same pattern. 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. I wasn't aware of that before, thank you, failing with an error now makes sense to me. 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. Neither was I before this PR. Previously I would have agreed with you on just coercing the call. |
||
}, TypeError); | ||
|
||
assert.throws(function() { | ||
Buffer.compare(fb, new Buffer(0)); | ||
}, TypeError); | ||
|
||
assert.throws(function() { | ||
fb.write('foo'); | ||
}, TypeError); | ||
|
||
assert.throws(function() { | ||
Buffer.concat([fb, fb]); | ||
}, TypeError); | ||
|
||
assert.throws(function() { | ||
fb.toString(); | ||
}, TypeError); | ||
|
||
assert.throws(function() { | ||
fb.equals(new Buffer(0)); | ||
}, TypeError); | ||
|
||
assert.throws(function() { | ||
fb.indexOf(5); | ||
}, TypeError); | ||
|
||
assert.throws(function() { | ||
fb.readFloatLE(0); | ||
}, TypeError); | ||
|
||
assert.throws(function() { | ||
fb.writeFloatLE(0); | ||
}, TypeError); | ||
|
||
assert.throws(function() { | ||
fb.fill(0); | ||
}, TypeError); |
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.
length
is only used whenarguments.length === 0
now.