1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

chore(serde_v8): throw error when string buffer exceeds v8 max length (#14588)

This commit is contained in:
Geert-Jan Zwiers 2022-05-26 17:15:44 +02:00 committed by GitHub
parent 402b497299
commit ab9c7f52e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View file

@ -446,3 +446,16 @@ Deno.test(function testBufferBytesCopyFalseGrowExactBytes() {
assertEquals(actualBytes.byteLength, bufSize);
assertEquals(actualBytes.buffer.byteLength, actualBytes.byteLength);
});
Deno.test(function testThrowsErrorWhenBufferExceedsMaxLength() {
const kStringMaxLengthPlusOne = 536870888 + 1;
const bytes = new Uint8Array(kStringMaxLengthPlusOne);
assertThrows(
() => {
new TextDecoder().decode(bytes);
},
TypeError,
"buffer exceeds maximum length",
);
});

View file

@ -11,10 +11,17 @@ impl ToV8 for U16String {
&self,
scope: &mut v8::HandleScope<'a>,
) -> Result<v8::Local<'a, v8::Value>, crate::Error> {
let v =
v8::String::new_from_two_byte(scope, self, v8::NewStringType::Normal)
.unwrap();
Ok(v.into())
let maybe_v =
v8::String::new_from_two_byte(scope, self, v8::NewStringType::Normal);
// 'new_from_two_byte' can return 'None' if buffer length > kMaxLength.
if let Some(v) = maybe_v {
Ok(v.into())
} else {
Err(Error::Message(String::from(
"Cannot allocate String from UTF-16: buffer exceeds maximum length.",
)))
}
}
}