-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ext/ffi): Check CStr for UTF-8 validity on read (#15318)
Co-authored-by: Phosra <[email protected]>
- Loading branch information
Showing
3 changed files
with
39 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
// Run using cargo test or `--v8-options=--allow-natives-syntax` | ||
|
||
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"; | ||
import { | ||
assertThrows, | ||
} from "../../test_util/std/testing/asserts.ts"; | ||
|
@@ -186,6 +187,12 @@ const dylib = Deno.dlopen(libPath, { | |
"static_ptr": { | ||
type: "pointer", | ||
}, | ||
/** | ||
* Invalid UTF-8 characters, buffer of length 14 | ||
*/ | ||
"static_char": { | ||
type: "pointer", | ||
}, | ||
}); | ||
const { symbols } = dylib; | ||
|
||
|
@@ -478,6 +485,23 @@ uint32Array[0] = 55; // MUTATES! | |
console.log("uint32Array[0] after mutation:", uint32Array[0]); | ||
console.log("Static ptr value after mutation:", view.getUint32()); | ||
|
||
// Test non-UTF-8 characters | ||
|
||
const charView = new Deno.UnsafePointerView(dylib.symbols.static_char); | ||
|
||
const charArrayBuffer = charView.getArrayBuffer(14); | ||
const uint8Array = new Uint8Array(charArrayBuffer); | ||
assertEquals([...uint8Array], [ | ||
0xC0, 0xC1, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF, | ||
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()); | ||
} | ||
|
||
(function cleanup() { | ||
dylib.close(); | ||
throwCallback.close(); | ||
|