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

fix(core): panic on big string allocation #7395

Merged
merged 3 commits into from
Sep 9, 2020
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
19 changes: 16 additions & 3 deletions core/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,22 @@ fn decode(
)
};

let text_str =
v8::String::new_from_utf8(scope, &buf, v8::NewStringType::Normal).unwrap();
rv.set(text_str.into())
// If `String::new_from_utf8()` returns `None`, this means that the
// length of the decoded string would be longer than what V8 can
// handle. In this case we return `RangeError`.
//
// For more details see:
// - https://encoding.spec.whatwg.org/#dom-textdecoder-decode
// - https://github.com/denoland/deno/issues/6649
// - https://github.com/v8/v8/blob/d68fb4733e39525f9ff0a9222107c02c28096e2a/include/v8.h#L3277-L3278
match v8::String::new_from_utf8(scope, &buf, v8::NewStringType::Normal) {
Some(text) => rv.set(text.into()),
None => {
let msg = v8::String::new(scope, "string too long").unwrap();
let exception = v8::Exception::range_error(scope, msg);
scope.throw_exception(exception);
}
};
}

fn queue_microtask(
Expand Down
11 changes: 11 additions & 0 deletions core/encode_decode_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ function main() {

assert(Deno.core.decode(new Uint8Array(fixture1)) === "𝓽𝓮𝔁𝓽");
assert(Deno.core.decode(new Uint8Array(fixture2)) === "Hello �� World");

// See https://github.com/denoland/deno/issues/6649
let thrown = false;
try {
Deno.core.decode(new Uint8Array(2 ** 29));
} catch (e) {
thrown = true;
assert(e instanceof RangeError);
assert(e.message === "string too long");
}
assert(thrown);
}

main();