mirror of
https://github.com/nodejs/node.git
synced 2025-08-16 06:08:50 +02:00
buffer: throw when filling with empty buffers
Prior to this commit, Node would enter an infinite loop when attempting to fill a non-zero length buffer with a zero length buffer. This commit introduces a thrown exception in this scenario. PR-URL: https://github.com/nodejs/node/pull/18129 Fixes: https://github.com/nodejs/node/issues/18128 Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
This commit is contained in:
parent
db9c556f50
commit
1e802539b2
3 changed files with 22 additions and 7 deletions
|
@ -519,6 +519,10 @@ changes:
|
||||||
pr-url: https://github.com/nodejs/node/pull/17427
|
pr-url: https://github.com/nodejs/node/pull/17427
|
||||||
description: Specifying an invalid string for `fill` triggers a thrown
|
description: Specifying an invalid string for `fill` triggers a thrown
|
||||||
exception.
|
exception.
|
||||||
|
- version: REPLACEME
|
||||||
|
pr-url: https://github.com/nodejs/node/pull/18129
|
||||||
|
description: Attempting to fill a non-zero length buffer with a zero length
|
||||||
|
buffer triggers a thrown exception.
|
||||||
-->
|
-->
|
||||||
|
|
||||||
* `size` {integer} The desired length of the new `Buffer`.
|
* `size` {integer} The desired length of the new `Buffer`.
|
||||||
|
@ -1231,6 +1235,10 @@ changes:
|
||||||
pr-url: https://github.com/nodejs/node/pull/17427
|
pr-url: https://github.com/nodejs/node/pull/17427
|
||||||
description: Specifying an invalid string for `value` triggers a thrown
|
description: Specifying an invalid string for `value` triggers a thrown
|
||||||
exception.
|
exception.
|
||||||
|
- version: REPLACEME
|
||||||
|
pr-url: https://github.com/nodejs/node/pull/18129
|
||||||
|
description: Attempting to fill a non-zero length buffer with a zero length
|
||||||
|
buffer triggers a thrown exception.
|
||||||
-->
|
-->
|
||||||
|
|
||||||
* `value` {string|Buffer|integer} The value to fill `buf` with.
|
* `value` {string|Buffer|integer} The value to fill `buf` with.
|
||||||
|
|
|
@ -642,13 +642,6 @@ void Fill(const FunctionCallbackInfo<Value>& args) {
|
||||||
str_obj,
|
str_obj,
|
||||||
enc,
|
enc,
|
||||||
nullptr);
|
nullptr);
|
||||||
// This check is also needed in case Write() returns that no bytes could
|
|
||||||
// be written. If no bytes could be written, then return -1 because the
|
|
||||||
// string is invalid. This will trigger a throw in JavaScript. Silently
|
|
||||||
// failing should be avoided because it can lead to buffers with unexpected
|
|
||||||
// contents.
|
|
||||||
if (str_length == 0)
|
|
||||||
return args.GetReturnValue().Set(-1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
start_fill:
|
start_fill:
|
||||||
|
@ -656,6 +649,13 @@ void Fill(const FunctionCallbackInfo<Value>& args) {
|
||||||
if (str_length >= fill_length)
|
if (str_length >= fill_length)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
// If str_length is zero, then either an empty buffer was provided, or Write()
|
||||||
|
// indicated that no bytes could be written. If no bytes could be written,
|
||||||
|
// then return -1 because the fill value is invalid. This will trigger a throw
|
||||||
|
// in JavaScript. Silently failing should be avoided because it can lead to
|
||||||
|
// buffers with unexpected contents.
|
||||||
|
if (str_length == 0)
|
||||||
|
return args.GetReturnValue().Set(-1);
|
||||||
|
|
||||||
size_t in_there = str_length;
|
size_t in_there = str_length;
|
||||||
char* ptr = ts_obj_data + start + str_length;
|
char* ptr = ts_obj_data + start + str_length;
|
||||||
|
|
|
@ -1024,3 +1024,10 @@ common.expectsError(() => {
|
||||||
code: 'ERR_INVALID_ARG_VALUE',
|
code: 'ERR_INVALID_ARG_VALUE',
|
||||||
type: TypeError
|
type: TypeError
|
||||||
});
|
});
|
||||||
|
|
||||||
|
common.expectsError(() => {
|
||||||
|
Buffer.alloc(1, Buffer.alloc(0));
|
||||||
|
}, {
|
||||||
|
code: 'ERR_INVALID_ARG_VALUE',
|
||||||
|
type: TypeError
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue