-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
[v8.x] buffer: zero-fill buffer allocated with invalid content #17467
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
Closed
Changes from 1 commit
Commits
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
Prev
Previous commit
buffer: zero-fill buffer allocated with invalid content
Zero-fill when `Buffer.alloc()` receives invalid fill data. A solution like #17427 which switches to throwing makes sense, but is likely a breaking change. This suggestion leaves the behaviour of `buffer.fill()` untouched, since any change to it would be a breaking change, and lets `Buffer.alloc()` check whether any filling took place or not. PR-URL: #17428 Refs: #17427 Refs: #17423 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
- Loading branch information
commit 29f621825bd0b3e930f14fbb68587c513dc3efdf
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
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 |
|---|---|---|
|
|
@@ -238,7 +238,9 @@ Buffer.alloc = function(size, fill, encoding) { | |
| // be interpreted as a start offset. | ||
| if (typeof encoding !== 'string') | ||
| encoding = undefined; | ||
| return createUnsafeBuffer(size).fill(fill, encoding); | ||
| const ret = createUnsafeBuffer(size); | ||
| if (fill_(ret, fill, encoding) > 0) | ||
| return ret; | ||
| } | ||
| return new FastBuffer(size); | ||
| }; | ||
|
|
@@ -796,15 +798,20 @@ Buffer.prototype.includes = function includes(val, byteOffset, encoding) { | |
| // buffer.fill(buffer[, offset[, end]]) | ||
| // buffer.fill(string[, offset[, end]][, encoding]) | ||
| Buffer.prototype.fill = function fill(val, start, end, encoding) { | ||
| fill_(this, val, start, end, encoding); | ||
| return this; | ||
| }; | ||
|
|
||
| function fill_(buf, val, start, end, encoding) { | ||
| // Handle string cases: | ||
| if (typeof val === 'string') { | ||
| if (typeof start === 'string') { | ||
| encoding = start; | ||
| start = 0; | ||
| end = this.length; | ||
| end = buf.length; | ||
| } else if (typeof end === 'string') { | ||
| encoding = end; | ||
| end = this.length; | ||
| end = buf.length; | ||
| } | ||
|
|
||
| if (encoding !== undefined && typeof encoding !== 'string') { | ||
|
|
@@ -832,19 +839,17 @@ Buffer.prototype.fill = function fill(val, start, end, encoding) { | |
| } | ||
|
|
||
| // Invalid ranges are not set to a default, so can range check early. | ||
|
Member
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. Should it say "so one can range check early."?
Member
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. Or maybe
|
||
| if (start < 0 || end > this.length) | ||
| if (start < 0 || end > buf.length) | ||
| throw new RangeError('Out of range index'); | ||
|
|
||
| if (end <= start) | ||
| return this; | ||
| return 0; | ||
|
|
||
| start = start >>> 0; | ||
| end = end === undefined ? this.length : end >>> 0; | ||
| end = end === undefined ? buf.length : end >>> 0; | ||
|
|
||
| binding.fill(this, val, start, end, encoding); | ||
|
|
||
| return this; | ||
| }; | ||
| return binding.fill(buf, val, start, end, encoding); | ||
| } | ||
|
|
||
|
|
||
| Buffer.prototype.write = function(string, offset, length, encoding) { | ||
|
|
||
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
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.
Maybe leave a comment here?
// Invalid data, no filling.