Skip to content
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

buffer,doc: Throw error instead of assert when buffer too large #40243

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/api/v8.md
Expand Up @@ -323,6 +323,10 @@ added: v8.0.0

Uses a [`DefaultSerializer`][] to serialize `value` into a buffer.

[`ERR_BUFFER_TOO_LARGE`][] will be thrown when trying to
serialize a huge object which requires buffer
larger than [`buffer.constants.MAX_LENGTH`][].

### `v8.deserialize(buffer)`
<!-- YAML
added: v8.0.0
Expand Down Expand Up @@ -548,10 +552,12 @@ A subclass of [`Deserializer`][] corresponding to the format written by
[`DefaultDeserializer`]: #class-v8defaultdeserializer
[`DefaultSerializer`]: #class-v8defaultserializer
[`Deserializer`]: #class-v8deserializer
[`ERR_BUFFER_TOO_LARGE`]: errors.md#err_buffer_too_large
[`Error`]: errors.md#class-error
[`GetHeapSpaceStatistics`]: https://v8docs.nodesource.com/node-13.2/d5/dda/classv8_1_1_isolate.html#ac673576f24fdc7a33378f8f57e1d13a4
[`NODE_V8_COVERAGE`]: cli.md#node_v8_coveragedir
[`Serializer`]: #class-v8serializer
[`buffer.constants.MAX_LENGTH`]: buffer.md#bufferconstantsmax_length
[`deserializer._readHostObject()`]: #deserializer_readhostobject
[`deserializer.transferArrayBuffer()`]: #deserializertransferarraybufferid-arraybuffer
[`serialize()`]: #v8serializevalue
Expand Down
7 changes: 6 additions & 1 deletion src/node_buffer.cc
Expand Up @@ -494,7 +494,12 @@ MaybeLocal<Object> New(Environment* env,
size_t length) {
if (length > 0) {
CHECK_NOT_NULL(data);
CHECK(length <= kMaxLength);
// V8 currently only allows a maximum Typed Array index of max Smi.
if (length > kMaxLength) {
Isolate* isolate(env->isolate());
isolate->ThrowException(ERR_BUFFER_TOO_LARGE(isolate));
return Local<Object>();
}
}

auto free_callback = [](char* data, void* hint) { free(data); };
Expand Down