1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

perf(ext/ffi): Revert UTF-8 validity check from getCString (#17741)

This commit is contained in:
Aapo Alasuutari 2023-02-12 18:42:35 +02:00 committed by GitHub
parent d4e5a295f2
commit 5a83af4837
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 12 deletions

View file

@ -145,14 +145,13 @@ where
let ptr = unsafe { ptr.add(offset) };
// SAFETY: Pointer is user provided.
let cstr = unsafe { CStr::from_ptr(ptr as *const c_char) }
.to_str()
.map_err(|_| type_error("Invalid CString pointer, not valid UTF-8"))?;
let value: v8::Local<v8::Value> = v8::String::new(scope, cstr)
.ok_or_else(|| {
type_error("Invalid CString pointer, string exceeds max length")
})?
.into();
let cstr = unsafe { CStr::from_ptr(ptr as *const c_char) }.to_bytes();
let value: v8::Local<v8::Value> =
v8::String::new_from_utf8(scope, cstr, v8::NewStringType::Normal)
.ok_or_else(|| {
type_error("Invalid CString pointer, string exceeds max length")
})?
.into();
Ok(value.into())
}

View file

@ -699,10 +699,26 @@ assertEquals([...uint8Array], [
0x00
]);
try {
assertThrows(() => charView.getCString(), TypeError, "Invalid CString pointer, not valid UTF-8");
} catch (_err) {
console.log("Invalid UTF-8 characters to `v8::String`:", charView.getCString());
// Check that `getCString` works equally to `TextDecoder`
assertEquals(charView.getCString(), new TextDecoder().decode(uint8Array.subarray(0, uint8Array.length - 1)));
// Check a selection of various invalid UTF-8 sequences in C strings and verify
// that the `getCString` API does not cause unexpected behaviour.
for (const charBuffer of [
Uint8Array.from([0xA0, 0xA1, 0x00]),
Uint8Array.from([0xE2, 0x28, 0xA1, 0x00]),
Uint8Array.from([0xE2, 0x82, 0x28, 0x00]),
Uint8Array.from([0xF0, 0x28, 0x8C, 0xBC, 0x00]),
Uint8Array.from([0xF0, 0x90, 0x28, 0xBC, 0x00]),
Uint8Array.from([0xF0, 0x28, 0x8C, 0x28, 0x00]),
Uint8Array.from([0xF8, 0xA1, 0xA1, 0xA1, 0xA1, 0x00]),
Uint8Array.from([0xFC, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0x00]),
]) {
const charBufferPointer = Deno.UnsafePointer.of(charBuffer);
const charString = Deno.UnsafePointerView.getCString(charBufferPointer);
const charBufferPointerArrayBuffer = new Uint8Array(Deno.UnsafePointerView.getArrayBuffer(charBufferPointer, charBuffer.length - 1));
assertEquals(charString, new TextDecoder().decode(charBufferPointerArrayBuffer));
assertEquals([...charBuffer.subarray(0, charBuffer.length - 1)], [...charBufferPointerArrayBuffer]);
}