1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

fix(serde_v8): no panic on reading large text file (#15494)

Co-authored-by: Nayeem Rahman <nayeemrmn99@gmail.com>
This commit is contained in:
Geert-Jan Zwiers 2022-09-01 22:20:11 +02:00 committed by GitHub
parent 778eb1da24
commit 58e76098e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 6 deletions

View file

@ -145,3 +145,45 @@ Deno.test(
assert(data.length > 0);
},
);
Deno.test(
{ permissions: { read: true, write: true } },
function readTextFileSyncV8LimitError() {
const kStringMaxLengthPlusOne = 536870888 + 1;
const bytes = new Uint8Array(kStringMaxLengthPlusOne);
const filePath = "cli/tests/testdata/too_big_a_file.txt";
Deno.writeFileSync(filePath, bytes);
assertThrows(
() => {
Deno.readTextFileSync(filePath);
},
TypeError,
"buffer exceeds maximum length",
);
Deno.removeSync(filePath);
},
);
Deno.test(
{ permissions: { read: true, write: true } },
async function readTextFileV8LimitError() {
const kStringMaxLengthPlusOne = 536870888 + 1;
const bytes = new Uint8Array(kStringMaxLengthPlusOne);
const filePath = "cli/tests/testdata/too_big_a_file_2.txt";
await Deno.writeFile(filePath, bytes);
await assertRejects(
async () => {
await Deno.readTextFile(filePath);
},
TypeError,
"buffer exceeds maximum length",
);
await Deno.remove(filePath);
},
);

View file

@ -1946,7 +1946,12 @@ impl JsRuntime {
for (promise_id, mut resp) in results.into_iter() {
args.push(v8::Integer::new(scope, promise_id).into());
args.push(resp.to_v8(scope).unwrap());
args.push(match resp.to_v8(scope) {
Ok(v) => v,
Err(e) => OpResult::Err(OpError::new(&|_| "TypeError", e.into()))
.to_v8(scope)
.unwrap(),
});
}
let tc_scope = &mut v8::TryCatch::new(scope);

View file

@ -447,11 +447,16 @@ impl<'a, 'b, 'c> ser::Serializer for Serializer<'a, 'b, 'c> {
}
fn serialize_str(self, v: &str) -> JsResult<'a> {
Ok(
v8::String::new(&mut self.scope.borrow_mut(), v)
.unwrap()
.into(),
)
let maybe_str = v8::String::new(&mut self.scope.borrow_mut(), v);
// v8 string can return 'None' if buffer length > kMaxLength.
if let Some(str) = maybe_str {
Ok(str.into())
} else {
Err(Error::Message(String::from(
"Cannot allocate String: buffer exceeds maximum length.",
)))
}
}
fn serialize_bytes(self, v: &[u8]) -> JsResult<'a> {