-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
buffer: prevent failed assertions #1922
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
Closed
brendanashworth
wants to merge
2
commits into
nodejs:master
from
brendanashworth:fix/buffer-segfault-this
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| const binding = process.binding('buffer'); | ||
| const smalloc = process.binding('smalloc'); | ||
| const util = require('util'); | ||
| const assert = require('assert'); | ||
| const internalUtil = require('internal/util'); | ||
| const alloc = smalloc.alloc; | ||
| const truncate = smalloc.truncate; | ||
|
|
@@ -335,6 +336,14 @@ Buffer.byteLength = byteLength; | |
|
|
||
| // toString(encoding, start=0, end=buffer.length) | ||
| Buffer.prototype.toString = function(encoding, start, end) { | ||
| // .toString() can be called automatically on non-Buffers. | ||
| if (!(this instanceof Buffer)) { | ||
| if (arguments.length === 0) | ||
|
Contributor
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 still don't understand the purpose of this check at all. Why not always return O.p.toString when used on a non-buffer. |
||
| return Object.prototype.toString.call(this); | ||
| else | ||
| assert(this instanceof Buffer); | ||
| } | ||
|
|
||
| var loweredCase = false; | ||
|
|
||
| start = start >>> 0; | ||
|
|
@@ -380,8 +389,7 @@ Buffer.prototype.toString = function(encoding, start, end) { | |
|
|
||
|
|
||
| Buffer.prototype.equals = function equals(b) { | ||
| if (!(b instanceof Buffer)) | ||
| throw new TypeError('Argument must be a Buffer'); | ||
| assert(this instanceof Buffer && b instanceof Buffer); | ||
|
|
||
| if (this === b) | ||
| return true; | ||
|
|
@@ -404,8 +412,7 @@ Buffer.prototype.inspect = function inspect() { | |
|
|
||
|
|
||
| Buffer.prototype.compare = function compare(b) { | ||
| if (!(b instanceof Buffer)) | ||
| throw new TypeError('Argument must be a Buffer'); | ||
| assert(this instanceof Buffer && b instanceof Buffer); | ||
|
|
||
| if (this === b) | ||
| return 0; | ||
|
|
@@ -415,6 +422,8 @@ Buffer.prototype.compare = function compare(b) { | |
|
|
||
|
|
||
| Buffer.prototype.indexOf = function indexOf(val, byteOffset) { | ||
| assert(this instanceof Buffer); | ||
|
|
||
| if (byteOffset > 0x7fffffff) | ||
| byteOffset = 0x7fffffff; | ||
| else if (byteOffset < -0x80000000) | ||
|
|
@@ -433,6 +442,8 @@ Buffer.prototype.indexOf = function indexOf(val, byteOffset) { | |
|
|
||
|
|
||
| Buffer.prototype.fill = function fill(val, start, end) { | ||
| assert(this instanceof Buffer); | ||
|
|
||
| start = start >> 0; | ||
| end = (end === undefined) ? this.length : end >> 0; | ||
|
|
||
|
|
@@ -455,6 +466,13 @@ Buffer.prototype.fill = function fill(val, start, end) { | |
| }; | ||
|
|
||
|
|
||
| Buffer.prototype.copy = function copy(target, start, sourceStart, sourceEnd) { | ||
| assert(this instanceof Buffer && target instanceof Buffer); | ||
|
|
||
| return binding.copy(this, target, start, sourceStart, sourceEnd); | ||
| }; | ||
|
|
||
|
|
||
| // XXX remove in v0.13 | ||
| Buffer.prototype.get = util.deprecate(function get(offset) { | ||
| offset = ~~offset; | ||
|
|
@@ -779,6 +797,8 @@ Buffer.prototype.readInt32BE = function(offset, noAssert) { | |
|
|
||
|
|
||
| Buffer.prototype.readFloatLE = function readFloatLE(offset, noAssert) { | ||
| assert(this instanceof Buffer); | ||
|
|
||
| offset = offset >>> 0; | ||
| if (!noAssert) | ||
| checkOffset(offset, 4, this.length); | ||
|
|
@@ -787,6 +807,8 @@ Buffer.prototype.readFloatLE = function readFloatLE(offset, noAssert) { | |
|
|
||
|
|
||
| Buffer.prototype.readFloatBE = function readFloatBE(offset, noAssert) { | ||
| assert(this instanceof Buffer); | ||
|
|
||
| offset = offset >>> 0; | ||
| if (!noAssert) | ||
| checkOffset(offset, 4, this.length); | ||
|
|
@@ -795,6 +817,8 @@ Buffer.prototype.readFloatBE = function readFloatBE(offset, noAssert) { | |
|
|
||
|
|
||
| Buffer.prototype.readDoubleLE = function readDoubleLE(offset, noAssert) { | ||
| assert(this instanceof Buffer); | ||
|
|
||
| offset = offset >>> 0; | ||
| if (!noAssert) | ||
| checkOffset(offset, 8, this.length); | ||
|
|
@@ -803,6 +827,8 @@ Buffer.prototype.readDoubleLE = function readDoubleLE(offset, noAssert) { | |
|
|
||
|
|
||
| Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) { | ||
| assert(this instanceof Buffer); | ||
|
|
||
| offset = offset >>> 0; | ||
| if (!noAssert) | ||
| checkOffset(offset, 8, this.length); | ||
|
|
@@ -1025,6 +1051,8 @@ function checkFloat(buffer, value, offset, ext) { | |
|
|
||
|
|
||
| Buffer.prototype.writeFloatLE = function writeFloatLE(val, offset, noAssert) { | ||
| assert(this instanceof Buffer); | ||
|
|
||
| val = +val; | ||
| offset = offset >>> 0; | ||
| if (!noAssert) | ||
|
|
@@ -1035,6 +1063,8 @@ Buffer.prototype.writeFloatLE = function writeFloatLE(val, offset, noAssert) { | |
|
|
||
|
|
||
| Buffer.prototype.writeFloatBE = function writeFloatBE(val, offset, noAssert) { | ||
| assert(this instanceof Buffer); | ||
|
|
||
| val = +val; | ||
| offset = offset >>> 0; | ||
| if (!noAssert) | ||
|
|
@@ -1045,6 +1075,8 @@ Buffer.prototype.writeFloatBE = function writeFloatBE(val, offset, noAssert) { | |
|
|
||
|
|
||
| Buffer.prototype.writeDoubleLE = function writeDoubleLE(val, offset, noAssert) { | ||
| assert(this instanceof Buffer); | ||
|
|
||
| val = +val; | ||
| offset = offset >>> 0; | ||
| if (!noAssert) | ||
|
|
@@ -1055,6 +1087,8 @@ Buffer.prototype.writeDoubleLE = function writeDoubleLE(val, offset, noAssert) { | |
|
|
||
|
|
||
| Buffer.prototype.writeDoubleBE = function writeDoubleBE(val, offset, noAssert) { | ||
| assert(this instanceof Buffer); | ||
|
|
||
| val = +val; | ||
| offset = offset >>> 0; | ||
| if (!noAssert) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
How?
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.
+Buffer.prototype, it isn't "supported" but it was decided that it shouldn't kill the process. I can add that to the comment, if you'd like.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.
Yes, and some more detail on what "automatically" means would be great...
The fix seems overcomplicated in any case. Why throw for arguments.length !== 0?
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.
To get here you'd have to call
toString.{call,apply}()or to copy it to the prototype of another constructor, etc. Either way you're hosed. Like @domenic mentions, just throw aTypeErrorif theinstanceofcheck fails.