-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
buffer: zero-fill buffer allocated with invalid content #17428
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
Changes from 2 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 |
---|---|---|
|
@@ -510,6 +510,11 @@ console.log(buf2.toString()); | |
### Class Method: Buffer.alloc(size[, fill[, encoding]]) | ||
<!-- YAML | ||
added: v5.10.0 | ||
changes: | ||
- version: REPLACEME | ||
pr-url: https://github.com/nodejs/node/pull/17428 | ||
description: Specifying an invalid string for `fill` now yields a | ||
zero-filled buffer. | ||
--> | ||
|
||
* `size` {integer} The desired length of the new `Buffer`. | ||
|
@@ -1254,6 +1259,19 @@ Example: Fill a `Buffer` with a two-byte character | |
console.log(Buffer.allocUnsafe(3).fill('\u0222')); | ||
``` | ||
|
||
If `value` is contains invalid characters, it is truncated; if no valid | ||
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. Nit: extra 'is' before contains |
||
fill data remains, no filling is performed: | ||
|
||
```js | ||
const buf = Buffer.allocUnsafe(5); | ||
// Prints: <Buffer 61 61 61 61 61> | ||
console.log(buf.fill('a')); | ||
// Prints: <Buffer aa aa aa aa aa> | ||
console.log(buf.fill('aazz', 'hex')); | ||
// Prints: <Buffer aa aa aa aa aa> | ||
console.log(buf.fill('zz', 'hex')); | ||
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.
Edit: nvm. 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. It seems the comments cover the next line, not the previous. 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. Yes, I misread, sorry. |
||
``` | ||
|
||
### buf.includes(value[, byteOffset][, encoding]) | ||
<!-- YAML | ||
added: v5.3.0 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ const { | |
compare: _compare, | ||
compareOffset, | ||
createFromString, | ||
fill: _fill, | ||
fill: bindingFill, | ||
indexOfBuffer, | ||
indexOfNumber, | ||
indexOfString, | ||
|
@@ -266,7 +266,9 @@ Buffer.alloc = function alloc(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)) | ||
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. nit: 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. @targos yup, will fix while landing |
||
return ret; | ||
} | ||
return new FastBuffer(size); | ||
}; | ||
|
@@ -840,15 +842,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') { | ||
|
@@ -877,19 +884,17 @@ Buffer.prototype.fill = function fill(val, start, end, encoding) { | |
} | ||
|
||
// Invalid ranges are not set to a default, so can range check early. | ||
if (start < 0 || end > this.length) | ||
if (start < 0 || end > buf.length) | ||
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE'); | ||
|
||
if (end <= start) | ||
return this; | ||
return 0; | ||
|
||
start = start >>> 0; | ||
end = end === undefined ? this.length : end >>> 0; | ||
end = end === undefined ? buf.length : end >>> 0; | ||
|
||
_fill(this, val, start, end, encoding); | ||
|
||
return this; | ||
}; | ||
return bindingFill(buf, val, start, end, encoding); | ||
} | ||
|
||
|
||
Buffer.prototype.write = function write(string, offset, length, encoding) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
Just a thought but given
yield
is a JS keyword with a specific meaning and not necessarily a super familiar word for non-native speakers, is there another word we could use? Mayberesults in
,creates
orreturns
?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.
You can say the same about
returns
;) I’m happy with anything,results in
sounds fine to me.