Skip to content

buffer: throw when filling with empty buffers #18129

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

Merged
merged 2 commits into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,14 +511,18 @@ console.log(buf2.toString());
<!-- YAML
added: v5.10.0
changes:
- version: v8.9.3
pr-url: https://github.com/nodejs/node/pull/17428
description: Specifying an invalid string for `fill` now results in a
zero-filled buffer.
- 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.
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/17427
description: Specifying an invalid string for `fill` triggers a thrown
exception.
- version: v8.9.3
pr-url: https://github.com/nodejs/node/pull/17428
description: Specifying an invalid string for `fill` now results in a
zero-filled buffer.
-->

* `size` {integer} The desired length of the new `Buffer`.
Expand Down Expand Up @@ -1224,13 +1228,17 @@ console.log(buf1.equals(buf3));
<!-- YAML
added: v0.5.0
changes:
- version: v5.7.0
pr-url: https://github.com/nodejs/node/pull/4935
description: The `encoding` parameter is supported now.
- 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.
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/17427
description: Specifying an invalid string for `value` triggers a thrown
exception.
- version: v5.7.0
pr-url: https://github.com/nodejs/node/pull/4935
description: The `encoding` parameter is supported now.
-->

* `value` {string|Buffer|integer} The value to fill `buf` with.
Expand Down
14 changes: 7 additions & 7 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -642,20 +642,20 @@ void Fill(const FunctionCallbackInfo<Value>& args) {
str_obj,
enc,
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:

if (str_length >= fill_length)
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;
char* ptr = ts_obj_data + start + str_length;
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1024,3 +1024,10 @@ common.expectsError(() => {
code: 'ERR_INVALID_ARG_VALUE',
type: TypeError
});

common.expectsError(() => {
Buffer.alloc(1, Buffer.alloc(0));
}, {
code: 'ERR_INVALID_ARG_VALUE',
type: TypeError
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue seems to be everywhere ERR_INVALID_ARG_VALUE is used, so consider this non-blocking, but I'd think TypeError is the wrong error to use here. The type of the argument (Buffer) is fine. It's the value that's the problem. I think it should be vanilla Error or maybe RangeError.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To your point, this PR uses the same error handling logic from #17427. In that PR, I was against using a TypeError for the same reasons you list here, but other collaborators felt differently.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think TypeError is fine here.

});