Skip to content

Commit

Permalink
chore(serde_v8): throw error when string buffer exceeds v8 max length (
Browse files Browse the repository at this point in the history
  • Loading branch information
GJZwiers committed May 26, 2022
1 parent 402b497 commit ab9c7f5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
13 changes: 13 additions & 0 deletions cli/tests/unit/buffer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
});
15 changes: 11 additions & 4 deletions serde_v8/magic/u16string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
)))
}
}
}

Expand Down

0 comments on commit ab9c7f5

Please sign in to comment.